
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)
1
2
3
4
$ brew install aws-sam-cli
$ sam --version
SAM CLI, version 1.105.0
1
git clone https://github.com/jayyanar/learning-aws-bedrock/tree/main/blog9-Lambda-Basic
1
2
3
4
5
6
7
8
# Validate the Code
$ cd simple-bedrock-lambda-app && sam validate
# Build the Template
$ sam build
# Deploy the Template
$ sam deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"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": "*"
}
]
}
1
2
3
4
5
6
7
8
$ 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
1
2
3
4
What package type would you like to use?
1 - Zip
2 - Image
Package type: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
2
3
requests
boto3~=1.33.6
botocore~=1.33.6%
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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)
}
1
2
3
4
5
6
7
8
# Validate the Code
$ cd simple-bedrock-lambda-app && sam validate
# Build the Template
$ sam build
# Deploy the Template
$ sam deploy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
{
"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": "*"
}
]
}
1
$ cd simple-bedrock-lambda-app && sam sync --stack-name simple-bedrock-lambda-app --watch
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