Skip to content

Commit 9118ecb

Browse files
committed
Merge branch 'main' of https://github.com/sidd130/serverless-patterns into sidd130-feature-sqs-lambda-s3
2 parents 70c246e + a7268e6 commit 9118ecb

File tree

6 files changed

+27
-25
lines changed

6 files changed

+27
-25
lines changed

apigw-lambda-bedrock-cdk-python/README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Text generation via ApiGateway -> Lambda -> Bedrock
1+
# Text generation via Amazon API Gateway -> AWS Lambda -> Amazon Bedrock
22

33
![architecture](architecture/architecture.png)
44

@@ -18,27 +18,27 @@ Important: this application uses various AWS services and there are costs associ
1818
## Amazon Bedrock setup instructions
1919
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).
2020

21-
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.
21+
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.
2222

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

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

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

2929
1. Expand the side menu.
3030

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

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

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

3737
1. Select the **Edit** button.
3838

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

41-
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.
41+
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.
4242

4343
## Deployment Instructions
4444

@@ -74,14 +74,14 @@ You must request access to a model before you can use it. If you try to use the
7474
```
7575
cdk deploy
7676
```
77-
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.
77+
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.
7878
7979
The format of the URL will be something like `https://{id}.execute-api.{region}.amazonaws.com/prod`
8080
8181
8282
## How it works
8383
84-
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.
84+
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.
8585
8686
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.
8787
@@ -107,7 +107,7 @@ Follow the example below and replace `{your-api-url}` with your api url from ste
107107
```
108108
109109
## Extra Resources
110-
* [Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
110+
* [Amazon Bedrock Api Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/welcome.html)
111111
112112
----
113113
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

apigw-lambda-bedrock-cdk-python/apigw_lambda_bedrock/apigw_lambda_bedrock_stack.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,23 @@ class ApigwLambdaBedrockStack(Stack):
1212
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
1313
super().__init__(scope, construct_id, **kwargs)
1414

15-
#lambda layer containing boto
15+
#AWS Lambda Layer containing boto
1616
layer = _lambda.LayerVersion(self, "Boto3Layer",
1717
code=_lambda.Code.from_asset("./boto_layer.zip"),
1818
compatible_runtimes=[_lambda.Runtime.PYTHON_3_10]
1919
)
2020

21-
#add policy to invoke bedrock model
21+
#add policy to invoke Amazon Bedrock model
2222
invoke_model_policy = iam.Policy(self, "InvokeModelPolicy",
2323
statements=[
2424
iam.PolicyStatement(
2525
actions=["bedrock:InvokeModel"],
26-
resources=[f"arn:aws:bedrock:{self.region}::foundation-model/anthropic.claude-v2"]
26+
resources=[f"arn:aws:bedrock:{self.region}::foundation-model/*"]
2727
)
2828
]
2929
)
3030

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

4040
invoke_model_policy.attach_to_role(lambda_function.role)
4141

42-
#create api gateway
42+
#create Amazon API Gateway
4343
api = apigw.RestApi(self, "ServerlessLandGenAI",)
4444

4545
#create a new resource
-24.8 KB
Loading
153 KB
Loading

apigw-lambda-bedrock-cdk-python/function_code/index.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@ def handler(event, context):
1010
prompt = body.get('prompt', 'Write a text to be posted on my social media channels about how Amazon Bedrock works')
1111

1212
body = json.dumps({
13-
'prompt': "\n\nHuman:" + prompt + "\n\nAssistant:",
14-
"temperature": 0.5,
15-
"top_p": 1,
16-
"top_k": 250,
17-
"max_tokens_to_sample": 200,
18-
"stop_sequences": ["\n\nHuman:"]
13+
'anthropic_version': 'bedrock-2023-05-31',
14+
'messages': [
15+
{'role': 'user', 'content': prompt}
16+
],
17+
'max_tokens': 200,
18+
'temperature': 0.5,
19+
'top_p': 1,
20+
'top_k': 250
1921
}) #all parameters (except for prompt) are set to default values
2022

21-
modelId = 'anthropic.claude-v2'
23+
modelId = 'anthropic.claude-3-haiku-20240307-v1:0'
2224
accept = 'application/json'
2325
contentType = 'application/json'
2426

@@ -28,6 +30,6 @@ def handler(event, context):
2830
return {
2931
'statusCode': 200,
3032
'body': json.dumps({
31-
'generated-text': response_body
33+
'generated-text': response_body.get('content', '')
3234
})
33-
}
35+
}

apigw-secretsmanager-apikey-cdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"typescript": "~5.6.3"
2121
},
2222
"dependencies": {
23-
"aws-cdk-lib": "2.181.1",
23+
"aws-cdk-lib": "2.189.1",
2424
"constructs": "^10.0.0"
2525
}
2626
}

0 commit comments

Comments
 (0)