Generate Image from Text Using Amazon Bedrock's Stable Diffusion XL Model via AWS CLI
Tags: aws, generativeai, amazonbedrock
Published: 2023-10-09 19:36:59

In this article, we will learn [another way](https://dev.to/aws-builders/build-serverless-generative-ai-api-service-with-aws-lambda-and-amazon-bedrock-3abc) to invoke Amazon Bedrock's Model using AWS Command Line Interface (CLI).

We are going to generate an image from a text prompt using Amazon Bedrock's Stable DIffusion XL Model via AWS CLI.

From this text prompt
```sh
the quick brown fox jumps over the lazy dog
```
to this generated image by Amazon Bedrock.

![the quick brown fox jumps over the lazy dog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j19ke2b14xwqnr3jjq6u.png)

## Follow me for more
{% embed https://linktr.ee/fullstacksaiyan %}

## Amazon Bedrock 

[Amazon Bedrock](https://aws.amazon.com/bedrock/) is a fully managed service from Amazon Web Services that provides access to foundation models from Amazon and select third-party providers through a simple API. It allows customers to easily choose from a wide range of foundation models to find the one best suited for their use case. With Amazon Bedrock, customers can quickly get started with these models, privately customize them using their own data, and integrate them into applications. The service handles all infrastructure management so customers can focus on using the models without having to worry about deployment or maintenance. Amazon Bedrock also integrates with other Amazon Web Services like Amazon SageMaker for capabilities such as model testing and lifecycle management.

## Prerequisites Tools <a name="prerequisites"></a>
- [Install AWS CLI version >= 1.29.57](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)
- [Install Jq](https://jqlang.github.io/jq/download/)

## Table of Contents
1. [Create an IAM User with Amazon Bedrock Invoke Model Policy](#step-1)
2. [Create User Access Keys](#step-2)
3. [Configure AWS CLI](#step-3)
4. [Enable Amazon Bedrock Model Access](#step-4)
5. [Create Text Prompt](#step-5)
6. [Invoke Amazon Bedrock's Stable Diffusion XL Model](#step-6)
7. [Conclusion](#step-7)

## Create an IAM User with Amazon Bedrock Invoke Model Policy <a name="step-1"></a>
- From AWS Management Console search IAM
![iam](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92km0lp95iqxhaallcbp.png)
- Go to user menu and click Create User
![create user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9h6lqt9by2u6p89gbuf7.png)
- Specify user detail then click Next
![user detail](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x4iftzknx2h6noddlkny.png)
- Create Policy
![create policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk199hs4f6amy1sn543t.png)
- Specify Permission
![specify permission](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/10a7jqetoel3hmkxy0mq.png)
- Review and Create Policy
![create policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqgbxgzktztvjh3dghq5.png)

## Create User Access Keys <a name="step-2"></a>
- Go to Security Credentials
![security credentials](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iexetu24bv5c1mwmfixs.png)

- Choose the access key use case
![access key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzhy7gmn6wct6onpkb0i.png)
- Set Descriptions
![descriptions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v7yxmrflfakxwrqrkoy4.png)
- Copy Access Key and Secret access key or you can download in a csv format then click done
![access key secret key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yjd1ad3pm6bbc6youmbt.png)

## Enable Amazon Bedrock Model Access <a name="step-3"></a>
- Go to Amazon Bedrock in AWS Management Console (make sure you are in `N.Virginia (us-east-1)` region.
- Go to Model access menu then click Edit.

![model access](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2e8rlby92a4lu5y5b9ru.png)

- Check list Stability AI model and click save changes

![check list stability AI model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cul2pxpdtgc3zacfmcwo.png)

## Configure AWS CLI <a name="step-4"></a>

Configure AWS CLI with the Access Key and Secret Access key that you created before using `aws configure` command in your terminal.
```sh
$ aws configure
AWS Access Key ID [****************T7JE]: xxxxxx
AWS Secret Access Key [****************153Y]: xxxxx
Default region name [ap-southeast-1]: us-east-1
Default output format [json]: json
```

## Create Text Prompt <a name="step-5"></a>
We will create a file `body.json` that contains a text prompt and some model controls that we are going to use to invoke Amazon Bedrock Model.
```json
{
  "text_prompts": [{ "text": "the quick brown fox jumps over the lazy dog" }],
  "cfg_scale": 10,
  "seed": 0,
  "steps": 50
}
```
The Stability.ai Diffusion models support the following controls.

Prompt strength (`cfg_scale`) – Determines how much the final image portrays the prompt. Use a lower number to increase randomness in the generation.

Generation step (`steps`) – Generation step determines how many times the image is sampled. More steps can result in a more accurate result.

Seed (`seed`) – The seed determines the initial noise setting. Use the same seed and the same settings as a previous run to allow inference to create a similar image. If you don't set this value, it is set as a random number.

## Invoke Amazon Bedrock's Stable Diffusion XL Model <a name="step-6"></a>
- Invoke Amazon Bedrock model using command below and then output it into `bedrock-response.json`
```sh
aws bedrock-runtime invoke-model \
--body "$(base64 -i body.json)" \
--model-id "stability.stable-diffusion-xl-v0" \
--region us-east-1
bedrock-response.json 
```
- Amazon Bedrock will output the image in base64 format into `bedrock-response.json`
```json
{"result":"success","artifacts":[{"seed":0,"base64":"iVBORw0KGgoAAA.......","finishReason":"SUCCESS"}]}
```
- So we need to filter out the base64 part from `bedrock-response.json `, then output it into a file `base64.txt`
```sh
cat bedrock-response.json | jq -r ".artifacts[0].base64" > base64.txt
```
- Last, Decode `base64.txt` file into `image-result.png`
```sh
cat base64.txt | base64 -d > image-result.png
```
- You should get `image-result.png` file with this image
![the quick brown fox jumps over the lazy dog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j19ke2b14xwqnr3jjq6u.png)


## Conclusion <a name="step-7"></a>
From this article, we learn that we have the option to utilize the Amazon Bedrock model not only through the AWS-SDK but also directly via AWS-CLI from our terminal.

## Check out my previous post

{% embed https://dev.to/aws-builders/build-serverless-generative-ai-api-service-with-aws-lambda-and-amazon-bedrock-3abc %}