Search code examples
amazon-web-servicesaws-cloudformationaws-step-functions

Using Ref for Resource in Step function inside cloudformation template


I have a step function inside cloudformation. The cloudformation stack also create Lambdas which i will use as resource in step function. I have something like

TestLambda:
  Type: "AWS::Lambda::Function"
  Properties:
    Handler: "test_lambda.lambda_handler"
    Role: "arn:aws:iam::1234342334:role/Lambda"
    Code:
      ZipFile: !Sub |
        from __future__ import print_function
        import boto3
        def lambda_handler(event, context):
          print(event)
    Runtime: "python2.7"

  ....

TestStateMachine:
  Type: AWS::StepFunctions::StateMachine
  Properties:
    StateMachineName: "Test"
    DefinitionString: |-
      {
        "StartAt": "State1",
        "States": {
          "State1" : {
            "Type" : "Task",
            "Resource" : "${!GetAtt TestLambda.Arn}",
            "Next": "State2?"
          },
      ... 
      ...

all inside one cloudformation template.

"SCHEMA_VALIDATION_FAILED: Value is not a valid resource ARN"

I also tried !GetAtt TestLambda.Arn, it didn't work. I want the lambda and stepfunction created inside the single cloudformation template. Please let me know if there is better, cleaner way of doing it.

Thank you


Solution

  • You should use Fn::Sub function for that:

    TestStateMachine:
      Type: AWS::StepFunctions::StateMachine
      Properties:
        StateMachineName: "Test"
        DefinitionString: 
          Fn::Sub:
            |-
              {
                "StartAt": "State1",
                "States": {
                  "State1" : {
                    "Type" : "Task",
                    "Resource" : "${TestLambda.Arn}",
                    "Next": "State2?"
              },