Menu
AWS Step Functions
Developer Guide

Creating a Lambda State Machine Using AWS CloudFormation

This tutorial shows you how to create a basic AWS Lambda function and start a state machine execution automatically. You will use the AWS CloudFormation console and a YAML template to create the stack (IAM roles, the Lambda function, and the state machine). You will then use the AWS Step Functions console to start the state machine execution. For more information, see Working with CloudFormation Templates and the AWS::StepFunctions::StateMachine resource in the AWS CloudFormation User Guide.

Step 1: Setting Up Your AWS CloudFormation Template

Before you use the example YAML template, you should understand its separate parts.

To create an IAM role for Lambda

Define the trust policy associated with the IAM role for the Lambda function.

LambdaExecutionRole:
  Type: "AWS::IAM::Role"
  Properties:
    AssumeRolePolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Effect: Allow
          Principal:
            Service: lambda.amazonaws.com
          Action: "sts:AssumeRole"

To create a Lambda function

Define the following properties of the Lambda function which prints the message Hello World.

Important

Ensure that your Lambda function is under the same AWS account as your state machine.

MyLambdaFunction:
  Type: "AWS::Lambda::Function"
  Properties:
    Handler: "index.handler"
    Role: !GetAtt [ LambdaExecutionRole, Arn ]
    Code:
      ZipFile: |
        exports.handler = (event, context, callback) => {
            callback(null, "Hello World!");
        };
    Runtime: "nodejs4.3"
    Timeout: "25"

To create an IAM role for the state machine execution

Define the trust policy associated with the IAM role for the state machine execution.

StatesExecutionRole:
  Type: "AWS::IAM::Role"
  Properties:
    AssumeRolePolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Effect: "Allow"
          Principal:
            Service:
              - !Sub states.${AWS::Region}.amazonaws.com
          Action: "sts:AssumeRole"
    Path: "/"
    Policies:
      - PolicyName: StatesExecutionPolicy
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Action:
                - "lambda:InvokeFunction"
              Resource: "*"

To create a Lambda state machine

Define the Lambda state machine.

MyStateMachine:
  Type: "AWS::StepFunctions::StateMachine"
  Properties:
    DefinitionString:
      !Sub
        - |-
          {
            "Comment": "A Hello World AWL example using an AWS Lambda function",
            "StartAt": "HelloWorld",
            "States": {
              "HelloWorld": {
                "Type": "Task",
                "Resource": "${lambdaArn}",
                "End": true
              }
            }
          }
        - {lambdaArn: !GetAtt [ MyLambdaFunction, Arn ]}
    RoleArn: !GetAtt [ StatesExecutionRole, Arn ]

Step 2: Using the AWS CloudFormation Template to Create a Lambda State Machine

After you understand the different parts of the AWS CloudFormation template, you can put them together and use the template to create a AWS CloudFormation stack.

To create the Lambda state machine

  1. Copy the following example YAML data to a file named MyStateMachine.yaml.

    Copy
    AWSTemplateFormatVersion: "2010-09-09" Description: "An example template with an IAM role for a Lambda state machine." Resources: LambdaExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: "sts:AssumeRole" MyLambdaFunction: Type: "AWS::Lambda::Function" Properties: Handler: "index.handler" Role: !GetAtt [ LambdaExecutionRole, Arn ] Code: ZipFile: | exports.handler = (event, context, callback) => { callback(null, "Hello World!"); }; Runtime: "nodejs4.3" Timeout: "25" StatesExecutionRole: Type: "AWS::IAM::Role" Properties: AssumeRolePolicyDocument: Version: "2012-10-17" Statement: - Effect: "Allow" Principal: Service: - !Sub states.${AWS::Region}.amazonaws.com Action: "sts:AssumeRole" Path: "/" Policies: - PolicyName: StatesExecutionPolicy PolicyDocument: Version: "2012-10-17" Statement: - Effect: Allow Action: - "lambda:InvokeFunction" Resource: "*" MyStateMachine: Type: "AWS::StepFunctions::StateMachine" Properties: DefinitionString: !Sub - |- { "Comment": "A Hello World AWL example using an AWS Lambda function", "StartAt": "HelloWorld", "States": { "HelloWorld": { "Type": "Task", "Resource": "${lambdaArn}", "End": true } } } - {lambdaArn: !GetAtt [ MyLambdaFunction, Arn ]} RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
  2. Log in to the AWS CloudFormation console and choose Create Stack.

  3. On the Select Template page, select Upload a template to Amazon S3. Choose your MyStateMachine.yaml file, and then choose Next.

  4. On the Specify Details page, for Stack name, type MyStateMachine, and then choose Next.

  5. On the Options page, choose Next.

  6. On the Review page, choose I acknowledge that AWS CloudFormation might create IAM resources. and then choose Create.

    AWS CloudFormation begins to create the MyStateMachine stack and displays the CREATE_IN_PROGRESS status. When the process is complete, AWS CloudFormation displays the CREATE_COMPLETE status.

  7. (Optional) To display the resources in your stack, select the stack and choose the Resources tab.

Step 3: Starting a State Machine Execution

After you create your Lambda state machine, you can start an execution.

To start the state machine execution

  1. Log in to the Step Functions console and choose the name of the state machine that you created using AWS CloudFormation.

  2. On the MyStateMachine-ABCDEFGHIJ1K page, choose New execution.

  3. At the bottom of the New execution page, choose Start Execution.

    The state machine is executed and Step Functions displays the Succeeded status.