Skip to content

Add Bedrock Agent example #357

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 2 commits into from
Jan 26, 2024

Conversation

moritalous
Copy link
Contributor

#317

Add Bedrock Agent example

Properties:
PackageType: Image
MemorySize: 256
Policies: arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you can use policy name:

      Policies: AmazonS3ReadOnlyAccess

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Metadata:
Dockerfile: Dockerfile
DockerContext: ./app
DockerTag: python3.12-v1
Copy link
Contributor

Choose a reason for hiding this comment

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

You need to configure resource policy to allow Bedrock service to invoke this lambda function. And output the function arn will be useful.

  BedrockAgentPermission:
    Type: AWS::Lambda::Permission
    Properties:
      FunctionName: !Ref BedrockAgentFastAPIFunction
      Action: lambda:InvokeFunction
      Principal: bedrock.amazonaws.com
      SourceAccount: !Ref 'AWS::AccountId'
      SourceArn: !Sub arn:aws:bedrock:${AWS::Region}:${AWS::AccountId}:agent/*

Outputs:
  BedrockAgentFastAPIFunction:
    Description: "BedrockAgentFastAPIFunction Lambda Function ARN"
    Value: !GetAtt BedrockAgentFastAPIFunction.Arn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added your reccomend code

Create a bedrock agent. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-create.html
))

## Test
Copy link
Contributor

Choose a reason for hiding this comment

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

Before testing, we should show how to get the openapi spec from the FastAPI.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

add "Generate OpenAPI schema" section


Test your agent. (see [reference](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-test.html))

## Run the docker locally
Copy link
Contributor

Choose a reason for hiding this comment

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

This section could be removed and replaced with how to use sam local invoke to test locally the agent function with event payloads in the events directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove "Run the docker locally" section.
add "Test locally" section.

Type: AWS::Serverless::Function
Properties:
PackageType: Image
MemorySize: 256
Copy link
Contributor

Choose a reason for hiding this comment

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

256 is a bit slow. Increase it to 1024.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,8 @@
FROM public.ecr.aws/docker/library/python:3.12.0-slim
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter
ENV PORT=8000
Copy link
Contributor

Choose a reason for hiding this comment

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

Add AWS_LWA_READINESS_CHECK_PROTOCOL=tcp to use TCP connect for healthcheck

ENV PORT=8000 AWS_LWA_READINESS_CHECK_PROTOCOL=tcp

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

* Update Dockerfile
  - add `AWS_LWA_READINESS_CHECK_PROTOCOL` env

* Update template.yaml
  - change `MemorySize` 256 to 1024
  - fix Policies define `arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess` to `AmazonS3ReadOnlyAccess`
  - add BedrockAgentPermission
  - add Output

* Update README.md
  - update Dockerfile
  - add "Generate OpenAPI schema", "Create an agent.", "Test locally" section.
  - remove "Run the docker locally" section.
@bnusunny bnusunny merged commit b4718bc into awslabs:main Jan 26, 2024
@bnusunny
Copy link
Contributor

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants