CDK Stack for Amazon Bedrock and Stability.ai Stable Diffusion XL 1.0 Image Generation
A CDK pattern for generating images using AWS Bedrock via SDXL. It is built using the AWS Cloud Development Kit, an Infrastructure as Code (IaC) tool to compose existing AWS constructs to create an AWS Lambda with a Function URL and permissions to call the Bedrock API. Implemented in Go. :-)
- Generates photorealistic images with high quality
- Includes HTML application for generating images
- Responses can be received in 7-10 seconds
- Configurable inputs, limited to a single prompt
- Quick, repeatable set-up and teardown
- Automatic bundling with 3-6 second hot swaps
- Rigorous error handling with forgiving responses
This project leverages Amazon Lambda Golang Library, an experimental library under active development that allows us to define our Lambda function using a handler code entrypoint as opposed to defining the handler using an asset for automatic bundling and faster hot-swapping behavior.
- AWS CLI installed and configured with your AWS account
- Go for building the Lambda function
- Bun for running CDK CLI commands directly
Once deployed, curl the function URL to generate an image:
curl -X POST \
-H "Content-Type: text/plain" \
-d 'a photograph of an astronaut riding a horse, 8k' \
'https://tfxu2xorlbqekqv3l1kytkdb3e0nkjgk.lambda-url.us-east-1.on.aws/?cfg_scale=8.0&steps=20&seed=0'The endpoint URL will become available upon deployment and will appear in the CDK stack outputs. Pass in your prompt as the body of the request in plain text. The following query string parameters are supported:
cfg_scale: The scale to use when generating the imageseed: The seed to use when generating the imagesteps: The number of steps to use when generating the imagewidth: The width of the image to generateheight: The height of the image to generate
All parameters are optional. If not specified, the Bedrock defaults will be used.
Accounts and regions
aws sso loginlogin to AWS using Identity Centeraws sts get-caller-identityget your AWS identityaws configure ssoconfigure your AWS credentialsaws configure listlist your AWS credentials
Deployment and testing
bunx cdk deploydeploy this stack to your default AWS account/regionbunx cdk diffcompare deployed stack with current statebunx cdk synthemits the synthesized CloudFormation templatego testrun unit testsbunx cdk destroydestroy the stack
Development
go mod downloaddownload dependenciesgo run sdxl-cdk-go.gobundle handlergo mod tidyupdate dependenciesgo mod verifyverify dependencies
