Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions apigw-lambda-bedrock-js-sam/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Amazon API Gateway to AWS Lambda to Amazon Bedrock using AWS SDK for JS and SAM

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.
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.

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

Expand Down Expand Up @@ -100,16 +100,16 @@ Amazon Bedrock users need to request access to models before they are available

## How it works

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:
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:
![End to End Architecture](images/architecture.png)

Here's a breakdown of the steps:

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

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.
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.

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

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

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

```
"{\"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}"
{"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}}
```


## Cleanup

1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:

```bash
sam delete
```
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:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

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:
```bash
./delete_lambda_layer.sh
```
Expand Down
4 changes: 2 additions & 2 deletions apigw-lambda-bedrock-js-sam/example-pattern.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"headline": "How it works",
"text": [
"Amazon API Gateway receives the HTTP POST request containing a text which will is taken as a prompt.",
"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.",
"Based on the given prompt, using Anthropic Claude-v2 model, Amazon Bedrock generates the content and returns the response to Lambda.",
"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.",
"Based on the given prompt, using Anthropic Claude Sonnet 4.5 model, Amazon Bedrock generates the content and returns the response to Lambda.",
"Lambda processes the Bedrock output and sends it back to the user via the API Gateway."
]
},
Expand Down
27 changes: 17 additions & 10 deletions apigw-lambda-bedrock-js-sam/src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ export const handler = async (event) => {
//console.log(prompt)
//const prompt = 'Write a text to be posted on my social media channels about how Amazon Bedrock works';
const body = {
'prompt': "\n\nHuman:" + prompt + "\n\nAssistant:",
"temperature": 0.5,
"top_p": 1,
"top_k": 250,
"max_tokens_to_sample": 200,
"stop_sequences": ["\n\nHuman:"]
}
anthropic_version: "bedrock-2023-05-31",
messages: [
{
role: "user",
content: [
{
"type": "text",
"text": prompt
}
],
},
],
max_tokens: 200
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const input = {
body: JSON.stringify(body),
contentType: "application/json",
accept: "application/json",
modelId: "anthropic.claude-v2",
modelId: "global.anthropic.claude-sonnet-4-5-20250929-v1:0",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Update ModelId.

};
const command = new InvokeModelCommand(input);
const res = await client.send(command);
Expand All @@ -29,7 +36,7 @@ export const handler = async (event) => {
//console.log(JSON.stringify(stringifiedResponse));
const response = {
statusCode: 200,
body: JSON.stringify(stringifiedResponse),
body: stringifiedResponse,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Removed JSON.stringify because response escaped like this.

"{\"model\":\"claude-sonnet-4-5-20250929\",\"id\":\"msg_bdrk_016r5f2XBbdEPKDiAD24SJGq\",\"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 all the mass in our Solar System, making it by far the most dominant object gravitationally.\\n\\n3. **Eight planets** orbit our Sun in elliptical paths: Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, and Neptune, ranging from rocky inner planets to gaseous outer giants.\\n\\n4. **Beyond Neptune** lies the Kuiper Belt, home to dwarf planets like Pluto and countless icy objects left over from the Solar System's formation.\\n\\n5. **Scientists estimate** there are billions of other solar systems (planetary systems) throughout our Milky Way galaxy alone, many potentially harboring Earth-like worlds.\"}],\"stop_reason\":\"end_turn\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":16,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":0,\"output_tokens\":195}}"

};
return response;
};
};