AWS. Lesson 5. Step Functions Tutorial

AWS Step Functions Tutorial
1. Creating a Simple Step Function
Example 1: Define a Basic Step Function
{
"StartAt": "FirstStep",
"States": {
"FirstStep": {
"Type": "Pass",
"Next": "SecondStep"
},
"SecondStep": {
"Type": "Succeed"
}
}
}
Explanation
Defines the starting point of execution.
"StartAt": "FirstStep"
FirstStep acts as a pass-through state.
"Type": "Pass", "Next": "SecondStep"
SecondStep completes the execution successfully.
"Type": "Succeed"
Creates a basic sequential workflow.
"Next": "SecondStep"
Example 2: Invoke a Lambda Function
{
"StartAt": "CallLambda",
"States": {
"CallLambda": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda",
"End": true
}
}
}
Explanation
Defines a state that calls a Lambda function.
"Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda"
Uses an AWS Lambda ARN for invocation.
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda"
Ends the step function after execution.
"End": true"
Executes the function and stops the workflow.
"StartAt": "CallLambda"
Example 3: Choice State for Conditional Logic
{
"StartAt": "CheckValue",
"States": {
"CheckValue": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.value",
"NumericGreaterThan": 10,
"Next": "Greater"
}
],
"Default": "LessThanOrEqual"
},
"Greater": { "Type": "Succeed" },
"LessThanOrEqual": { "Type": "Succeed" }
}
}
Explanation
Checks if the input value is greater than 10.
"Variable": "$.value", "NumericGreaterThan": 10
Routes execution to the ‘Greater’ state if true.
"Next": "Greater"
Defaults to ‘LessThanOrEqual’ if condition fails.
"Default": "LessThanOrEqual"
Enables branching workflows in Step Functions.
"Type": "Choice"
Example 4: Parallel Execution
{
"StartAt": "ParallelExecution",
"States": {
"ParallelExecution": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "Task1",
"States": { "Task1": { "Type": "Succeed" } }
},
{
"StartAt": "Task2",
"States": { "Task2": { "Type": "Succeed" } }
}
],
"End": true
}
}
}
Explanation
Executes multiple branches simultaneously.
"Type": "Parallel"
Defines two parallel branches, Task1 and Task2.
"Branches": [{ "StartAt": "Task1" }, { "StartAt": "Task2" }]
Completes execution when both tasks finish.
"End": true"
Improves performance by running independent tasks together.
"States": { "Task1": { "Type": "Succeed" } }
2. Passing Data Between Steps
Example 1: Input Data for Step Function
{
"StartAt": "ProcessData",
"States": {
"ProcessData": {
"Type": "Pass",
"Result": { "message": "Hello, World!" },
"Next": "OutputData"
},
"OutputData": {
"Type": "Succeed"
}
}
}
Explanation
Passes a static JSON object to the next step.
"Result": { "message": "Hello, World!" }
Ensures data flows from ‘ProcessData’ to ‘OutputData’.
"Next": "OutputData"
Useful for debugging and testing workflows.
"Type": "Pass"
Completes the workflow when OutputData executes.
"Type": "Succeed"
Example 2: Use InputPath to Filter Incoming Data
{
"StartAt": "FilterData",
"States": {
"FilterData": {
"Type": "Pass",
"InputPath": "$.details",
"Next": "OutputFilteredData"
},
"OutputFilteredData": { "Type": "Succeed" }
}
}
Explanation
Extracts only the ‘details’ field from input.
"InputPath": "$.details"
Removes unnecessary input fields.
"Type": "Pass"
Sends the filtered data to the next state.
"Next": "OutputFilteredData"
Ensures clean, optimized data processing.
"Type": "Succeed"
Example 3: Use ResultPath to Modify Output
{
"StartAt": "TransformData",
"States": {
"TransformData": {
"Type": "Pass",
"Result": { "status": "processed" },
"ResultPath": "$.processingInfo",
"Next": "Done"
},
"Done": { "Type": "Succeed" }
}
}
Explanation
Adds new data without modifying original input.
"ResultPath": "$.processingInfo"
Stores processed status separately.
"Result": { "status": "processed" }
Combines existing and new data in workflow.
"Next": "Done"
Useful for transforming input data.
"Type": "Pass"
Example 4: Using OutputPath
{
"StartAt": "TrimData",
"States": {
"TrimData": {
"Type": "Pass",
"OutputPath": "$.trimmed",
"Next": "Finish"
},
"Finish": { "Type": "Succeed" }
}
}
Explanation
Reduces the output to only the ‘trimmed’ field.
"OutputPath": "$.trimmed"
Removes unnecessary data from the workflow output.
"Type": "Pass"
Passes only the needed data to Finish state.
"Next": "Finish"
Helps optimize Step Function execution costs.
"Type": "Succeed"
Next Steps
You can integrate Step Functions with Lambda, DynamoDB, SNS, and SQS.
More advanced scenarios, are: error handling, retries, or state machine triggers, etc.
Subscribe to my newsletter
Read articles from user1272047 directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
