Skip to content

Pattern update - apigw-lambda-bedrock-cdk-python #2723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2025
Merged
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
20 changes: 10 additions & 10 deletions apigw-lambda-bedrock-cdk-python/README.md
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please update the title as well to include full service names, e.g. Amazon API Gateway.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Text generation via ApiGateway -> Lambda -> Bedrock
# Text generation via Amazon API Gateway -> AWS Lambda -> Amazon Bedrock

![architecture](architecture/architecture.png)

Expand All @@ -18,27 +18,27 @@ Important: this application uses various AWS services and there are costs associ
## Amazon Bedrock setup instructions
You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).

1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in us-east-1 (N. Virginia) and us-west-2 (Oregon) regions.
1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in most major regions.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in most major regions.
1. In the AWS console, select the region from which you want to access Amazon Bedrock. At the time of writing, Amazon Bedrock is available in most regions.


![Region Selection](bedrock_setup/region-selection.png)

1. Find **Amazon Bedrock** by searching in the AWS console.

![Bedrock Search](bedrock_setup/bedrock-search.png)
![Amazon Bedrock Search](bedrock_setup/bedrock-search.png)

1. Expand the side menu.

![Bedrock Expand Menu](bedrock_setup/bedrock-menu-expand.png)
![Amazon Bedrock Expand Menu](bedrock_setup/bedrock-menu-expand.png)

1. From the side menu, select **Model access**.

![Model Access](bedrock_setup/model-access-link.png)
![Amazon Bedrock Model Access](bedrock_setup/model-access-link.png)

1. Select the **Edit** button.

![Model Access View](bedrock_setup/model-access-view.png)
![Amazon Bedrock Model Access View](bedrock_setup/model-access-view.png)

6. Use the checkboxes to select the models you wish to enable. Review the applicable EULAs as needed. Click **Save changes** to activate the models in your account. For this pattern we only need Anthropic/Claude but feel free to experiment with others.
6. Use the checkboxes to select the models you wish to enable. Review the applicable EULAs as needed. Click **Save changes** to activate the models in your account. For this pattern we only need Anthropic Claude 3 Haiku but feel free to experiment with others.

## Deployment Instructions

Expand Down Expand Up @@ -74,14 +74,14 @@ You must request access to a model before you can use it. If you try to use the
```
cdk deploy
```
1. After deployment completes, take a look at the Outputs section. There will be an entry containing the URL of the API Gateway resource you just created. Copy that URL as you'll need it for your tests.
1. After deployment completes, take a look at the Outputs section. There will be an entry containing the URL of the Amazon API Gateway resource you just created. Copy that URL as you'll need it for your tests.

The format of the URL will be something like `https://{id}.execute-api.{region}.amazonaws.com/prod`


## How it works

CDK will create an Api Gateway, along with a resource and a POST method. There's a AWS Lambda function that will be taking the prompt and invoking an Amazon Bedrock model (anthropic.claude-v2) synchronously. If you wish to try other models, make sure to modify the policy attached to the Lambda function and invoke the right model.
CDK will create an Amazon API Gateway, along with a resource and a POST method. There is an AWS Lambda function that will be taking the prompt and invoking an Amazon Bedrock model (anthropic.claude-3-haiku-20240307-v1:0) synchronously. If you wish to try other models, make sure to modify the policy attached to the Lambda function and invoke the right model.

This pattern is a synchronous pattern. For an asynchronous approach, please check [this](../apigw-rest-api-sqs-lambda-bedrock-cdk) pattern that involves the usage of Amazon SQS.

Expand All @@ -107,7 +107,7 @@ Follow the example below and replace `{your-api-url}` with your api url from ste
```

## Extra Resources
* [Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
* [Amazon Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)

----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ class ApigwLambdaBedrockStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)

#lambda layer containing boto
#AWS Lambda Layer containing boto
layer = _lambda.LayerVersion(self, "Boto3Layer",
code=_lambda.Code.from_asset("./boto_layer.zip"),
compatible_runtimes=[_lambda.Runtime.PYTHON_3_10]
)

#add policy to invoke bedrock model
#add policy to invoke Amazon Bedrock model
invoke_model_policy = iam.Policy(self, "InvokeModelPolicy",
statements=[
iam.PolicyStatement(
actions=["bedrock:InvokeModel"],
resources=[f"arn:aws:bedrock:{self.region}::foundation-model/anthropic.claude-v2"]
resources=[f"arn:aws:bedrock:{self.region}::foundation-model/*"]
)
]
)

# Create the Lambda function and attach the layer
# Create AWS Lambda function and attach the layer
lambda_function = _lambda.Function(self, "MyFunction",
runtime=_lambda.Runtime.PYTHON_3_10,
handler="index.handler",
Expand All @@ -39,7 +39,7 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:

invoke_model_policy.attach_to_role(lambda_function.role)

#create api gateway
#create Amazon API Gateway
api = apigw.RestApi(self, "ServerlessLandGenAI",)

#create a new resource
Expand Down
Binary file modified apigw-lambda-bedrock-cdk-python/architecture/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 11 additions & 9 deletions apigw-lambda-bedrock-cdk-python/function_code/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ def handler(event, context):
prompt = body.get('prompt', 'Write a text to be posted on my social media channels about how Amazon Bedrock works')

body = json.dumps({
'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': prompt}
],
'max_tokens': 200,
'temperature': 0.5,
'top_p': 1,
'top_k': 250
}) #all parameters (except for prompt) are set to default values

modelId = 'anthropic.claude-v2'
modelId = 'anthropic.claude-3-haiku-20240307-v1:0'
accept = 'application/json'
contentType = 'application/json'

Expand All @@ -28,6 +30,6 @@ def handler(event, context):
return {
'statusCode': 200,
'body': json.dumps({
'generated-text': response_body
'generated-text': response_body.get('content', '')
})
}
}