Skip to content

Commit a4734db

Browse files
authored
Merge pull request aws-samples#2906 from kakakakakku/apigw-lambda-bedrock-js-sam
apigw-lambda-bedrock-js-sam: Update ModelId to Claude Sonnet 4.5
2 parents 2a31c14 + 6417520 commit a4734db

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

apigw-lambda-bedrock-js-sam/README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Amazon API Gateway to AWS Lambda to Amazon Bedrock using AWS SDK for JS and SAM
22

3-
This sample project deploys an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function is written in TypeScript, calls the Amazon Bedrock API for Anthropic Claude-v2 model and returns a response containing the generated content.
3+
This sample project deploys an Amazon API Gateway REST API with an AWS Lambda integration. The Lambda function is written in TypeScript, calls the Amazon Bedrock API for Anthropic Claude Sonnet 4.5 model and returns a response containing the generated content.
44

55
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-lambda-bedrock-js-sam
66

@@ -100,16 +100,16 @@ Amazon Bedrock users need to request access to models before they are available
100100
101101
## How it works
102102
103-
This SAM project uses Amazon Bedrock API for Anthropic Claude-v2 model to generate content based on given prompt. This is exposed through a serverless REST API. Please refer to the architecture diagram below:
103+
This SAM project uses Amazon Bedrock API for Anthropic Claude Sonnet 4.5 model to generate content based on given prompt. This is exposed through a serverless REST API. Please refer to the architecture diagram below:
104104
![End to End Architecture](images/architecture.png)
105105
106106
Here's a breakdown of the steps:
107107

108108
1. **Amazon API Gateway**: Receives the HTTP POST request containing the prompt.
109109

110-
2. **AWS Lambda**: Triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using boto3 bedrock-runtime API. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.
110+
2. **AWS Lambda**: Triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using boto3 bedrock-runtime API. It uses Anthropic Claude Sonnet 4.5 model and sets other required parameters to fixed values for simplicity.
111111

112-
3. **Amazon Bedrock**: Based on the given prompt, using Anthropic Claude-v2 model generates the content and returns the response to Lambda.
112+
3. **Amazon Bedrock**: Based on the given prompt, using Anthropic Claude Sonnet 4.5 model generates the content and returns the response to Lambda.
113113

114114
4. **Response**: Lambda processes the Bedrock output and sends it back to the user via the API Gateway.
115115

@@ -124,18 +124,17 @@ curl -d '{"prompt": "Please write 5 lines on Solar Systems"}' -H 'Content-Type:
124124
The API returns a response with generated content. Such as (Your output may vary):
125125

126126
```
127-
"{\"completion\":\" Here are a few key things about Amazon Web Services (AWS):\\n\\n- AWS is a cloud computing platform that provides services like compute power, storage, databases, analytics, etc on demand over the internet. It operates data centers around the world that customers can access.\\n\\n- Some of the most popular AWS services are EC2 (virtual servers), S3 (cloud storage), Lambda (serverless computing), CloudFront (content delivery network), Route 53 (DNS service), and many more. \\n\\n- AWS was first launched in 2006 and has grown to be the largest cloud provider in the world. Many big companies like Netflix, Airbnb, Expedia rely on AWS for their infrastructure.\\n\\n- Customers pay only for the services they use without any upfront costs. This makes AWS attractive for startups and companies that want flexibility and don't want to manage their own hardware.\\n\\n- AWS offers high availability, scalability, security and reliability at a fraction of the\",\"stop_reason\":\"max_tokens\",\"stop\":null}"
127+
{"model":"claude-sonnet-4-5-20250929","id":"msg_bdrk_01S1Rn7gPy3cdRt9aFWf255a","type":"message","role":"assistant","content":[{"type":"text","text":"# Five Facts About Solar Systems\n\n1. **Our Solar System** formed approximately 4.6 billion years ago from a giant rotating cloud of gas and dust called the solar nebula.\n\n2. **The Sun** contains 99.8% of the Solar System's total mass and provides the gravitational force that keeps all planets in orbit.\n\n3. **Eight planets** orbit our Sun in elliptical paths, divided into rocky terrestrial planets (Mercury, Venus, Earth, Mars) and gas/ice giants (Jupiter, Saturn, Uranus, Neptune).\n\n4. **Beyond Neptune** lies the Kuiper Belt, home to dwarf planets like Pluto and countless icy bodies left over from the Solar System's formation.\n\n5. **Scientists estimate** there are billions of other solar systems in our Milky Way galaxy alone, many potentially harboring planets in habitable zones."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":16,"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"cache_creation":{"ephemeral_5m_input_tokens":0,"ephemeral_1h_input_tokens":0},"output_tokens":192}}
128128
```
129129
130-
131130
## Cleanup
132131
133132
1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:
134133
135134
```bash
136135
sam delete
137136
```
138-
2. Delete the Lambda layer version using the `delete_lambda_layer.sh` script. You may have to give execution permission to the file. You will need to pass the Lambda layer name and the version in the inpout when requested:
137+
2. Delete the Lambda layer version using the `delete_lambda_layer.sh` script. You may have to give execution permission to the file. You will need to pass the Lambda layer name and the version in the input when requested:
139138
```bash
140139
./delete_lambda_layer.sh
141140
```

apigw-lambda-bedrock-js-sam/example-pattern.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"headline": "How it works",
99
"text": [
1010
"Amazon API Gateway receives the HTTP POST request containing a text which will is taken as a prompt.",
11-
"AWS Lambda is triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using AWS SDK for javascript bedrock-runtime API. It uses Anthropic Claude-v2 model and sets other required parameters to fixed values for simplicity.",
12-
"Based on the given prompt, using Anthropic Claude-v2 model, Amazon Bedrock generates the content and returns the response to Lambda.",
11+
"AWS Lambda is triggered by the API Gateway, this function forwards the prompt to Amazon Bedrock API using AWS SDK for javascript bedrock-runtime API. It uses Anthropic Claude Sonnet 4.5 model and sets other required parameters to fixed values for simplicity.",
12+
"Based on the given prompt, using Anthropic Claude Sonnet 4.5 model, Amazon Bedrock generates the content and returns the response to Lambda.",
1313
"Lambda processes the Bedrock output and sends it back to the user via the API Gateway."
1414
]
1515
},

apigw-lambda-bedrock-js-sam/src/index.mjs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ export const handler = async (event) => {
99
//console.log(prompt)
1010
//const prompt = 'Write a text to be posted on my social media channels about how Amazon Bedrock works';
1111
const body = {
12-
'prompt': "\n\nHuman:" + prompt + "\n\nAssistant:",
13-
"temperature": 0.5,
14-
"top_p": 1,
15-
"top_k": 250,
16-
"max_tokens_to_sample": 200,
17-
"stop_sequences": ["\n\nHuman:"]
18-
}
12+
anthropic_version: "bedrock-2023-05-31",
13+
messages: [
14+
{
15+
role: "user",
16+
content: [
17+
{
18+
"type": "text",
19+
"text": prompt
20+
}
21+
],
22+
},
23+
],
24+
max_tokens: 200
25+
};
1926
const input = {
2027
body: JSON.stringify(body),
2128
contentType: "application/json",
2229
accept: "application/json",
23-
modelId: "anthropic.claude-v2",
30+
modelId: "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
2431
};
2532
const command = new InvokeModelCommand(input);
2633
const res = await client.send(command);
@@ -29,7 +36,7 @@ export const handler = async (event) => {
2936
//console.log(JSON.stringify(stringifiedResponse));
3037
const response = {
3138
statusCode: 200,
32-
body: JSON.stringify(stringifiedResponse),
39+
body: stringifiedResponse,
3340
};
3441
return response;
35-
};
42+
};

0 commit comments

Comments
 (0)