Automate Prompt Flow Creation in Amazon Bedrock Using AWS CLI

Introduction

Amazon Bedrock Flows enables you to build and orchestrate AI workflows using a visual builder, seamlessly integrating with Amazon Bedrock services like foundational models, knowledge bases, and prompt management. It also connects with other AWS services, such as AWS Lambda and Amazon S3. In this article, you’ll learn how to automate the creation of a flow with a single prompt in Amazon Bedrock using the AWS CLI. Specifically, we’ll generate a music playlist based on genre and the number of songs requested.

List

Pre-requisites

  1. AWS account and the required permissions to access Amazon Bedrock.
  2. Access to Amazon Bedrock foundation model to validate the prompt flow.
  3. Install or update to the latest version of the AWS CLI.
  4. Get credentials to grant programmatic access.

Create a service role to create and manage a flow in Amazon Bedrock

Perform the following steps to create a service role for Amazon Bedrock flows.

Create a service role

Create a JSON file with the following content and save it as trust-policy.json.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "bedrock.amazonaws.com"
            },
            "Action": "sts:AssumeRole",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "<account-id>"
                },
                "ArnLike": {
                    "aws:SourceArn": "arn:aws:bedrock:<region>:<account-id>:flow/*"
                }
            }
        }
    ]
}

Replace the <region> and <account-id> with the actual values.

Open Windows Prompt and execute the following command to create a service role.

# Create the service role
aws iam create-role \
  --role-name AmazonBedrockFlowServiceRole \
  --assume-role-policy-document file://trust-policy.json

Create the permission policy

Create a JSON file with the following content and save it as flow-permission-policy.json.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "InvokeModel",
            "Effect": "Allow",
            "Action": "bedrock:InvokeModel",
            "Resource": [
                "arn:aws:bedrock:<region>::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0"
            ]
        },
        {
            "Sid": "InvokeProvisionedThroughput",
            "Effect": "Allow",
            "Action": [
                "bedrock:GetFlow"
            ],
            "Resource": [
                "arn:aws:bedrock:<region>:<account-id>:flow/*"
            ]
        }
    ]
}

Replace the <region> and <account-id> with the actual values.

Open Windows Prompt and execute the following command to create a policy.

# Create the permission policy
aws iam create-policy \
  --policy-name AmazonBedrockFlowServicePolicy \
  --policy-document file://flow-permission-policy.json

Attach the permission policy

Open Windows Prompt and execute the following command to attach the policy to the service role.

# Attach the permission policy
aws iam attach-role-policy \
    --role-name AmazonBedrockFlowServiceRole \
    --policy-arn arn:aws:iam::654654320368:policy/AmazonBedrockFlowServicePolicy

Create a prompt flow

Perform the following steps to create a flow in Amazon Bedrock.

Create a JSON file with the following content and save it as flow-definition.json. Refer to this Create Flow request syntax documentation to prepare the flow definition. Note: Alternatively, you can use the Amazon Bedrock console to create the flow based on your requirements in the visual builder. Once you've created the flow, you can use the get-flow CLI command to retrieve the flow definition.

{
    "nodes": [
        {
            "name": "FlowInput",
            "type": "Input",
            "configuration": {
                "input": {}
            },
            "outputs": [
                {
                    "name": "document",
                    "type": "Object"
                }
            ]
        },
        {
            "name": "MakePlaylist",
            "type": "Prompt",
            "configuration": {
                "prompt": {
                    "sourceConfiguration": {
                        "inline": {
                            "modelId": "anthropic.claude-3-5-sonnet-20240620-v1:0",
                            "templateConfiguration": {
                                "text": {
                                    "text": "Make me a {{genre}} playlist consisting of the following number of songs: {{number}}.",
                                    "inputVariables": [
                                        { "name": "genre" },
                                        { "name": "number" }
                                    ]
                                }
                            },
                            "templateType": "TEXT",
                            "inferenceConfiguration": {
                                "text": {
                                    "maxTokens": 2000,
                                    "stopSequences": [],
                                    "temperature": 1.0,
                                    "topP": 0.9990000128746033
                                }
                            }
                        }
                    }
                }
            },
            "inputs": [
                {
                    "name": "genre",
                    "type": "String",
                    "expression": "$.data.genre"
                },
                {
                    "name": "number",
                    "type": "Number",
                    "expression": "$.data.number"
                }
            ],
            "outputs": [
                {
                    "name": "modelCompletion",
                    "type": "String"
                }
            ]
        },
        {
            "name": "FlowOutputNode",
            "type": "Output",
            "configuration": {
                "output": {}
            },
            "inputs": [
                {
                    "name": "document",
                    "type": "String",
                    "expression": "$.data"
                }
            ]
        }
    ],
    "connections": [
        {
            "name": "input1ToPrompt",
            "source": "FlowInput",
            "target": "MakePlaylist",
            "type": "Data",
            "configuration": {
                "data": {
                    "sourceOutput": "document",
                    "targetInput": "genre"
                }
            }
        },
        {
            "name": "input2ToPrompt",
            "source": "FlowInput",
            "target": "MakePlaylist",
            "type": "Data",
            "configuration": {
                "data": {
                    "sourceOutput": "document",
                    "targetInput": "number"
                }
            }
        },
        {
            "name": "promptToOutput",
            "source": "MakePlaylist",
            "target": "FlowOutputNode",
            "type": "Data",
            "configuration": {
                "data": {
                    "sourceOutput": "modelCompletion",
                    "targetInput": "document"
                }
            }
        }
    ]
}

Execute the following command to create a new flow. Replace the <account-id> with the actual value. Note down the flow ID.

aws bedrock-agent create-flow \
  --name "MakePlaylist" \
  --description "Flow to create a playlist based on genre and number" \
  --execution-role-arn "arn:aws:iam::<account-id>:role/AmazonBedrockFlowServiceRole" \
  --definition file://flow-definition.json

Execute the following command to prepare the DRAFT version of a flow so that it can be invoked. Replace the <flow-id> with the actual value.

# Prepare the DRAFT version of a flow so that it can be invoked
aws bedrock-agent prepare-flow --flow-identifier <flow-id>

Execute the following command to create a version of the flow. Replace the <flow-id> with the actual value.

# Create a version of the flow
aws bedrock-agent create-flow-version --flow-identifier <flow-id>

Execute the following command to create an alias of a flow. Replace the <flow-id> with the actual value.

# Creates an alias of a flow
aws bedrock-agent create-flow-alias \
    --flow-identifier <flow-id> \
    --name dev \
    --routing-configuration flowVersion="1"

Validate the flow

Navigate to the Amazon Bedrock service in the AWS Console. In the left-hand navigation pane, under the Builder Tools section, click Flows and select the newly created flow. Click Edit in Flow Builder. In the Test flow section, enter the following JSON to view the response and the trace.

References

https://docs.aws.amazon.com/bedrock/latest/userguide/flows.html

Summary

This article describes how to how to automate the prompt flow creation in Amazon Bedrock using the AWS CLI.

Up Next
    Ebook Download
    View all
    Learn
    View all