|
| 1 | +# Serverless Text-to-Image Generation with Amazon Bedrock Nova Canvas |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This pattern implements a serverless text-to-image generation service using Amazon API Gateway, AWS Lambda and Amazon Bedrock's Nova Canvas model. It provides a REST API endpoint where users can submit text prompts. |
| 6 | + |
| 7 | +This invokes a Lambda function containing the request and the function makes a call to Amazon Bedrock's Nova Canvas model to generate an image based on the text description. Once the image is generated, the Lambda function saves it to an S3 bucket and returns this filename to the user through the API Gateway API. |
| 8 | + |
| 9 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/apigw-bedrock-nova-canvas |
| 10 | + |
| 11 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 16 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 17 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 18 | +* [Terraform](https://learn.hashicorp.cxom/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed |
| 19 | +* [Amazon Bedrock Nova Canvas Foundation Model Access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html#add-model-access) |
| 20 | + |
| 21 | +## Deployment Instructions |
| 22 | + |
| 23 | +For this pattern, you would need access only to Amazon Nova Canvas foundation model (Model ID: amazon.nova-canvas-v1:0) in us-east-1 region, since the pattern uses us-east-1 region by default. |
| 24 | + |
| 25 | +You must request access to the model before you can use it. If you try to use the model before you have requested access to it, you will receive an error message. |
| 26 | + |
| 27 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 28 | + ``` |
| 29 | + git clone https://github.com/aws-samples/serverless-patterns |
| 30 | + ``` |
| 31 | +1. Change directory to the pattern directory: |
| 32 | + ``` |
| 33 | + cd apigw-bedrock-nova-canvas |
| 34 | + ``` |
| 35 | +1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration: |
| 36 | + ``` |
| 37 | + terraform init |
| 38 | + ``` |
| 39 | +1. From the command line, apply the configuration in the main.tf file: |
| 40 | + ``` |
| 41 | + terraform apply |
| 42 | + ``` |
| 43 | +1. During the prompts |
| 44 | + ``` |
| 45 | + #var.prefix |
| 46 | + - Enter a value: {enter any prefix to associate with resources} |
| 47 | + ``` |
| 48 | +
|
| 49 | +## Testing |
| 50 | +
|
| 51 | +1. Make a POST request to the API using the following cURL command: |
| 52 | +
|
| 53 | + ``` |
| 54 | + curl -X POST 'API_ENDPOINT' --header "Content-Type: application/json" --data '{"prompt": "YOUR_PROMPT"}' |
| 55 | + ``` |
| 56 | +
|
| 57 | + Note: Replace `API_ENDPOINT` with the generated `api_endpoint` from Terraform (refer to the Terraform Outputs section), "YOUR_PROMPT" with your desired prompt. For ex, |
| 58 | +
|
| 59 | + ``` |
| 60 | + curl -X POST 'https://1234abcde.execute-api.us-east-1.amazonaws.com/dev/image_gen' --header "Content-Type: application/json" --data '{"prompt": "Kitten playing the piano"}' |
| 61 | + ``` |
| 62 | +
|
| 63 | +1. Once the API Gateway responds with the image ID, you can navigate to the S3 bucket (refer to the Terraform Outputs section for the bucket name) and select the correct image ID to view the generated image. |
| 64 | +
|
| 65 | +## Cleanup |
| 66 | +
|
| 67 | +1. Change directory to the pattern directory: |
| 68 | + ``` |
| 69 | + cd serverless-patterns/apigw-bedrock-nova-canvas |
| 70 | + ``` |
| 71 | +
|
| 72 | +1. Delete all created resources |
| 73 | + ``` |
| 74 | + terraform destroy |
| 75 | + ``` |
| 76 | + |
| 77 | +1. During the prompts: |
| 78 | + ``` |
| 79 | + Enter all details as entered during creation. |
| 80 | + ``` |
| 81 | +
|
| 82 | +1. Confirm all created resources has been deleted |
| 83 | + ``` |
| 84 | + terraform show |
| 85 | + ``` |
| 86 | +---- |
| 87 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 88 | +
|
| 89 | +SPDX-License-Identifier: MIT-0 |
0 commit comments