Build a Serverless GenAI image creator - Amazon Bedrock model deployed with AWS CDK
The goal of this post is to provide a solution to quickly deploy a scalable Serverless image creator Gen AI application using AWS CDK built with Amazon Bedrock, Streamlit, Elastic Container Service (ECS), API Gateway, and AWS Lambda
Published Jan 30, 2024
Generative AI is a type of AI that can create new content and ideas, including conversations, stories, images, videos, and music. Like all AI, generative AI is powered by ML models—very large models that are pre-trained on vast corpora of data and commonly referred to as foundation models (FMs). Generative AI has the potential to disrupt many industries by revolutionizing the way content is created and consumed. Original content production, code generation, customer service enhancement, and document summarization are typical use cases of generative AI.
Amazon Bedrock is a fully managed service that provides access to FMs from third-party providers and Amazon; available via an API. With Bedrock, you can choose from a variety of models to find the one that’s best suited for your use case.
In this post, I will show how to build a scalable solution to invoke the "amazon.titan-image-generator-v1" image generation Bedrock model using a Streamlit UI deployed using Serverless services like API Gateway, Lambda with AWS Cloud Development Kit (AWS CDK). The AWS CDK is an open-source software development framework to define your cloud application resources using familiar programming languages like Python.
The web application is built on Streamlit, an open-source Python library that makes it easy to create and share beautiful, custom web apps for ML and data science. The web application is hosted using Amazon Elastic Container Service (Amazon ECS) with AWS Fargate and it is accessed via an Application Load Balancer. Fargate is a technology that you can use with Amazon ECS to run containers without having to manage servers or clusters or virtual machines. The Bedrock model endpoint is invoked from an AWS Lambda function. The web application interacts with the models via Amazon API Gateway and AWS Lambda function as shown in the following diagram.
API Gateway provides the web application and other clients a standard RESTful interface, while shielding the Lambda function that interfaces with the model. This simplifies the client application code that consumes the models. The API Gateway endpoints are publicly accessible in this example, allowing for the possibility to extend this architecture to implement different API access controls and integrate with other applications.
Here are the high level steps to deploy this solution:
- Clone and set up the Gen AI application.
- Deploy the AWS CDK app.
- Invoke the image generation Bedrock model.
You must have the following prerequisites:
- An AWS account
- The AWS CLI v2
- Python 3.6 or later
- node.js 14.x or later
- The AWS CDK v2
- Docker v20.10 or later
You can deploy the infrastructure in this tutorial from your local computer or you can use AWS Cloud9 as your deployment workstation. AWS Cloud9 comes pre-loaded with AWS CLI, AWS CDK and Docker. If you opt for AWS Cloud9, create the environment from the AWS console.
If you don’t already have the AWS CLI on your local machine, refer to Installing or updating the latest version of the AWS CLI and Configuring the AWS CLI.
Install the AWS CDK Toolkit globally using the following node package manager command:
npm install -g aws-cdk-lib@latest
Run the following command to verify the correct installation and print the version number of the AWS CDK:
Make sure you have Docker installed. Issue the following command to verify the version:
On your local machine, clone the AWS CDK application with the following command:
git clone https://github.com/suryakva/genai-titan-image-generator.git
Navigate to the project folder:
stack folder contains the code for each stack in the AWS CDK application. The
code folder contains the code for the AWS Lambda function. The repository also contains the web application located under the folder
cdk.json file tells the AWS CDK Toolkit how to run your application.
This application was tested in the
us-east-1 Region but it should work in any Region that has the required services.
This project is set up like a standard Python project. Create a Python virtual environment using the following command:
python3 -m venv .venv
Use the following command to activate the virtual environment:
If you’re on a Windows platform, activate the virtual environment as follows:
After the virtual environment is activated, upgrade pip to the latest version:
python3 -m pip install --upgrade pip
Install the required dependencies:
pip install -r requirements.txt
Before you deploy any AWS CDK application, you need to bootstrap a space in your account and the Region you’re deploying into. To bootstrap in your default Region, issue the following command:
If you want to deploy into a specific account and Region, issue the following command:
cdk bootstrap aws://ACCOUNT-NUMBER/REGION
For more information about this setup, visit Getting started with the AWS CDK.
You can list stacks in your CDK application with the following command:
You should get the following output:
The AWS CDK application will be deployed to the default Region based on your workstation configuration. If you want to force the deployment in a specific Region, set your
AWS_DEFAULT_REGION environment variable accordingly.
At this point, you can deploy the AWS CDK application. First you launch the VPC network stack:
cdk deploy VpcNetworkStack
If you are prompted, enter
y to proceed with the deployment. You should see a list of AWS resources that are being provisioned in the stack.
Then you launch the web application stack:
cdk deploy ApplicationStack
After analyzing the stack, AWS CDK will display the resources listed in the stack. Enter y to proceed with the deployment.
Note down the
WebApplicationServiceURL from the output as you will need it later. You can also retrieve it later in the CloudFormation console, under the
ApplicationStack stack outputs.
- Access the web application using the
WebApplicationServiceURLfrom the output of the
ApplicationStackin your browser.
- Enter a prompt in the prompt text field. Ex: "Waterfall on Mars"
- Click the Generate button.
The application will make a call to the Bedrock endpoint. It takes a few seconds. An image with the details in your text prompt description will be displayed.
To avoid unnecessary costs, clean up all the infrastructure created with the following command:
cdk destroy --all
y at the prompt. Check if all resources are deleted on the console. Also delete the assets S3 buckets created by the AWS CDK on the Amazon S3 console as well as the assets repositories on Amazon ECR.
As demonstrated in this post, you can use the AWS CDK to deploy Serverless architectures to invoke Bedrock models. We saw an image generation example using a user interface powered by Streamlit, Lambda, and API Gateway.
This sample code is made available under a modified MIT license. See the LICENSE file for more information.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.