Creating a Generative AI Bedrock Lambda using Claude 3 Opus with CDK

Collin Smith
6 min readMay 7, 2024

--

AWS Bedrock with Claude 3 Opus

Introduction

In this article, we will discuss Generative AI and then proceed with showing how to deploy an AWS Lambda which allows the user to submit a prompt or question to a model to retrieve a response.

Generative AI

Generative Artificial Intelligence is “artificial intelligence capable of generating text, images, videos, or other data using generative models, often in response to prompts. Generative AI models learn the patterns and structure of their input training data and then generate new data that has similar characteristics.”

Recently large language models(LLMs) have enabled the recent AI boom which many players presenting their models for use with a wide variety of applications.

AWS Bedrock

AWS Bedrock is a service offered by AWS to allow organizations to build and scale generative AI applications with foundation models. This is a fully managed service that offers a choice of high-performing foundational models (FMs) from leading AI companies such as AI21 Labs, Anthropic, and others.

This allows you to easily experiment with a variety of models and evaluate their performance across a variety of metrics such as use case suitability, cost, time response and other metrics.

Anthropic Model

For this article we will focus on the use of the Claude 3 Opus Anthropic model. Anthropic is one of the leaders in the generative AI space and on AWS currently offers the Claude 3 Family of models.

Claude 3 Opus — Anthropic’s most intelligent , with best-in-market performance on highly complex tasks

Claude 3 Sonnet — offers a balance between intelligence and speed

Claude 3 Haiku — Anthropic’s fastest, most compact model for near-instant responsiveness, best choice for mimicking human interactions

We will now walk through how to create an AWS Lambda in Python using CDK for infrastructure as code that utilizes the Claude 3 Opus model:

Claude 3 Demo

Architecture Diagram of this Bedrock Claude 3 Opus implementation

Prerequisites

Implementation Steps

  1. Validate the Prerequisites
python --version
aws --version
cdk --version
node -v

2. Create the CDK Project

cd c:\projects
mkdir python-bedrock-lambda
cd python-bedrock-lambda

cdk init app --language python
source .venv/bin/activate
.venv\Scripts\activate.bat
python -m pip install --upgrade pip
python -m pip install -r requirements.txt

2. Open the project up in Visual Studio Code

All of the code is present in the following https://github.com/collin-smith/python-bedrock-lambda

Key files to examine are

3. Verify that the Bedrock AI model is set up properly

Ensure that Claude 3 Opus is enabled by going to the AWS Bedrock Console under Model Access

When selecting a model, you should also review the API request model to ensure that you are sending the appropriate information

4. Deploy the Lambda code

Ensure you have your console set up with the proper credentials for your AWS account and region set up before attempting to deploy

cdk bootstrap
cdk synth
cdk deploy

5. Test out the call

Here we test out the model by asking it “What is the best way to experiment with Generative AI?

The model actually answers back with a Yes along with a detailed response. The Claude 3 Opus response to What is the best way to experiment with Generative AI? was the following

There are several ways to experiment with Generative AI, depending on your technical background and specific interests.
Here are some popular approaches:
1. Use pre-trained models: Platforms like Hugging Face, OpenAI, and Runway ML provide access to pre-trained models that you can use to generate text, images, and audio without the need for extensive coding knowledge.
2. Learn a deep learning framework: If you have some programming experience, you can learn a deep learning framework like TensorFlow or PyTorch and use it to build and train your own generative models.
3. Explore no-code or low-code tools: Tools like Runway ML, Artbreeder, and Deep Dream Generator allow you to create generative art and experiment with AI models without coding.
4. Enroll in online courses: Platforms like Coursera, edX, and Udemy offer courses on deep learning, generative models, and creative AI applications.
5. Join online communities: Engage with communities on Reddit, Discord, or Twitter dedicated to generative AI, where you can learn from others, share your work, and collaborate on projects.
6. Participate in hackathons and competitions: Look for AI-themed hackathons, competitions, or challenges that encourage participants to create generative models for various applications.
7. Experiment with creative coding: Use creative coding tools like Processing, p5.js, or OpenFrameworks to combine generative AI with visual programming and create interactive artworks.
The best approach for you will depend on your goals, technical skills, and the amount of time you can invest in learning and experimentation

Also, note the number of input_tokens and input_tokens as this has a direct impact on the cost of running the query.

If we look at the Amazon Bedrock Pricing, we might infer that the cost of this query to be:

input_tokens = 20

output_tokens = 354

For Claude 3 Opus we can see that the cost for On-Demand and Batch:pricing will look like something the following?

Claude 3 Opus cost = input_tokens x $0.015 + output_tokens x $0.075

=((20 x $0.015) + (354 x $0.075))/1000 (as price is per 1,000 tokens)

= ($0.30 + $26.55) / 1000

= $0.027 (or about 2.6 cents)

Other models are cheaper and present different functionality and it is open for you to select the right model that offers the right price/performance.

Cleaning up

cdk destroy

Conclusion

This has shown how to make a call to a Bedrock Gen AI model endpoint but you can do much more with Bedrock. You should experiment with the many different AI models that AWS Bedrock has to offer. This will depend on your use case. There are plenty of models and options available. This article can help you get started on your journey.

--

--

Collin Smith

AWS Ambassador/Solutions Architect/Ex-French Foreign Legion