Introduction
Amazon Bedrock Flows allows you to build and orchestrate AI workflows through a visual builder, enabling seamless integration with Amazon Bedrock services like foundational models, knowledge bases, and prompt management. It also integrates smoothly with other AWS services, such as AWS Lambda and Amazon S3. In my previous article, I covered how to automate the creation of a flow with a single prompt in Amazon Bedrock using the AWS CLI—specifically to generate a music playlist based on genre and the number of songs requested. In this article, you’ll learn how to invoke an Amazon Bedrock flow using C# within a simple console application created using Visual Studio 2022.
Prerequisites
- AWS account and the required permissions to access Amazon Bedrock.
- Access to Amazon Bedrock foundation model to validate the prompt flow.
- Get credentials to grant programmatic access.
- Visual Studio 2022.
- Install and set up the AWS Toolkit for Visual Studio.
Steps Involved
Perform the following steps to invoke an Amazon Bedrock flow using C# within a simple console application created using Visual Studio 2022.
- Open Visual Studio 2022.
- Click File -> New -> Project.
- Select the Console App template. Click Next.
- Enter the project name and click Next.
- Select the .NET 8.0 framework. Click Create.
- Add the following NuGet packages.
AWSSDK.BedrockAgentRuntime - Open Program.cs and replace the code with the following. Replace the flowId and flowAliasId with actual values.
using Amazon; using Amazon.BedrockAgentRuntime; using Amazon.BedrockAgentRuntime.Model; using Amazon.Runtime.Documents; namespace AmazonBedrockFlows { internal class Program { static async Task Main(string[] args) { // Initialize the AmazonBedrockAgentRuntimeClient to interact with Amazon Bedrock services var client = new AmazonBedrockAgentRuntimeClient(RegionEndpoint.USEast1); // Set the Flow ID and Flow Alias ID which will be used to invoke the flow var flowId = "W41B*****C"; // Flow ID (unique identifier for the flow) var flowAliasId = "69XU6****F"; // Flow Alias ID (represents a specific version of the flow) // Create the input document that will be sent to the flow var document = new Document(new Dictionary<string, Document> { { "genre", new Document("pop") }, { "number", new Document(5) } }); // Create a flow input object which includes the document var flowInput = new FlowInput { Content = new FlowInputContent { Document = document }, NodeName = "FlowInput", NodeOutputName = "document" }; // Prepare the request object that will be sent to invoke the flow var request = new InvokeFlowRequest { FlowIdentifier = flowId, FlowAliasIdentifier = flowAliasId, Inputs = new List<FlowInput> { flowInput } }; try { // Invoke the flow asynchronously and await the response var response = await client.InvokeFlowAsync(request); // Initialize variables to hold the flow output and completion events FlowOutputEvent? flowOutputEvent = null; FlowCompletionEvent? flowCompletionEvent = null; // Process the response stream from the flow invocation foreach (var eventItem in response.ResponseStream) { if (eventItem is FlowOutputEvent outputEvent) flowOutputEvent = outputEvent; if (eventItem is FlowCompletionEvent completionEvent) flowCompletionEvent = completionEvent; } // Check if the flow has completed successfully and output the results if (flowCompletionEvent?.CompletionReason == "SUCCESS" && flowOutputEvent?.Content?.Document != null) { Console.WriteLine("Flow invocation was successful! The output of the flow is as follows:"); Console.WriteLine($"Document Content: {flowOutputEvent.Content.Document}"); } else { Console.WriteLine($"Flow invocation completed with reason: {flowCompletionEvent?.CompletionReason ?? "Unknown"}"); } } catch (Exception ex) { Console.WriteLine($"Error invoking flow: {ex.Message}"); } } } } - Run the application.
Output

References
Summary
This article describes how to invoke an Amazon Bedrock flow using C# within a simple console application created using Visual Studio 2022.

Join the conversation! Your thoughts help the community grow.