Invoke Amazon Bedrock models with Lambda functions

Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models (FMs) from leading AI companies like AI21 Labs, Anthropic, Cohere, Meta, Stability AI, and Amazon via a single API, along with a broad set of capabilities you need to build generative AI applications with security, privacy, and responsible AI.

Since Amazon Bedrock is serverless, you don’t have to manage any infrastructure, and you can securely integrate and deploy generative AI capabilities into your applications.

What can I use Amazon Bedrock for?

1. Building chatbots: You can create and customize chatbots with Bedrock, then add them to your website or a stand-alone section.

2. Text and image generation: The bread and butter of generative AI models. You can generate a wide range of original written or image-based content from language prompts.

3. Searching through data: There is a search function that allows you to search through information to answer questions from huge amounts of data.

4. Text summarization: Text summarization is also a build option. This feature allows you to get a quick and easy summary of any text based content, such as articles, blog posts, books, or documentation.

There’s a wide range of supported FM providers to choose from such as:

  • Amazon
  • Anthropic
  • AI21 Labs
  • Cohere
  • Stability AI
Invoke Amazon Bedrock models with Lambda functions

Getting access to these models

These models can be accessed from the model access feature in Amazon bedrock. Once you have access to the models, create a new Lambda function in your AWS Account through the console, or however you usually provision infrastructure, and write out your code to access the bedrock service.

Invoke Amazon Bedrock models with Lambda functions

What is AWS Lambda?

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.

Now let’s create a lambda function on AWS.

Invoke Amazon Bedrock models with Lambda functions

Click on create function and follow the steps on the page to create the function.

Use case:

Let us consider a simple use case of text generation. We will ask the model to generate an article on a topic we want. You can choose the topic of your wish.

In the below code prompt is what a user can enter or can be considered as an input to the model. Let’s use Claude model provided by Anthropic.

You can use model of your choice but keep in the mind each model follows different parameters while calling an API on bedrock service. API endpoint can be found on AWS bedrock page.

import boto3 and then Initialize the bedrock runtime. In the example above, we are assigning it to the bedrock_runtime variable. Then, in the main handler function named lambda_handler, we invoke the bedrock api invoke_model with the parameters required for the model

import boto3 

import json 


def lambda_handler(event, context): 


    prompt = "Write an article on Generative AI" 

    # Bedrock Runtime client used to invoke and question the models 

    bedrock_runtime = boto3.client( 




    body = json.dumps({ 

        "prompt": "\n\nHuman:" + prompt + " " + "\n\nAssistant:", 

        "max_tokens_to_sample": 500, 

        "temperature": 0.9, 

        "top_k": 250, 

        "top_p": 1.0, 

        "stop_sequences": ["\n\nHuman:"], 

        "anthropic_version": "bedrock-2023-05-31" }) 


    response = bedrock_runtime.invoke_model(body=body, modelId="anthropic.claude-v2") 

    response_body = json.loads(response.get("body").read()) 

    return response_body.get("completion") 

This code will generate an article on Generative AI. Once the lambda function is created copy the above in the code section of the lambda function. Above code can be written in other languages as well like Node.js etc but I prefer Python. Keep in the mind that there are few APIs and functions are only supported by node.js and not Python.

Above code generates the article but it may take a few seconds to generate the whole article. This may lead to poor user engagement. Do you know how to avoid the same?


You can also access the bedrock foundation models outside the AWS. Do you know how? I will leave that to you to explore.

Adding an Endpoint to lambda function 

Next step is to call the lambda function in our application. We can do this by creating an API gateway for the lambda function. You can create a web API with an HTTP endpoint for your Lambda function by using Amazon API Gateway. API Gateway provides tools for creating and documenting web APIs that route HTTP requests to Lambda functions. You can secure access to your API with authentication and authorization controls. Your APIs can serve traffic over the internet or can be accessible only within your VPC.

When a prompt is received, the API Gateway triggers an AWS Lambda function. This Lambda function is responsible for invoking a model (anthropic.claude-v2 for now, but can be extended to include others) hosted on Amazon Bedrock.

Invoke Amazon Bedrock models with Lambda functions

To create the resource

  • In the API Gateway console, on the Resources page for your API, choose Create Resource.
  • In Resource details, for Resource name enter ‘test’.
  • Choose Create Resource.
Invoke Amazon Bedrock models with Lambda functions

To create the POST method

  • On the Resources page for your API, then, in the Methods pane, choose Create Method.
  • For Method type, choose POST.
  • For Integration type, leave Lambda function selected.
  • For Lambda function, choose the Amazon Resource Name (ARN) for your function (LambdaFunctionOverHttps).
  • Choose Create method.

To deploy the API

  • Open the APIs page of the API Gateway Console. On the Resources page for your API choose Deploy API.
  • For Stage, choose *New stage*, then for Stage name, enter the test.
  • Choose Deploy.
  • In the Stage details pane, copy the Invoke URL. You will use this in the next step to invoke your function using an HTTP request.

AWS Lambda can easily work with Amazon Bedrock and a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers.

To use Amazon Bedrock with AWS Lambda, the lambda needs to be updated with the latest AWS SDK.


What’s your Reaction?

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *