-
Notifications
You must be signed in to change notification settings - Fork 951
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2548 from ReethiJ/reethi-bedrock-agent-lambda
New serverless pattern submission - bedrock-agents-lambda
- Loading branch information
Showing
10 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Amazon Bedrock Agents with AWS Lambda | ||
|
||
![Architecture](image/architecture.png) | ||
|
||
In this pattern, we show you how to use Amazon Bedrock Agents to call an API and get the results from it for a user. Bedrock Agents is helpful in various situations such as fetching real-time information from APIs, interacting with knowledge bases, and performing actions based on user inputs. For more information on Bedrock agents, see [Automate tasks in your application using AI agents](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html). | ||
|
||
> [!Important] | ||
> this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. | ||
## Requirements | ||
|
||
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. | ||
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured | ||
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) | ||
* [Install AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) | ||
* [Install Python 3](https://www.python.org/downloads/) | ||
* [Grant Bedrock Model Access for Claude 3 Sonnet](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html) | ||
|
||
## Deployment Instructions | ||
|
||
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: | ||
|
||
``` | ||
git clone https://github.com/aws-samples/serverless-patterns | ||
``` | ||
|
||
2. Change directory to the pattern directory: | ||
|
||
``` | ||
cd bedrock-agents-lambda-cdk | ||
``` | ||
|
||
3. Create a virtual environment for Python: | ||
|
||
``` | ||
python3 -m venv .venv | ||
``` | ||
|
||
4. Activate the virtual environment: | ||
|
||
``` | ||
source .venv/bin/activate | ||
``` | ||
|
||
For a Windows platform, activate the virtualenv like this: | ||
|
||
``` | ||
.venv\Scripts\activate.bat | ||
``` | ||
|
||
5. Install the required Python dependencies: | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
6. Bootstrap the AWS environment, if you haven't already done so: | ||
|
||
``` | ||
cdk bootstrap | ||
``` | ||
|
||
7. Review the CloudFormation template AWS CDK generates for the stack: | ||
|
||
``` | ||
cdk synth | ||
``` | ||
|
||
8. Deploy the AWS resources: | ||
|
||
``` | ||
cdk deploy | ||
``` | ||
|
||
|
||
## How it works | ||
|
||
Bedrock agent processes user input through a foundation model to determine its next steps. It then decides whether to invoke an action or query a knowledge base. For performing an action it has action groups defined, in this example we are using a Lambda function which invokes an API. It either executes the action or it gathers more information from its knowledge base or the user. The agent generates an observation from these results, which it uses to generate final response or determine if it needs more user input, allowing for dynamic and context-aware interactions. | ||
|
||
## Testing | ||
|
||
To test the Bedrock agent, we will need to use the AWS console. The Lambda function will call the International Space Station API to fetch its location, so it can answer questions such as “What is the current location of ISS'”, “Where is ISS?'”, “Where is the internation space station now”, etc | ||
|
||
1. Navigate to the Amazon Bedrock Agents page on the AWS console | ||
2. Selected the deployed agent **ISSLocationAgent** | ||
3. If you don’t see a Test section on the right pane, click on **Test** | ||
4. Enter a message similar to “What is the current location of ISS'” and notice that the results are returned | ||
|
||
## Cleanup | ||
|
||
Run below script in the `bedrock-agents-lambda-cdk` directory to delete AWS resources created by this sample stack. | ||
|
||
``` | ||
cdk destroy | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
|
||
import aws_cdk as cdk | ||
|
||
from stacks.bedrock_agents_lambda_cdk_stack import BedrockAgentsLambdaCdkStack | ||
|
||
|
||
app = cdk.App() | ||
BedrockAgentsLambdaCdkStack(app, "BedrockAgentsLambdaCdkStack", | ||
# If you don't specify 'env', this stack will be environment-agnostic. | ||
# Account/Region-dependent features and context lookups will not work, | ||
# but a single synthesized template can be deployed anywhere. | ||
|
||
# Uncomment the next line to specialize this stack for the AWS Account | ||
# and Region that are implied by the current CLI configuration. | ||
|
||
#env=cdk.Environment(account=os.getenv('CDK_DEFAULT_ACCOUNT'), region=os.getenv('CDK_DEFAULT_REGION')), | ||
|
||
# For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html | ||
) | ||
|
||
app.synth() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"title": "Amazon Bedrock Agents with AWS Lambda", | ||
"description": "Create a agent with Amazon Bedrock with a Lambda function action group", | ||
"language": "Python", | ||
"level": "300", | ||
"framework": "CDK", | ||
"introBox": { | ||
"headline": "How it works", | ||
"text": [ | ||
"In this pattern, we show you how to use Amazon Bedrock Agents to call an API and get the results from it for a user. Bedrock Agents is helpful in various situations such as fetching real-time information from APIs, interacting with knowledge bases, and performing actions based on user inputs.", | ||
"It processes user input through a foundation model to determine its next steps. It then decides whether to invoke an action or query a knowledge base. For performing an action it has action groups defined, in this example we are using a Lambda function which invokes an API. It then either executes the action or it gathers more information from its knowledge base or the user. The agent generates an observation from these results, which it uses to update its understanding. This cycle continues until the agent can provide a final response or needs more user input, allowing for dynamic and context-aware interactions." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-agents-lambda-cdk", | ||
"templateURL": "serverless-patterns/bedrock-agents-lambda-cdk", | ||
"projectFolder": "bedrock-agents-lambda-cdk", | ||
"templateFile": "stacks/bedrock_agents_lambda_cdk_stack.py" | ||
} | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Bedrock Agents", | ||
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html" | ||
}, | ||
{ | ||
"text": "Creating Bedrock Agent Action Groups", | ||
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html" | ||
} | ||
] | ||
}, | ||
"deploy": { | ||
"text": [ | ||
"cdk deploy" | ||
] | ||
}, | ||
"testing": { | ||
"text": [ | ||
"See the GitHub repo for detailed testing instructions." | ||
] | ||
}, | ||
"cleanup": { | ||
"text": [ | ||
"Delete the stack: <code>cdk destroy</code>." | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Reethi Joseph", | ||
"image": "https://drive.google.com/file/d/1fthW0WuTMx8dKt3EPoh6NHFdj-jBrtvf/view", | ||
"bio": "Reethi is a Senior Cloud Engineer at AWS.", | ||
"linkedin": "reethi-joseph" | ||
} | ||
], | ||
"patternArch": { | ||
"icon1": { | ||
"x": 20, | ||
"y": 50, | ||
"service": "bedrock", | ||
"label": "Amazon Bedrock Agent" | ||
}, | ||
"icon2": { | ||
"x": 80, | ||
"y": 50, | ||
"service": "lambda", | ||
"label": "AWS Lambda" | ||
}, | ||
"line1": { | ||
"from": "icon1", | ||
"to": "icon2", | ||
"label": "" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"app": "python3 app.py", | ||
"watch": { | ||
"include": [ | ||
"**" | ||
], | ||
"exclude": [ | ||
"README.md", | ||
"cdk*.json", | ||
"requirements*.txt", | ||
"source.bat", | ||
"**/__init__.py", | ||
"**/__pycache__", | ||
"tests" | ||
] | ||
}, | ||
"context": { | ||
"@aws-cdk/aws-lambda:recognizeLayerVersion": true, | ||
"@aws-cdk/core:checkSecretUsage": true, | ||
"@aws-cdk/core:target-partitions": [ | ||
"aws", | ||
"aws-cn" | ||
], | ||
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, | ||
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, | ||
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, | ||
"@aws-cdk/aws-iam:minimizePolicies": true, | ||
"@aws-cdk/core:validateSnapshotRemovalPolicy": true, | ||
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, | ||
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, | ||
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, | ||
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true, | ||
"@aws-cdk/core:enablePartitionLiterals": true, | ||
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, | ||
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, | ||
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, | ||
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, | ||
"@aws-cdk/aws-route53-patters:useCertificate": true, | ||
"@aws-cdk/customresources:installLatestAwsSdkDefault": false, | ||
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true, | ||
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true, | ||
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true, | ||
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true, | ||
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true, | ||
"@aws-cdk/aws-redshift:columnId": true, | ||
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true, | ||
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true, | ||
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true, | ||
"@aws-cdk/aws-kms:aliasNameRef": true, | ||
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true, | ||
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true, | ||
"@aws-cdk/aws-efs:denyAnonymousAccess": true, | ||
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true, | ||
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true, | ||
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true, | ||
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true, | ||
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true, | ||
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true, | ||
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true, | ||
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true, | ||
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true, | ||
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true, | ||
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true, | ||
"@aws-cdk/aws-eks:nodegroupNameAttribute": true, | ||
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true, | ||
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true, | ||
"@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"title": "Amazon Bedrock Agents with AWS Lambda", | ||
"description": "Create a agent with Amazon Bedrock with a Lambda function action group", | ||
"language": "Python", | ||
"level": "300", | ||
"framework": "CDK", | ||
"introBox": { | ||
"headline": "How it works", | ||
"text": [ | ||
"In this pattern, we show you how to use Amazon Bedrock Agents to call an API and get the results from it for a user. Bedrock Agents is helpful in various situations such as fetching real-time information from APIs, interacting with knowledge bases, and performing actions based on user inputs.", | ||
"It processes user input through a foundation model to determine its next steps. It then decides whether to invoke an action or query a knowledge base. For performing an action it has action groups defined, in this example we are using a Lambda function which invokes an API. It then either executes the action or it gathers more information from its knowledge base or the user. The agent generates an observation from these results, which it uses to update its understanding. This cycle continues until the agent can provide a final response or needs more user input, allowing for dynamic and context-aware interactions." | ||
] | ||
}, | ||
"gitHub": { | ||
"template": { | ||
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/bedrock-agents-lambda-cdk", | ||
"templateURL": "serverless-patterns/bedrock-agents-lambda-cdk", | ||
"projectFolder": "bedrock-agents-lambda-cdk", | ||
"templateFile": "stacks/bedrock_agents_lambda_cdk_stack.py" | ||
} | ||
}, | ||
"resources": { | ||
"bullets": [ | ||
{ | ||
"text": "Bedrock Agents", | ||
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html" | ||
}, | ||
{ | ||
"text": "Creating Bedrock Agent Action Groups", | ||
"link": "https://docs.aws.amazon.com/bedrock/latest/userguide/agents-action-create.html" | ||
} | ||
] | ||
}, | ||
"deploy": { | ||
"text": [ | ||
"cdk deploy" | ||
] | ||
}, | ||
"testing": { | ||
"text": [ | ||
"See the GitHub repo for detailed testing instructions." | ||
] | ||
}, | ||
"cleanup": { | ||
"text": [ | ||
"Delete the stack: <code>cdk destroy</code>." | ||
] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Reethi Joseph", | ||
"image": "https://drive.google.com/file/d/1fthW0WuTMx8dKt3EPoh6NHFdj-jBrtvf/view", | ||
"bio": "Reethi is a Senior Cloud Engineer at AWS.", | ||
"linkedin": "https://www.linkedin.com/in/reethi-joseph/" | ||
} | ||
] | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import json | ||
import urllib3 | ||
from urllib3.util import Retry | ||
|
||
|
||
def lambda_handler(event, context): | ||
http = urllib3.PoolManager( | ||
retries=Retry(3, backoff_factor=0.5) | ||
) | ||
response = http.request( | ||
'GET', | ||
'http://api.open-notify.org/iss-now.json', | ||
timeout=10.0 | ||
) | ||
iss_data = json.loads(response.data.decode('utf-8')) | ||
responseBody = { | ||
"TEXT": { | ||
"body": f"The ISS is currently at latitude {iss_data["iss_position"]['latitude']} and longitude {iss_data["iss_position"]['longitude']}" | ||
} | ||
} | ||
|
||
action_response = { | ||
'actionGroup': event['actionGroup'], | ||
'function': event['function'], | ||
'functionResponse': { | ||
'responseBody': responseBody | ||
} | ||
} | ||
function_response = {'response': action_response, 'messageVersion': event['messageVersion']} | ||
print("Response: {}".format(function_response)) | ||
|
||
return function_response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
aws-cdk-lib==2.151.0 | ||
constructs>=10.0.0,<11.0.0 |
Empty file.
Oops, something went wrong.