Introduction
AWS provides SDK for .Net, using which we can write code to manage Step Function. In this article, we are going to learn how we can start a Step Function using AWS SDK. One important point, to start a step function, we will require the Amazon Resource Name (ARN) of the Step Function state machine.
Prerequisite
Install the package 'AWSSDK.StepFunctions' using Nuget in your .Net project.
Sample code to start a step-function
1. AWSSateMachineHelper.cs: Helper class for step function
using Amazon.StepFunctions.Model;
using Amazon.StepFunctions;
using System.Net;
using Amazon;
using Amazon.Util;
namespace StepFunction.Examples.Helper
{
public class AWSSateMachineHelper
{
/// <summary>
/// Start execution of an AWS Step Functions state machine.
/// </summary>
/// <param name="executionName">The name to use for the execution.</param>
/// <param name="executionJson">The JSON string to pass for execution.</param>
/// <param name="stateMachineArn">The Amazon Resource Name (ARN) of the Step Functions state machine.</param>
/// <returns>The Amazon Resource Name (ARN) of the AWS Step Functions execution.</returns>
public async Task<string> StartExecutionAsync(string executionName, string executionJson, string stateMachineArn)
{
using (var _amazonStepFunctions = GetStepfunctionClient())
{
StartExecutionRequest executionRequest = new StartExecutionRequest
{
Name = $"{executionName}_{Guid.NewGuid().ToString()}",
Input = executionJson,
StateMachineArn = stateMachineArn
};
// response from the StartExecution service method, as returned by StepFunction.
var response = await _amazonStepFunctions.StartExecutionAsync(executionRequest);
return response.ExecutionArn;
}
}
/// <summary>
/// Get Object Of AWS Step Function Client
/// </summary>
/// <returns>Object Of AWS Step Function Client</returns>
private AmazonStepFunctionsClient GetStepfunctionClient()
{
AmazonStepFunctionsConfig amazonStepFunctionsConfig = new AmazonStepFunctionsConfig { RegionEndpoint = RegionEndpoint.EUWest1 };
string aws_Access_Key = Environment.GetEnvironmentVariable("AWS_Access_Key");
string aws_Access_Key_Secret = Environment.GetEnvironmentVariable("AWS_Access_Key_Secret");
if (!string.IsNullOrEmpty(aws_Access_Key) && !string.IsNullOrEmpty(aws_Access_Key_Secret))
{
return new AmazonStepFunctionsClient(aws_Access_Key, aws_Access_Key_Secret, amazonStepFunctionsConfig);
}
else
{
return new AmazonStepFunctionsClient(amazonStepFunctionsConfig);
}
}
}
}
2. Call StartExecutionAsync method of AWSSateMachineHelper class.
public async Task<string> ExecuteCustomerOrder(OrderDetails orderDetails)
{
try
{
AWSSateMachineHelper aWSSateMachineHelper = new AWSSateMachineHelper();
string executionName = "OrderExecution";
string executionJSON = JsonSerializer.Serialize(orderDetails);
string stateMachineARN = "arn:aws:states:eu-west-1:873710106825:stateMachine:MyStateMachine";
var result = await aWSSateMachineHelper.StartExecutionAsync(executionName, executionJSON, stateMachineARN);
return await Task.FromResult(result);
}
catch (Exception ex)
{
return ex.ToString();
}
}
About ARN
A qualified state machine ARN can either refer to a Distributed Map state defined within a state machine, a version ARN, or an alias ARN.
Examples of qualified and unqualified state machine ARNs
- The following qualified state machine ARN refers to a Distributed Map state with a label mapStateLabel in a state machine named myStateMachine.
arn:partition:states:region:account-id:stateMachine:myStateMachine/mapStateLabel
If you provide a qualified state machine ARN that refers to a Distributed Map state, the request fails with ValidationException.
- The following qualified state machine ARN refers to an alias named PROD.
arn::states:::stateMachine:
If you provide a qualified state machine ARN that refers to a version ARN or an alias ARN, the request starts execution for that version or alias.
- The following unqualified state machine ARN refers to a state machine named myStateMachine.
arn::states:::stateMachine:
Note
- If you start an execution with an unqualified state machine ARN, Step Functions uses the latest revision of the state machine for the execution.
- To start executions of a state machine version, call StartExecution and provide the version ARN or the ARN of an alias that points to the version.
Conclusion
In this article, we have learned how to start an AWS Step Function State machine using AWS SDK for .NET.