logo
Menu

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

Surya Vangala
Amazon Employee
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.

Solution Overview

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.
Architecture:
Bedrock Serverless Architecture
Bedrock Serverless Architecture
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:
  1. Install the AWS Command Line Interface (AWS CLI) and AWS CDK v2 on your local machine.
  2. Clone and set up the Gen AI application.
  3. Deploy the AWS CDK app.
  4. Invoke the image generation Bedrock model.

Prerequisites

You must have the following prerequisites:
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.

Install the AWS CLI and AWS CDK

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:
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:

Clone and set up the Gen AI application

On your local machine, clone the AWS CDK application with the following command:
Navigate to the project folder:
The 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 web-app.
The 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.

Setup a virtual environment

This project is set up like a standard Python project. Create a Python virtual environment using the following command:
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:
Install the required dependencies:
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:
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:

Deploy the AWS CDK app

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:
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:
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.

Invoke the image generation Bedrock model

  1. Access the web application using the WebApplicationServiceURL from the output of the ApplicationStack in your browser.
  2. Enter a prompt in the prompt text field. Ex: "Waterfall on Mars"
  3. 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.
Generated Image
Generated Image

Code

Clean up

To avoid unnecessary costs, clean up all the infrastructure created with the following command:
Enter 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.

Conclusion

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.

License summary

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.

Comments