A Step-by-Step Guide to Creating a Hello World LLM with AWS Bedrock and SAM (Lambda and API Gateway)

A Step-by-Step Guide to Creating a Hello World LLM with AWS Bedrock and SAM (Lambda and API Gateway)

Introduction:

Embarking on the journey of serverless architecture often begins with a simple "Hello World Bedrock" application, providing a foundational understanding of key concepts and SAM framework and AWS Bedrock - Serverless GenAI as a Service.

Whether you're a newcomer to serverless development or looking to explore the capabilities of AWS Bedrock, this step-by-step demonstration will equip you with the knowledge and confidence to kickstart your serverless projects. Join us as we unravel the intricacies of Bedrock and SAM, uncovering the seamless integration of Lambda functions and API Gateway for a robust serverless architecture. Let's embark on this hands-on journey together, where "Hello World" becomes the gateway to learning of AWS Bedrock and SAM.

Upgrading / Installing SAM (Serverless Application Model) Framework:

$ brew install aws-sam-cli
$ sam --version
SAM CLI, version 1.105.0

Note: I am using MAC, So I am using Brew - You can follow below documentation to update the MAC : https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/manage-sam-cli-versions.html

Quickstart

You can download from Github and Perform the Sam Deploy.

git clone https://github.com/jayyanar/learning-aws-bedrock/tree/main/blog9/simple-bedrock-lambda-app

Validate and Deploy the SAM Template

# Validate the Code
$ cd simple-bedrock-lambda-app && sam validate

# Build the Template
$ sam build

# Deploy the Template
$ sam deploy

Verify the Deployment from Cloudformation

Update the IAM Role for Bedrock Role Access Policy for Lambda Deployed

Enhance the security of your Bedrock application by updating the IAM Role access policy. A sample policy is provided to serve as a reference, offering insights into best practices for securing your serverless architecture.

--> Create IAM Policy (Inline or Custom Policy) and update to the IAM Role

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "bedrock:InvokeModel",
            "Resource": "arn:aws:bedrock:*::foundation-model/amazon.titan-text-lite-v1"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "bedrock:ListModelInvocationJobs",
            "Resource": "*"
        }
    ]
}

Verify Via API Gateway for Bedrock Response from Lambda

Step by Step - Instruction.

Create HelloWorld via SAM

Ensure a smooth deployment of your serverless application by verifying the CloudFormation template. This section guides you through the necessary steps to validate your configuration before launching your application to the AWS cloud.

$ sam init

You can preselect a particular runtime or package type when using the `sam init` experience.
Call `sam init --help` to learn more.

Which template source would you like to use?
        1 - AWS Quick Start Templates
        2 - Custom Template Location

--> Select 1 --> AWS Quick Start Templates

Choose an AWS Quick Start application template
        1 - Hello World Example
        2 - Data processing
        3 - Hello World Example with Powertools for AWS Lambda
        4 - Multi-step workflow
        5 - Scheduled task
        6 - Standalone function
        7 - Serverless API
        8 - Infrastructure event management
        9 - Lambda Response Streaming
        10 - Serverless Connector Hello World Example
        11 - Multi-step workflow with Connectors
        12 - GraphQLApi Hello World Example
        13 - Full Stack
        14 - Lambda EFS example
        15 - Hello World Example With Powertools for AWS Lambda
        16 - DynamoDB Example
        17 - Machine Learning
Template:

--> Select 1 --> Hello World Example

Use the most popular runtime and package type? (Python and zip) [y/N]: 

Which runtime would you like to use?
        1 - aot.dotnet7 (provided.al2)
        2 - dotnet6
        3 - go1.x
        4 - go (provided.al2)
        5 - go (provided.al2023)
        6 - graalvm.java11 (provided.al2)
        7 - graalvm.java17 (provided.al2)
        8 - java21
        9 - java17
        10 - java11
        11 - java8.al2
        12 - java8
        13 - nodejs20.x
        14 - nodejs18.x
        15 - nodejs16.x
        16 - python3.9
        17 - python3.8
        18 - python3.12
        19 - python3.11
        20 - python3.10
        21 - ruby3.2
        22 - ruby2.7
        23 - rust (provided.al2)
        24 - rust (provided.al2023)
Runtime: 18

--> Select 18 --> python3.12

What package type would you like to use?
        1 - Zip
        2 - Image
Package type: 1

--> Select 1 --> Zip

Based on your selections, the only dependency manager available is pip. We will proceed copying the template using pip.

Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: n

Would you like to enable monitoring using CloudWatch Application Insights?

For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]: n

Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]: y

Structured Logging in JSON format might incur an additional cost. View https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs.html#monitoring-cloudwatchlogs-pricing for more details

--> Give a Project Name

Project name [sam-app]: simple-bedrock-lambda-app

  -----------------------
    Generating application:
    -----------------------
    Name: simple-bedrock-lambda-app
    Runtime: python3.12
    Architectures: x86_64
    Dependency Manager: pip
    Application Template: hello-world
    Output Directory: .
    Configuration file: simple-bedrock-lambda-app/samconfig.toml

    Next steps can be found in the README file at simple-bedrock-lambda-app/README.md

Update the requirement.txt for Boto3 Update.

Path: cd ~/simple-bedrock-lambda-app/hello_world/requirements.txt

requests
boto3~=1.33.6
botocore~=1.33.6%

Make Code Changes Needed from VSCode and Sync Using SAM CLI

Path: cat ~/simple-bedrock-lambda-app/hello_world/app.py

import json
import boto3

# Bedrock Runtime client used to invoke and question the models
bedrock_runtime = boto3.client(
    service_name='bedrock-runtime',
    region_name='us-east-1'  # Replace with your desired region
)

def lambda_handler(event, context):

    # PROVIDE your prompt here
    lite_prompt = "2 difference between AWS DynamoDB and AWS Redis"

    body = json.dumps({
        "inputText": lite_prompt,
        "textGenerationConfig": {
            "maxTokenCount": 128,
            "stopSequences": [],
            "temperature": 0,
            "topP": 0.9
        }
    })
    # The actual call to retrieve a response from the model
    response = bedrock_runtime.invoke_model(
        body=body,
        modelId="amazon.titan-text-lite-v1",  # Replace with your model ID
        accept='application/json',
        contentType='application/json'
        )

    response_body = json.loads(response.get('body').read())
    response_text = response_body.get('results')[0].get('outputText')
    parse_text = response_text[response_text.index('\n')+1:]
    model_completion = parse_text.strip()

    #  This code will send a respone of Text Completion with stats
    return {
        'statusCode': 200,
        'body': json.dumps(model_completion)
    }

Validate and Deploy the SAM Template

Patience is key! Understand the importance of waiting for the deployment process to complete successfully. This section provides insights into monitoring the deployment progress to ensure a seamless and error-free execution.

# Validate the Code
$ cd simple-bedrock-lambda-app && sam validate

# Build the Template
$ sam build

# Deploy the Template
$ sam deploy

Verify the Deployment from Cloudformation

Update the IAM Role for Bedrock Role Access Policy for Lambda Deployed

Enhance the security of your Bedrock application by updating the IAM Role access policy. A sample policy is provided to serve as a reference, offering insights into best practices for securing your serverless architecture.

--> Create IAM Policy (Inline or Custom Policy) and update to the IAM Role

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "bedrock:InvokeModel",
            "Resource": "arn:aws:bedrock:*::foundation-model/amazon.titan-text-lite-v1"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": "bedrock:ListModelInvocationJobs",
            "Resource": "*"
        }
    ]
}

Verify Via API Gateway for Bedrock Response from Lambda

Any Changes Needed from VSCode and Sync Using SAM Code

Explore the integration of Visual Studio Code (VSCode) with SAM, allowing for efficient coding and synchronization. Learn about any additional changes needed within your development environment and how to sync them using SAM code.

$ cd simple-bedrock-lambda-app && sam sync --stack-name simple-bedrock-lambda-app --watch

Check the Bedrock Cloudwatch Dashboard - Invocation

Feel Free to check on Dashboard

Check the Bedrock Cloudwatch Dashboard - Token Count

Log Insights for Cloudwatch Logs Information

--> Go to Log Insights --> Select the Cloudwatch Log Group --> <Where the logs Bedrock Logs Stored> --> Check on my Blog1 for reference --> Paste the below Query (You can custom the queries also via "Query Generator")

fields input.inputBodyJson.inputText,input.inputBodyJson.textGenerationConfig.maxTokenCount,input.inputBodyJson.textGenerationConfig.temperature,input.inputBodyJson.textGenerationConfig.topP,input.inputContentType,input.inputTokenCount,modelId,output.outputBodyJson.results.0.completionReason,output.outputBodyJson.results.0.outputText,output.outputBodyJson.results.0.tokenCount,output.outputTokenCount,region

-- Thanks for Reading - Share your comments if you interested on Live Youtube on this Blogs

Feel free to read AWS Bedrock Learning Series.

https://blog.dataopslabs.com/series/aws-bedrock