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 login
login to AWS using Identity Centeraws sts get-caller-identity
get your AWS identityaws configure sso
configure your AWS credentialsaws configure list
list your AWS credentials
Deployment and testing
bunx cdk deploy
deploy this stack to your default AWS account/regionbunx cdk diff
compare deployed stack with current statebunx cdk synth
emits the synthesized CloudFormation templatego test
run unit testsbunx cdk destroy
destroy the stack
Development
go mod download
download dependenciesgo run sdxl-cdk-go.go
bundle handlergo mod tidy
update dependenciesgo mod verify
verify dependencies