logo
Menu
Serverless Image Generation Application Using Generative AI on AWS

Serverless Image Generation Application Using Generative AI on AWS

Use Amazon Bedrock to build an image generation solution in Go and deploy it using AWS CDK.

Abhishek Gupta
Amazon Employee
Published Nov 1, 2023
Last Modified Apr 13, 2024
Whether it's crafting personalized content or tailoring images to user preferences, the ability to generate visual assets based on a description is quite powerful. But text-to-image conversion typically involves deploying an end-to-end machine learning solution, which is quite resource intensive. What if this capability was an API call away, thereby making the process simpler and more accessible for developers?
This tutorial will walk you through how to use AWS CDK to deploy a Serverless image generation application implemented using AWS Lambda and Amazon Bedrock, which is a fully managed service that makes base models from Amazon and third-party model providers (such as Anthropic, Cohere, and more) accessible through an API. Developers can leverage leading foundation models through a single API, while maintaining the flexibility to adopt new models in the future.
The solution is deployed as a static website hosted on Amazon S3 accessible via an Amazon CloudFront domain. Users can enter the image description which will be passed on to a Lambda function (via Amazon API Gateway) which in turn will invoke Stable Diffusion model on Amazon Bedrock to generate the image.
Architecture
The entire solution is built using Go - this includes the Lambda function (using aws-lambda-go library) as well as the complete solution deployment using AWS CDK.
The code is available on GitHub.
About
βœ… AWS Level200 - Intermediate
⏱ Time to complete30 minutes
πŸ’° Cost to completeFree when using the AWS Free Tier
πŸ’» Code SampleCode sample used in tutorial on GitHub
πŸ“’ FeedbackAny feedback, issues, or just a πŸ‘ / πŸ‘Ž ?
⏰ Last Updated2023-11-01

Prerequisites

Before starting this tutorial, you will need the following:
Clone this GitHub repository and change to the right directory:
1
2
3
git clone https://github.com/build-on-aws/amazon-bedrock-lambda-image-generation-golang

cd amazon-bedrock-lambda-image-generation-golang

Deploy the Solution Using AWS CDK

To start the deployment, simply invoke cdk deploy.
1
2
3
4
cd cdk

export DOCKER_DEFAULT_PLATFORM=linux/amd64
cdk deploy
You will see a list of resources that will be created and will need to provide your confirmation to proceed (output shortened for brevity).
1
2
3
4
5
6
7
8
9
10
11
12
Bundling asset BedrockLambdaImgeGenWebsiteStack/bedrock-imagegen-s3/Code/Stage...

✨ Synthesis time: 7.84s

//.... omitted

This deployment will make potentially sensitive changes according to your current security approval level (--require-approval broadening).
Please confirm you intend to make the following modifications:

//.... omitted

Do you wish to deploy these changes (y/n)? y
This will start creating the AWS resources required for the application.
If you want to see the AWS CloudFormation template which will be used behind the scenes, run cdk synth and check the cdk.out folder.
You can keep track of the progress in the terminal or navigate to AWS console: CloudFormation > Stacks > BedrockLambdaImgeGenWebsiteStack
CloudFormation Stack
Once all the resources are created, you can try out the application. You should have:
  • The image generation Lambda function and API Gateway.
  • An S3 bucket to host the website HTML page.
  • CloudFront distribution.
  • And a few other components (like IAM roles, permissions, S3 Bucket policy etc.)
The deployment can take a bit of time since creating the CloudFront distribution is a time-consuming process. Once complete, you should get a confirmation along with the values for the S3 bucket name, API Gateway URL, and the CloudFront domain name.
CloudFormation Outputs

Update the HTML Page and Copy It to S3 Bucket

Open the index.html file in the GitHub repo, and locate the following text ENTER_API_GATEWAY_URL. Replace this with the API Gateway URL that you received as the CDK deployment output above.
To copy the file to S3, I used the AWS CLI:
1
aws s3 cp index.html s3://<name of the S3 bucket from CDK output>
Verify that the file was uploaded:
1
aws s3 ls s3://<name of the S3 bucket from CDK output>
Now you are ready to access the website!

Verify the Solution

Enter the CloudFront domain name in your web browser to navigate to the website. You should see the website with a pre-populated description that can be used as a prompt.
Click Generate Image to start the process. After a few seconds, you should see the generated image.
Website

Modify the Model Parameters

The Stability Diffusion model allows us to refine the generation parameters as per our requirements.
The Stability.ai Diffusion models support the following controls:
  • Prompt strength (cfg_scale) controls the image's fidelity to the prompt, with lower values increasing randomness.
  • Generation step (steps) determines the accuracy of the result, with more steps producing more precise images.
  • Seed (seed) sets the initial noise level, allowing for reproducible results when using the same seed and settings.
Click Show Configuration to edit these.
Website configuration
Max values for cfg_steps and steps are 30 and 150 respectively.

Don’t Forget To Clean Up

Once you're done, to delete all the services, simply use:
1
2
3
4
5
cdk destroy

#output prompt (choose 'y' to continue)

Are you sure you want to delete: BedrockLambdaImgeGenWebsiteStack (y/n)?
You were able to set up and try the complete solution. Before we wrap up, let's quickly walk through some of important parts of the code to get a better understanding of what's going the behind the scenes.

Code Walkthrough

Since we will only focus on the important bits, a lot of the code (print statements, error handling etc.) has been omitted for brevity.

CDK

You can refer to the CDK code here.
We start by creating the API Gateway and the S3 bucket.
1
2
3
4
5
6
7
apigw := awscdkapigatewayv2alpha.NewHttpApi(stack, jsii.String("image-gen-http-api"), nil)

bucket := awss3.NewBucket(stack, jsii.String("website-s3-bucket"), &awss3.BucketProps{
BlockPublicAccess: awss3.BlockPublicAccess_BLOCK_ALL(),
RemovalPolicy: awscdk.RemovalPolicy_DESTROY,
AutoDeleteObjects: jsii.Bool(true),
})
Then we create the CloudFront Origin Access Identity and grant S3 bucket read permissions to the CloudFront Origin Access Identity principal. Then we create the CloudFront Distribution:
  • Specify the S3 bucket as the origin.
  • Specify the Origin Access Identity that we created before.
1
2
3
4
5
6
7
8
9
10
11
12
oai := awscloudfront.NewOriginAccessIdentity(stack, jsii.String("OAI"), nil)

bucket.GrantRead(oai.GrantPrincipal(), "*")

distribution := awscloudfront.NewDistribution(stack, jsii.String("MyDistribution"), &awscloudfront.DistributionProps{
DefaultBehavior: &awscloudfront.BehaviorOptions{
Origin: awscloudfrontorigins.NewS3Origin(bucket, &awscloudfrontorigins.S3OriginProps{
OriginAccessIdentity: oai,
}),
},
DefaultRootObject: jsii.String("index.html"), //name of the file in S3
})
Then, we create the image generation Lambda function along with IAM permissions (to the function execution IAM role) to allow it to invoke Bedrock operations.
1
2
3
4
5
6
7
8
9
10
11
12
function := awscdklambdagoalpha.NewGoFunction(stack, jsii.String("bedrock-imagegen-s3"),
&awscdklambdagoalpha.GoFunctionProps{
Runtime: awslambda.Runtime_GO_1_X(),
Entry: jsii.String(functionDir),
Timeout: awscdk.Duration_Seconds(jsii.Number(30)),
})

function.AddToRolePolicy(awsiam.NewPolicyStatement(&awsiam.PolicyStatementProps{
Actions: jsii.Strings("bedrock:*"),
Effect: awsiam.Effect_ALLOW,
Resources: jsii.Strings("*"),
}))
Finally, we configure Lambda function integration with API Gateway, add the HTTP routes and specify API Gateway endpoint, S3 bucket name and CloudFront domain name as CloudFormation outputs.
1
2
3
4
5
6
7
8
9
10
11
12
functionIntg := awscdkapigatewayv2integrationsalpha.NewHttpLambdaIntegration(jsii.String("function-integration"), function, nil)

apigw.AddRoutes(&awscdkapigatewayv2alpha.AddRoutesOptions{
Path: jsii.String("/"),
Methods: &[]awscdkapigatewayv2alpha.HttpMethod{awscdkapigatewayv2alpha.HttpMethod_POST},
Integration: functionIntg})

awscdk.NewCfnOutput(stack, jsii.String("apigw URL"), &awscdk.CfnOutputProps{Value: apigw.Url(), Description: jsii.String("API Gateway endpoint")})

awscdk.NewCfnOutput(stack, jsii.String("cloud front domain name"), &awscdk.CfnOutputProps{Value: distribution.DomainName(), Description: jsii.String("cloud front domain name")})

awscdk.NewCfnOutput(stack, jsii.String("s3 bucket name"), &awscdk.CfnOutputProps{Value: bucket.BucketName(), Description: jsii.String("s3 bucket name")})

Lambda Function

You can refer to the Lambda Function code here.
In the function handler, we extract the prompt from the HTTP request body, and the configuration from the query parameters. Then it's used to call the model using bedrockruntime.InvokeModel function. Note the JSON payload sent to Amazon Bedrock is represented by an instance of the Request struct.
The output body returned from Amazon Bedrock Stability Diffusion model is a JSON payload which is converted into a Response struct which contains the generated image as a base64 string. This is returned as an events.APIGatewayV2HTTPResponse object along with CORS headers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
func handler(ctx context.Context, req events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {

prompt := req.Body

cfgScaleF, _ := strconv.ParseFloat(req.QueryStringParameters["cfg_scale"], 64)
seed, _ := strconv.Atoi(req.QueryStringParameters["seed"])
steps, _ := strconv.Atoi(req.QueryStringParameters["steps"])

payload := Request{
TextPrompts: []TextPrompt{{Text: prompt}},
CfgScale: cfgScaleF,
Steps: steps,
}

if seed > 0 {
payload.Seed = seed
}

payloadBytes, err := json.Marshal(payload)

output, err := brc.InvokeModel(context.Background(), &bedrockruntime.InvokeModelInput{
Body: payloadBytes,
ModelId: aws.String(stableDiffusionXLModelID),
ContentType: aws.String("application/json"),
})

var resp Response

err = json.Unmarshal(output.Body, &resp)

image := resp.Artifacts[0].Base64

return events.APIGatewayV2HTTPResponse{
StatusCode: http.StatusOK,
Body: image,
IsBase64Encoded: false,
Headers: map[string]string{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST,OPTIONS",
},
}, nil
}

//request/response model

type Request struct {
TextPrompts []TextPrompt `json:"text_prompts"`
CfgScale float64 `json:"cfg_scale"`
Steps int `json:"steps"`
Seed int `json:"seed"`
}

type TextPrompt struct {
Text string `json:"text"`
}

type Response struct {
Result string `json:"result"`
Artifacts []Artifact `json:"artifacts"`
}

type Artifact struct {
Base64 string `json:"base64"`
FinishReason string `json:"finishReason"`
}

Conclusion

In this tutorial, you used AWS CDK to deploy a serverless image generation solution that was implemented using Amazon Bedrock and AWS Lambda and was accessed using a static website on S3 via a CloudFront domain.
To stay updated with Generative AI content, visit this Space. If you are interested in an introductory guide to using the AWS Go SDK and Amazon Bedrock Foundation Models (FMs), check out this blog post.
Happy building!

Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.