AWS Bedrock Generate Movie Poster Design

Prakash AgrawalPrakash Agrawal
4 min read

A. Get Access to Foundation models in AWS

Search for Bedrock in AWS Console, on the left side expand the bar and look for Model Access. Select the models as shown below to request model access.

You can provide use case details for Anthropic base models like this:

In the last page, it will show Review and Submit option. Review the base models that were selected and click on Submit.

B. Create an AWS S3 bucket with name bedrockposterdesignstore to store the design posters

  1. Search for S3 in AWS console

  1. Click on Create Bucket on the next page

  1. Provide a name for the S3 bucket

C. Create an AWS Lambda function bedrockPosterDesignFunction

  1. Search for Lambda in the console and then select button Create a Function

  1. Give the name to the Lambda function and select Runtime as Python

  1. In the Code section of Lambda created, print the boto 3 version and ensure that boto 3 version is above 1.28.63, then it will work with Bedrock.

  1. We need to increase timeout limit for the Lambda function. For that go to the Configuration section of Lambda and click on Edit.

  1. Increase Timeout from 3 seconds to 1 minute and 3 seconds

  1. Attach a Policy to the default role created for the Lambda function

Click on Permissions section of Lambda, and click the link for the role

  1. In the Policies select AdministratorAccess for now and attach the policy. We will change the policies attached to the role later in this tutorial.

  1. In Lambda code, create variables for client connection with S3 and Bedrock

  1. Store the event passed from API in a variable like prompt1

10. Call AWS Bedrock Stability Diffusion model to generate image for a prompt, the method parameter details can be found in boto3 documentation.

Convert the body returned in the response object to bytes

Get the base64 version from the bytes object

We can then get the image object by decoding the base64 version, this image object will be stored in S3 bucket in the next step.

  1. Store the image as an object in S3 bucket.

Use client_s3.put_object to put the image obtained in previous steps in S3. We mainly need to provide three parameters in put_object, those are Bucket, Body and Key.

12. Then generate a Pre-Signed URL for the image stored

By calling the method generate_presigned_url and passing the parameters like method name, Params like Bucket, Key and setting ExpiresIn value.

Next print the generated presigned url and also return it in the body so that it is sent back in the response.

  1. Set up API Gateway and send the URL as a response to AWS API Gateway

Search for API Gateway in AWS Console

On the API Gateway page, click on the link Create API

Click on the Build for Rest API

Give the name of API as GenerateImageAPI and click on Create API

  1. Create a resource for GenerateImageAPI

Create a GET method for this resource, then Edit the method and add URL query string parameters as prompt which will be passed to Lambda.

In the Integration Request settings add Integration Type as Lambda and select the Lambda function that was created from the dropdown.

Add the Mapping Template details for the prompt:

  1. Deploy and Test REST API

On the right side, select the button Deploy API and add the Stage details:

Once the API is deployed then go to the Test section of the GET method and pass the prompt as shown : prompt=image of lion and click on Test

As you click on Test, status code 200 and body with S3 signed url of the image is returned.

In this way we can use Rest API to be called from PostMan and test multiple prompts.

This completes this tutorial on generating the image using AWS Bedrock.

About me: I am an independent technical writer and if you are an organization that want to hire me then I can be contacted at techonlinewriter@gmail.com.

0
Subscribe to my newsletter

Read articles from Prakash Agrawal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Prakash Agrawal
Prakash Agrawal