Automate Workflows using Step Functions
Welcome to this lesson on automating workflows using AWS Step Functions. In this chapter, you'll learn how to coordinate distributed tasks into resilient, visible, and fault-tolerant workflows using Step Functions. You'll also explore a CLI-based demonstration that ties together Lambda functions orchestrated by a Step Function state machine.
What Are Step Functions?
AWS Step Functions is a fully managed service that lets you define application workflows as state machines in a visual and declarative way. These workflows can coordinate Lambda functions, ECS tasks, Batch jobs, Glue jobs, and other services without writing orchestration logic in your application code. Key advantages include built-in fault tolerance, retries, error handling, branching, parallel execution, and detailed execution history.
Think of a Step Function as a flowchart for your application—each step represents a task, choice, or wait state. Your application moves from one state to another based on transitions you define, following a predictable and observable path.
Why Use Step Functions?
Instead of building custom orchestration logic (e.g. retries, chaining, or branching) inside your Lambda or backend code, Step Functions can:
- Manage retries with exponential backoff;
- Track and store execution history;
- Integrate seamlessly with AWS Lambda, SQS, SNS, DynamoDB, Athena, ECS, and many other services;
- Separate business logic (inside Lambda) from orchestration logic (inside the state machine);
- Provide observability and auditability through logs and visualizations in the console.
This leads to cleaner, modular, and resilient cloud-native applications.
Components of a State Machine
An AWS Step Function state machine is defined using JSON (or YAML) following the Amazon States Language (ASL) specification; Its core components are:
- States: the tasks or steps (like calling a Lambda function);
- Transitions: how the workflow moves from one state to another;
- StartAt: the entry point of the workflow;
- End or Next: indicates where the workflow finishes or proceeds;
- Retry / Catch / TimeoutSeconds: built-in error handling and execution control;
- Choice / Parallel / Wait: advanced control structures for conditional logic and concurrency.
This makes it simple to model even complex workflows without introducing procedural complexity.
Building a Simple Step Function Workflow
Let's now walk through how to build a basic workflow using Step Functions and the AWS CLI.
This example ties together two Lambda functions: one prints "hello"
and the other prints "world"
.
We'll define a state machine that executes both in sequence and then inspect the result.
Step-by-Step Workflow
1. Create the Lambda Functions
Start by creating two simple Lambda functions, then zip them for deployment:
hello.py
: returns the string"Hello"
;world.py
: returns the string"World"
.
Zip each function into a deployable package.
2. Create a Lambda Execution Role
Create an IAM role for Lambda execution with the following permissions:
- Allow the Lambda service to assume the role;
- Attach the AWSLambdaBasicExecutionRole managed policy;
- This policy is essential to let Lambda write logs to CloudWatch.
Without this policy, the Lambda function won't execute properly.
4. Create the Step Function Execution Role
Create an IAM role for the Step Functions service with the following:
- An assume-role policy for
states.amazonaws.com
; - Attach a custom inline policy that allows the state machine to invoke your Lambda functions.
IAM is a critical part of Step Functions. The workflow won't execute unless Step Functions is permitted to call the resources in your tasks.
5. Define the State Machine
Create a JSON definition file (hello-world-definition.json
) with the following:
{
"Comment": "A simple Hello World step function",
"StartAt": "HelloState",
"States": {
"HelloState": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:HelloFunction",
"Next": "WorldState"
},
"WorldState": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:WorldFunction",
"End": true
}
}
}
- The workflow starts at
HelloState
; - Executes the first Lambda, then transitions to
WorldState
; WorldState
runs the second Lambda and ends the workflow.
6. Deploy the State Machine
Run the following command to create the state machine:
aws stepfunctions create-state-machine \
--name HelloWorldStateMachine \
--role-arn arn:aws:iam::account-id:role/StepFunctionExecutionRole \
--definition file://hello-world-definition.json
This deploys the workflow into AWS Step Functions.
7. Start an Execution
Trigger the state machine using:
aws stepfunctions start-execution \
--state-machine-arn arn:aws:states:region:account-id:stateMachine:HelloWorldStateMachine \
--name DemoExecution
This launches a new run of the workflow named "DemoExecution"
.
Executions are uniquely identified and independently tracked.
8. Inspect the Execution
Use DescribeExecution
to retrieve execution status and metadata:
aws stepfunctions describe-execution \
--execution-arn <your-execution-arn>
This command shows:
- Execution start and end times;
- Status (e.g.
SUCCEEDED
,FAILED
); - Input and output data;
- Execution name and ARN.
9. View Execution History
Use GetExecutionHistory
to view a detailed log of all events:
aws stepfunctions get-execution-history \
--execution-arn <your-execution-arn>
This provides:
- State transitions (e.g. entered/exited states);
- Lambda function invocation details;
- Timestamps and error details if any;
- Useful for debugging and audit trails.
Key Exam Tips
For the AWS certification exam, be sure to:
- Understand the JSON structure of Step Functions state machines;
- Know how Step Functions integrate with Lambda, especially permissions and role configurations;
- Remember that Step Functions require IAM roles with specific permissions to call resources like Lambda;
- Understand how retries and error handling are configured within the state machine definition;
- Be familiar with how to view execution history using the CLI and console.
You've now built and executed a simple yet powerful orchestrated workflow using AWS Step Functions; This architecture is suitable for building robust, maintainable, and cloud-native workflows that respond to business logic with precision and visibility.
Let's now move forward to explore more advanced orchestration techniques.
Obrigado pelo seu feedback!
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Awesome!
Completion rate improved to 6.25
Automate Workflows using Step Functions
Deslize para mostrar o menu
Welcome to this lesson on automating workflows using AWS Step Functions. In this chapter, you'll learn how to coordinate distributed tasks into resilient, visible, and fault-tolerant workflows using Step Functions. You'll also explore a CLI-based demonstration that ties together Lambda functions orchestrated by a Step Function state machine.
What Are Step Functions?
AWS Step Functions is a fully managed service that lets you define application workflows as state machines in a visual and declarative way. These workflows can coordinate Lambda functions, ECS tasks, Batch jobs, Glue jobs, and other services without writing orchestration logic in your application code. Key advantages include built-in fault tolerance, retries, error handling, branching, parallel execution, and detailed execution history.
Think of a Step Function as a flowchart for your application—each step represents a task, choice, or wait state. Your application moves from one state to another based on transitions you define, following a predictable and observable path.
Why Use Step Functions?
Instead of building custom orchestration logic (e.g. retries, chaining, or branching) inside your Lambda or backend code, Step Functions can:
- Manage retries with exponential backoff;
- Track and store execution history;
- Integrate seamlessly with AWS Lambda, SQS, SNS, DynamoDB, Athena, ECS, and many other services;
- Separate business logic (inside Lambda) from orchestration logic (inside the state machine);
- Provide observability and auditability through logs and visualizations in the console.
This leads to cleaner, modular, and resilient cloud-native applications.
Components of a State Machine
An AWS Step Function state machine is defined using JSON (or YAML) following the Amazon States Language (ASL) specification; Its core components are:
- States: the tasks or steps (like calling a Lambda function);
- Transitions: how the workflow moves from one state to another;
- StartAt: the entry point of the workflow;
- End or Next: indicates where the workflow finishes or proceeds;
- Retry / Catch / TimeoutSeconds: built-in error handling and execution control;
- Choice / Parallel / Wait: advanced control structures for conditional logic and concurrency.
This makes it simple to model even complex workflows without introducing procedural complexity.
Building a Simple Step Function Workflow
Let's now walk through how to build a basic workflow using Step Functions and the AWS CLI.
This example ties together two Lambda functions: one prints "hello"
and the other prints "world"
.
We'll define a state machine that executes both in sequence and then inspect the result.
Step-by-Step Workflow
1. Create the Lambda Functions
Start by creating two simple Lambda functions, then zip them for deployment:
hello.py
: returns the string"Hello"
;world.py
: returns the string"World"
.
Zip each function into a deployable package.
2. Create a Lambda Execution Role
Create an IAM role for Lambda execution with the following permissions:
- Allow the Lambda service to assume the role;
- Attach the AWSLambdaBasicExecutionRole managed policy;
- This policy is essential to let Lambda write logs to CloudWatch.
Without this policy, the Lambda function won't execute properly.
4. Create the Step Function Execution Role
Create an IAM role for the Step Functions service with the following:
- An assume-role policy for
states.amazonaws.com
; - Attach a custom inline policy that allows the state machine to invoke your Lambda functions.
IAM is a critical part of Step Functions. The workflow won't execute unless Step Functions is permitted to call the resources in your tasks.
5. Define the State Machine
Create a JSON definition file (hello-world-definition.json
) with the following:
{
"Comment": "A simple Hello World step function",
"StartAt": "HelloState",
"States": {
"HelloState": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:HelloFunction",
"Next": "WorldState"
},
"WorldState": {
"Type": "Task",
"Resource": "arn:aws:lambda:region:account-id:function:WorldFunction",
"End": true
}
}
}
- The workflow starts at
HelloState
; - Executes the first Lambda, then transitions to
WorldState
; WorldState
runs the second Lambda and ends the workflow.
6. Deploy the State Machine
Run the following command to create the state machine:
aws stepfunctions create-state-machine \
--name HelloWorldStateMachine \
--role-arn arn:aws:iam::account-id:role/StepFunctionExecutionRole \
--definition file://hello-world-definition.json
This deploys the workflow into AWS Step Functions.
7. Start an Execution
Trigger the state machine using:
aws stepfunctions start-execution \
--state-machine-arn arn:aws:states:region:account-id:stateMachine:HelloWorldStateMachine \
--name DemoExecution
This launches a new run of the workflow named "DemoExecution"
.
Executions are uniquely identified and independently tracked.
8. Inspect the Execution
Use DescribeExecution
to retrieve execution status and metadata:
aws stepfunctions describe-execution \
--execution-arn <your-execution-arn>
This command shows:
- Execution start and end times;
- Status (e.g.
SUCCEEDED
,FAILED
); - Input and output data;
- Execution name and ARN.
9. View Execution History
Use GetExecutionHistory
to view a detailed log of all events:
aws stepfunctions get-execution-history \
--execution-arn <your-execution-arn>
This provides:
- State transitions (e.g. entered/exited states);
- Lambda function invocation details;
- Timestamps and error details if any;
- Useful for debugging and audit trails.
Key Exam Tips
For the AWS certification exam, be sure to:
- Understand the JSON structure of Step Functions state machines;
- Know how Step Functions integrate with Lambda, especially permissions and role configurations;
- Remember that Step Functions require IAM roles with specific permissions to call resources like Lambda;
- Understand how retries and error handling are configured within the state machine definition;
- Be familiar with how to view execution history using the CLI and console.
You've now built and executed a simple yet powerful orchestrated workflow using AWS Step Functions; This architecture is suitable for building robust, maintainable, and cloud-native workflows that respond to business logic with precision and visibility.
Let's now move forward to explore more advanced orchestration techniques.
Obrigado pelo seu feedback!