Building Gen AI applications on AWS Bedrock

February 09, 2025

Let's start building generative AI applications on AWS platform Bedrock. In this guide, we'll explore the features, pricing, and practical implementation of various foundation models, allowing you to harness the power of generative AI in your projects.

What is AWS Bedrock?

AWS Bedrock is a fully managed service that simplifies the process of building and scaling generative AI applications. It provides access to a wide range of foundation models (FMs) from leading AI startups and Amazon, all available through a unified API. This means you can leverage various large language models (LLMs) and other generative models without the hassle of managing infrastructure or worrying about scalability.

With Bedrock, developers can quickly start customising foundation models using their own data, enabling seamless integration into applications. This serverless experience allows you to focus on developing your AI solutions rather than managing the underlying infrastructure.

Key Features of AWS Bedrock

  • Access to Multiple Models: Bedrock supports various FMs, including those from AI21 Labs, Anthropic, Stability AI, and Amazon’s own Titan models, allowing you to choose the best model for your specific use case.
  • Serverless Experience: You don’t need to worry about provisioning or managing servers, which reduces operational overhead and allows for rapid deployment.
  • Customisation: You can fine-tune models with your data, ensuring they meet your specific requirements more effectively.
  • Integration with AWS Tools: Bedrock works seamlessly with other AWS services, enabling you to build comprehensive solutions that leverage the full power of the AWS ecosystem.

Demo of Amazon Bedrock

Let’s dive into a practical demonstration of how to use AWS Bedrock to perform various tasks. We’ll start by setting up our environment and then execute some API calls to interact with the foundation models.

Setting Up Your Environment

To begin, you will need to create a virtual environment and install the necessary libraries. Open your terminal and execute the following commands:

conda create -p venv python=3.10 -y
conda activate venv
pip install boto3 awscli

Once your environment is set up, you can proceed to configure AWS CLI to connect to your AWS account. You will need an IAM user with the appropriate permissions to access Bedrock.

Creating an IAM User

Follow these steps to create an IAM user:

  • Log in to the AWS Management Console.
  • Navigate to the IAM service and click on "Users".
  • Click "Add user" and provide a username, such as bedrock-user.
  • Select "Programmatic access" as the access type.
  • Attach existing policies directly, and select "AdministratorAccess" for full access (be cautious with this in production environments).
  • Click "Next" through the options until you reach "Create user".
  • Download the CSV file containing your access key ID and secret access key.

Configuring AWS CLI

With your IAM user set up, you can configure AWS CLI. Use the following command and enter your access keys when prompted:

aws configure

This command will ask for your AWS Access Key ID, Secret Access Key, default region name, and output format. Enter the details as follows:

AWS Access Key ID [None]: YOUR_ACCESS_KEY_ID
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Default region name [None]: us-east-1
Default output format [None]: json

Making API Calls to AWS Bedrock

Now that you have configured your environment and AWS CLI, you can start making API calls to AWS Bedrock. Here’s an example of how to generate text using a foundation model:

import boto3

# Create a Bedrock client
client = boto3.client('bedrock', region_name='us-east-1')

# Define the parameters for the API call
response = client.invoke_model(
    modelId='YOUR_MODEL_ID',
    body={
        "input": "Generate a creative story about a robot learning to dance."
    }
)

# Print the response
print(response['body'])

This code snippet demonstrates how to create a Bedrock client using Boto3 and invoke a foundation model to generate text. Replace YOUR_MODEL_ID with the specific model ID you wish to use.

Understanding Pricing

Pricing for AWS Bedrock is based on the number of input and output tokens processed by the models. Each model may have different pricing structures, so it’s essential to review the details specific to the foundation model you intend to use.

Pricing Breakdown

Here’s a general overview of how pricing works:

  • Input Tokens: Charges apply based on the number of tokens in the input text sent to the model.
  • Output Tokens: Charges are also incurred for the tokens generated by the model as output.

For example, if you use a model that processes 1,000 input tokens and generates 1,500 output tokens, you will be billed accordingly based on the rates specified for that model. You can find detailed pricing information on the AWS pricing page for Bedrock.

Creating a User in AWS

In addition to the IAM user we created earlier, you might want to set up additional users for collaborative work or different access levels. Here’s how you can create another user:

  • Return to the IAM service in the AWS Management Console.
  • Click on "Users" and then "Add user".
  • Provide a unique username and select "Programmatic access".
  • Attach the necessary policies based on the user’s role (e.g., read-only access, admin access).
  • Complete the setup by clicking "Create user".
  • Download the access keys for the new user.

By managing users properly, you can ensure that each team member has the appropriate access level while maintaining security and compliance within your AWS environment.

Accessing Models

To effectively use AWS Bedrock, you need to ensure that you have access to the models available in your selected region. By default, you may not have access to all the models, especially when you’re operating in regions like Asia Pacific. The first step is to navigate to the model access settings in your AWS Management Console.

Here’s how to manage model access:

  • Log into your AWS Management Console and select the Bedrock service.
  • Ensure you are in the correct region, typically US East (N. Virginia), for the widest model availability.
  • Click on Manage Model Access to view the models available to you.
  • If necessary, request access to specific models that are not already available.

Once access is granted, you can start invoking models directly from your application code. Always ensure you have the appropriate permissions set for your IAM user to access Bedrock models.

Using Foundation Models

Foundation models are the backbone of your generative AI applications. AWS Bedrock provides a variety of these models, allowing you to select one that fits your use case best. Each model comes with its own strengths, so understanding the parameters and capabilities is crucial.

For instance, if you're working with the Llama 2 model, you would typically specify parameters such as max tokens, temperature, and top-p to control the output generation. Here’s a brief overview of how to set these parameters:

  • Max Tokens: Defines the maximum length of the generated response.
  • Temperature: Controls the randomness of the output; lower values make the output more deterministic.
  • Top-p: Limits the model's output to the most probable words, creating more coherent responses.

Invoking the Model

Once you have selected a model and configured your parameters, the next step is to invoke the model using the AWS SDK. Below is a code example demonstrating how to invoke the Llama 2 model:

import boto3
import json

# Create a Bedrock client
client = boto3.client('bedrock', region_name='us-east-1')

# Define the parameters for the API call
payload = {
    "prompt": "Act as Shakespeare and write a poem about machine learning.",
    "max_tokens": 100,
    "temperature": 0.7,
    "top_p": 0.9
}

# Convert payload to JSON format
body = json.dumps(payload)

# Invoke the model
response = client.invoke_model(
    modelId='Llama2-70B',
    body=body,
    contentType='application/json',
    accept='application/json'
)

# Process the response
response_body = json.loads(response['body'])
print(response_body['generation'])

This code illustrates how to invoke the model with your defined parameters and handle the response to extract the generated text.

Image Generation with Stable Diffusion

AWS Bedrock also supports image generation through models like Stable Diffusion. This allows you to create high-quality images based on textual prompts. The process is similar to text generation but requires specific parameters for image creation.

Here’s an example of how to generate an image using Stable Diffusion:

import boto3
import json

# Create a Bedrock client
client = boto3.client('bedrock', region_name='us-east-1')

# Define the parameters for the API call
image_payload = {
    "text_prompt": "A serene beach during sunset with palm trees.",
    "width": 1024,
    "height": 1024,
    "steps": 50,
    "cfg_scale": 7.5
}

# Convert payload to JSON format
image_body = json.dumps(image_payload)

# Invoke the image generation model
image_response = client.invoke_model(
    modelId='StableDiffusion-1.0',
    body=image_body,
    contentType='application/json',
    accept='application/json'
)

# Process the image response
image_data = json.loads(image_response['body'])
image_base64 = image_data['artifacts'][0]['base64']
with open('output_image.png', 'wb') as image_file:
    image_file.write(base64.b64decode(image_base64))

This snippet demonstrates how to set up the payload for image generation and save the resulting image to a file. Make sure to adjust the parameters based on your specific requirements.

Code Walkthrough: Setting Up the Environment

Setting up your environment is critical for a smooth development experience. You need to ensure that you have the necessary libraries installed and your AWS credentials configured correctly. Here’s a step-by-step guide:

  • Create a virtual environment to isolate your project dependencies:
conda create -p venv python=3.10 -y
conda activate venv
  • Install the required libraries:
pip install boto3 awscli
  • Configure your AWS CLI with your credentials:
aws configure
  • Follow the prompts to enter your AWS Access Key ID, Secret Access Key, and region.

With your environment set up, you can start coding and invoking AWS Bedrock models to build your generative AI applications.

Understanding Foundation Models

Foundation models are large-scale machine learning models that serve as the basis for various applications, allowing developers to fine-tune them for specific tasks. AWS Bedrock provides access to an array of these models from top AI companies, including AI21 Labs, Anthropic, Stability AI, and Amazon’s own Titan models. This diverse selection enables you to choose the most suitable model for your unique use case.

In essence, foundation models are pre-trained on vast datasets, enabling them to understand context and generate human-like text, images, and more. They can be customized using your own data, making them highly adaptable for specific applications, such as chatbots, content generation, and image synthesis.

Types of Foundation Models Available

  • Text Generation Models: These models, like Llama 2 and Jurassic 2, excel at generating coherent and contextually relevant text.
  • Image Generation Models: Models such as Stable Diffusion can create high-quality images based on text prompts, enabling creative applications.
  • Multimodal Models: Some models can process both text and images, allowing for more complex tasks that require understanding across different modalities.

By leveraging these foundation models, developers can accelerate their AI projects without starting from scratch. Instead, they can focus on fine-tuning and integrating these powerful models into their applications.

Hands-On Lab and Learning Resources

AWS Bedrock provides a hands-on lab environment where users can experiment with different foundation models. This interactive setting allows developers to understand the capabilities of each model more thoroughly and see firsthand how they can be applied to real-world tasks.

Getting Started with Hands-On Labs

To begin using the hands-on labs, follow these steps:

  • Log in to your AWS Management Console and navigate to the Bedrock service.
  • Select the "Hands-On Lab" option from the menu.
  • Choose a specific model you’re interested in exploring.
  • Follow the provided instructions to set up your environment and start making API calls.

These labs not only offer practical experience but also include guided tutorials and examples that can help you understand how to implement various features of AWS Bedrock.

Learning Resources

In addition to hands-on labs, AWS provides a wealth of learning resources, including:

  • Documentation: Comprehensive guides on using AWS Bedrock and its models.
  • Webinars and Workshops: Live sessions hosted by AWS experts covering best practices and use cases.
  • Community Forums: Engage with other developers to share insights, ask questions, and collaborate on projects.

Utilising these resources can significantly enhance your understanding of foundation models and how to leverage them effectively within your applications.

Examples of Use Cases

Foundation models can be applied across various domains, providing innovative solutions to complex problems. Here are some notable use cases that exemplify the versatility of AWS Bedrock's foundation models:

Text Generation and Processing

  • Content Creation: Use models like Llama 2 to generate articles, blog posts, or marketing copy tailored to specific audiences.
  • Chatbots: Implement advanced conversational agents that can understand context and provide informative responses.
  • Code Generation: Leverage models to assist in writing and debugging code, improving developer productivity.

Image Generation

  • Creative Design: Generate artwork and designs based on textual descriptions, ideal for marketing materials and social media posts.
  • Product Visualisation: Create realistic images of products from textual descriptions, enhancing e-commerce experiences.

Multimodal Applications

  • Interactive Storytelling: Combine text and images to create engaging stories or presentations.
  • Visual Question Answering: Develop applications that can answer questions based on images and accompanying text.

These examples illustrate the vast potential of foundation models in transforming how businesses operate and interact with customers. By integrating these models into their workflows, companies can unlock new levels of efficiency and creativity.

Pricing Overview

Understanding the pricing structure of AWS Bedrock is crucial for budgeting your projects effectively. The cost is primarily based on the number of tokens processed during interactions with the foundation models.

Token-Based Pricing

Here’s a breakdown of how pricing works:

  • Input Tokens: Charges are incurred for each token in the input text sent to the model.
  • Output Tokens: Additional charges apply for the tokens generated by the model as output.

For instance, if a model processes 1,000 input tokens and generates 1,500 output tokens, you will be billed based on the rates specified for that model. Each model may have different pricing tiers, so it’s essential to refer to the AWS pricing page for detailed information.

Example Pricing Scenario

If you work with a model that charges $0.0004 per input token and $0.0006 per output token, the total cost for processing would be calculated as follows:

  • Input Cost: 1,000 tokens * $0.0004 = $0.40
  • Output Cost: 1,500 tokens * $0.0006 = $0.90
  • Total Cost: $0.40 + $0.90 = $1.30

By monitoring token usage and optimising your prompts, you can manage costs effectively while utilising the powerful capabilities of AWS Bedrock.

Deploying Applications

Once you’ve developed your application using AWS Bedrock, the next step is deployment. AWS provides several options for deploying your generative AI applications, ensuring scalability and reliability.

Deployment Options

  • AWS Lambda: Use serverless functions to run your application code in response to events, automatically scaling based on demand.
  • Amazon ECS/EKS: Deploy containerised applications using Amazon Elastic Container Service (ECS) or Elastic Kubernetes Service (EKS) for more control over your environment.
  • Elastic Beanstalk: Simplify application deployment and management with a fully managed service that automatically handles the infrastructure for you.

Best Practices for Deployment

To ensure a smooth deployment process, consider the following best practices:

  • Testing: Rigorously test your application in a staging environment before deploying to production.
  • Monitoring: Implement monitoring tools to track application performance and usage metrics.
  • Cost Management: Regularly review your token usage and adjust your application to optimise costs.

By following these guidelines, you can effectively deploy and manage your generative AI applications on AWS, harnessing the full power of AWS Bedrock.

Follow my journey

Get my latest thoughts, posts and project updates in your inbox. No spam, I promise.