Skip to content

Latest commit

 

History

History

apigw-lambda-bedrock-js-sam

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.

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

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 for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

Prerequisite

Amazon Bedrock users need to request access to models before they are available for use. If you want to add additional models for text, chat, and image generation, you need to request access to models in Amazon Bedrock. Please refer to the link below for instruction: Model access.

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 apigw-lambda-bedrock-js-sam
  3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file:

    sam deploy --guided
  4. During the prompts:

    • Enter a stack name
    • Enter the desired AWS Region
    • Allow SAM CLI to create IAM roles with the required permissions.

    Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

    When asked "BedrockLambdaFunction has no authentication. Is this okay? [y/N]", answer explicitly with y for the purposes of this sample application. As a result, anyone will be able to call this example REST API without any form of authentication.

    For production applications, you should enable authentication for the API Gateway using one of several available options and follow the API Gateway security best practices.

  5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for next step as well as testing.

  6. Run the create_lambda_layer.sh. You may have to change the file permission to make it executable. This will create the lambda layer with the latest AWS SDK for JS for performing Bedrock API calls.

    ./create_lambda_layer.sh
  7. Provide a name for the Lambda layer. Such as:

    Enter the name of the Layer: awssdk-js-lambda-layer

    It will show output like below:

    Publishing the layer. Please wait ...
    {
     "Content": {
       .....
       .....
     },
     "LayerArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:awssdk-js-lambda-layer",
     "LayerVersionArn": "arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:awssdk-js-lambda-layer:1",
     "Description": "",
     "CreatedDate": "YYYY-MM-DDT10:47:36.983+0000",
     "Version": 1
    }
  8. You may have to press q to come out of the output. Copy the value of LayerVersionArn from the above output and provide it into the next step. Such as:

    Enter the LayerVersionArn from the above command: arn:aws:lambda:us-east-1:xxxxxxxxxxxx:layer:awssdk-js-lambda-layer:1
  9. Please copy the value of BedrockLambdaFunction from the sam deploy --guided output and provide that as response to next question. Such as:

    Enter the Lambda function name from the SAM deploy output: your-stack-name-BedrockLambdaFunctionXx-xxxxxxxxxxxx

    The script will now run aws cli command to add the newly created layer to the Lambda function. It will show output as below: It will show output like below:

    Adding the new layer to your Lambda function's configuration. Please wait ...
    {
       "FunctionName": "your-stack-name-BedrockLambdaFunctionXx-xxxxxxxxxxxx",
       ......
       ......
       "State": "Active",
       "LastUpdateStatus": "InProgress",
       "LastUpdateStatusReason": "The function is being created.",
       "LastUpdateStatusReasonCode": "Creating",
       "PackageType": "Zip",
       "Architectures": [
          "arm64"
       ],
       "EphemeralStorage": {
          "Size": 512
       }
    }      
  10. You may have to press q to come out of the output. The setup is ready for testing.

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: End to End Architecture

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.

  3. Amazon Bedrock: Based on the given prompt, using Anthropic Claude-v2 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.

Testing

Test the deployed content generation API by providing a prompt. You can use curl to send a HTTP POST request to the API. Make sure to replace BedrockRestApi with the one from your sam deploy --guided output:

curl -d '{"prompt": "Please write 5 lines on Solar Systems"}' -H 'Content-Type: application/json'  <BedrockRestApi>

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}"

Cleanup

  1. To delete the resources deployed to your AWS account via AWS SAM, run the following command:
sam delete
  1. 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:
./delete_lambda_layer.sh

Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0