How to integrate AWS Lambda with Amazon Bedrock using Typescript

How to integrate AWS Lambda with Amazon Bedrock using Typescript

What is Amazon Bedrock?

Amazon Bedrock is a fully managed service that makes FMs from leading AI startups and Amazon available via an API, so you can choose from a wide range of FMs to find the model that is best suited for your use case. With Bedrock’s serverless experience, you can get started quickly, privately customize FMs with your own data, and easily integrate and deploy them into your applications using the AWS tools without having to manage any infrastructure.

How to enable the models?

The first thing you need to do in case you don't have Amazon Bedrock enabled is to go to its page and enable the available models. It will be activated instantaneously. Note that at the time of publishing this post, only a few regions support Amazon Bedrock.

If you play with it in the AWS console, it's possible to get the request format to call it programmatically.

And here is the API request:

{
  "modelId": "ai21.j2-ultra-v1",
  "contentType": "application/json",
  "accept": "*/*",
  "body": "{\"prompt\":\"give me a recipe for a bolognese lasagna\",\"maxTokens\":200,\"temperature\":0.7,\"topP\":1,\"stopSequences\":[],\"countPenalty\":{\"scale\":0},\"presencePenalty\":{\"scale\":0},\"frequencyPenalty\":{\"scale\":0}}"
}

Writing the lambda code

Now we know how the API works, we can go and implement it using typescript.

The first thing to do is to install the bedrock-runtime client from the Javascript V3 SDK.

npm install @aws-sdk/client-bedrock-runtime

Optionally, we can also install the lambda types.

npm install --save-dev @types/aws-lambda

The lambda needs some of the following permissions to be able to call Amazon Bedrock API:

{
  "Sid": "AllowBedRockPermissions",
  "Action": [
    "bedrock:InvokeModelWithResponseStream",
    "bedrock:InvokeModel",
    "bedrock:InvokeAgent"
  ],
  "Effect": "Allow",
  "Resource": [
    "arn:aws:bedrock:*:*:provisioned-model/*",
    "arn:aws:bedrock:*:*:foundation-model/*",
    "arn:aws:bedrock:*:*:agent-alias/*/*"
  ]
}

Code example

Now that the lambda is capable of calling the Berock models, below there is an example of code on how to interact with the text model AI21 Labs - Jurassic-2 Ultra.

If you have set your lambda to have a function URL, you can call it like this:

curl -X POST -H "Content-Type: application/json" \
--request POST \
--data '{"prompt":"how can I connect a lambda to a dynamodb stream?"}' \
https://your-lambda-url.lambda-url.us-east-1.on.aws