How to Add Your Step Functions Inside Cloud Formation

I recently had to create a series of step functions for a project I am working on. The beauty of using step function is you get to work on it visually; from adding of functions, states, inputs and outputs up to real-time running and debugging.

It is awesome! However, after the wow factor, one thing that came to mind was “How am I going to get this inside a repository so it can be versioned?”. Turns out, I can actually add all of it inside a CloudFormation! Meaning in my application, I can create a YAML file, add it in there and it will generate the State Machine which has all of my step functions inside.

First I needed a policy so I can perform Step Function actions from within my application.


  Resources:
    MyAppRole:
      Type: AWS::IAM::Role
      Properties:
        AssumeRolePolicyDocument:
          Version: '2012-10-17'
          Statement:
            - Effect: Allow
              Principal:
                Service:
                  - states.amazonaws.com
              Action:
                - sts:AssumeRole
          - PolicyName: StepFunctions
            PolicyDocument:
              Version: '2012-10-17'
              Statement:
              - Effect: Allow
                Action:
                  - states:*
                Resource: '*'

Then I added the following inside my template.yaml file under Resources section:


  Resources:
    MyStateMachine:
      Type: AWS::StepFunctions::StateMachine
      Properties:
        StateMachineName: MyStateMachineName
        RoleArn: !GetAtt MyAppRole.Arn
        DefinitionString:
          Fn::Sub: |
            {
            ... STEP FUNCTION DEFINITION HERE
            }

Leave a Reply

Your email address will not be published. Required fields are marked *