How to use Stable Diffusion on AWS Bedrock
Quick answer
Use the
boto3 bedrock-runtime client to call the Stable Diffusion model on AWS Bedrock by invoking the converse method with a properly formatted message containing your prompt. Specify the Stable Diffusion model ID such as amazon.stable-diffusion-xl-1024-v1 and parse the base64-encoded image from the response.PREREQUISITES
Python 3.8+AWS account with Bedrock accessAWS CLI configured with credentialspip install boto3
Setup
Install the boto3 library and configure your AWS credentials with Bedrock access. Ensure your AWS CLI is set up with the appropriate permissions to call Bedrock models.
- Install boto3:
pip install boto3 - Configure AWS CLI:
aws configure
pip install boto3 Step by step
This example shows how to generate an image from a text prompt using the Stable Diffusion model amazon.stable-diffusion-xl-1024-v1 on AWS Bedrock. The response contains a base64-encoded PNG image string which you can decode and save locally.
import boto3
import base64
# Initialize Bedrock runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Define the model ID for Stable Diffusion
model_id = 'amazon.stable-diffusion-xl-1024-v1'
# Text prompt for image generation
prompt = 'A futuristic cityscape at sunset, vibrant colors, highly detailed'
# Prepare the message payload as per Bedrock API
messages = [
{
'role': 'user',
'content': [
{
'type': 'text',
'text': prompt
}
]
}
]
# Call the Bedrock converse API
response = client.converse(
modelId=model_id,
messages=messages
)
# Extract the base64 image string from the response
output_message = response['output']['message']['content'][0]
image_b64 = output_message.get('image_base64') or output_message.get('text')
if image_b64:
# Decode and save the image
image_data = base64.b64decode(image_b64)
with open('generated_image.png', 'wb') as f:
f.write(image_data)
print('Image saved as generated_image.png')
else:
print('No image data found in response') output
Image saved as generated_image.png
Common variations
You can customize the image generation by modifying the prompt or using different Stable Diffusion model versions available on Bedrock. For asynchronous calls, use client.invoke_model with the same payload. You can also adjust parameters like image size or guidance scale if supported by the model.
import boto3
import json
import base64
client = boto3.client('bedrock-runtime', region_name='us-east-1')
model_id = 'amazon.stable-diffusion-xl-1024-v1'
prompt = 'A serene mountain lake at dawn, photorealistic'
payload = {
'messages': [
{
'role': 'user',
'content': [
{
'type': 'text',
'text': prompt
}
]
}
]
}
response = client.invoke_model(
modelId=model_id,
body=json.dumps(payload)
)
result = json.loads(response['body'].read())
image_b64 = result['output']['message']['content'][0].get('image_base64')
if image_b64:
with open('mountain_lake.png', 'wb') as f:
f.write(base64.b64decode(image_b64))
print('Image saved as mountain_lake.png')
else:
print('No image data found') output
Image saved as mountain_lake.png
Troubleshooting
- AccessDeniedException: Ensure your AWS IAM user or role has Bedrock permissions.
- Model not found: Verify the model ID is correct and available in your region.
- No image data returned: Check the prompt format and model response structure; some models may return image data under different keys.
- Timeouts: Increase client timeout or retry the request.
Key Takeaways
- Use boto3's bedrock-runtime client to call Stable Diffusion on AWS Bedrock.
- Pass the text prompt in the messages parameter with role 'user' and type 'text'.
- Decode the base64 image string from the response to save the generated image.
- Verify AWS permissions and model ID to avoid common errors.
- You can use both synchronous converse and invoke_model methods for generation.