0
0
AwsHow-ToBeginner · 3 min read

How to Create State Machine Step Functions in AWS

To create a state machine in AWS Step Functions, define your workflow using the Amazon States Language in JSON or YAML format, then deploy it via the AWS Management Console or AWS CLI using the create-state-machine command. This state machine controls the flow of tasks and decisions in your application.
📐

Syntax

A Step Functions state machine is defined using Amazon States Language, a JSON-based language. The main parts include:

  • StartAt: The state where execution begins.
  • States: A map of all states and their types (Task, Choice, Pass, etc.).
  • Type: Defines the state type.
  • Resource: The ARN of the AWS service or Lambda function to invoke.
  • End: Marks the end of the workflow.

You deploy the state machine using AWS CLI or Console by providing the definition and a role ARN.

json
{
  "Comment": "A simple AWS Step Functions state machine",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
      "End": true
    }
  }
}
💻

Example

This example creates a simple state machine that calls a Lambda function named HelloWorldFunction. It starts at the HelloWorld state, runs the Lambda, and ends.

json
{
  "Comment": "Example state machine calling Lambda",
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloWorldFunction",
      "End": true
    }
  }
}
Output
State machine created successfully with ARN arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorldStateMachine
⚠️

Common Pitfalls

  • Forgetting to assign the correct IAM role with permissions to execute Lambda or other services.
  • Incorrect ARN format for Lambda or other resources.
  • Missing End or Next fields causing invalid state machine definitions.
  • Using unsupported state types or syntax errors in JSON.

Always validate your JSON definition using the AWS Console or CLI before deployment.

json
Wrong example (missing End or Next):
{
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloWorldFunction"
    }
  }
}

Right example (with End):
{
  "StartAt": "HelloWorld",
  "States": {
    "HelloWorld": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:HelloWorldFunction",
      "End": true
    }
  }
}
📊

Quick Reference

FieldDescription
StartAtName of the state where execution starts
StatesMap of all states in the workflow
TypeType of state (Task, Choice, Pass, Wait, Succeed, Fail)
ResourceARN of the AWS service or Lambda function to invoke
NextName of the next state to transition to
EndBoolean to mark the end of the state machine

Key Takeaways

Define your state machine using Amazon States Language in JSON or YAML format.
Always specify a StartAt state and use End or Next to control flow.
Assign an IAM role with permissions for the services your state machine calls.
Validate your state machine definition before deployment to avoid syntax errors.
Deploy state machines via AWS Console or AWS CLI using the create-state-machine command.