AgentX
AgentX

Reputation: 1462

Cloudformation - Unable to Import resource

I am creating Step Functions and would like to reference a Lambda function in the cloudformation code. The lambda is already created from separate stack and is exported as LambdaA from that stack.

I am running into issue when I try to import LambdaA into my step function code.

Here is my cloudformation snippet.

ABCStateMachine:
Type: 'AWS::StepFunctions::StateMachine'
Properties:
  StateMachineName: 'AbcStateMachine_1.0'
  RoleArn: 
    Fn::GetAtt: [ AbcStateMachineRole, Arn ] 
  DefinitionString: 
    Fn::Sub:
      - |-
        {
          "StartAt": "DoStuff",
          "Version": "1.0",
          "States": {
            "DoStuff" : {
              "Type": "Task",
              "Comment": "Does some stuff.,
              "Resource": {"Fn::ImportValue": "LambdaA"}, # error here
              "Next": "IsStuffDone"
            },
            "IsStuffDone": {
              "Type": "Choice",
            ...
            ...

I get the following error in Cloudformation console:

Invalid State Machine Definition: 'SCHEMA_VALIDATION_FAILED at /DoStuff/Resource' (Service: AWSStepFunctions; Status Code: 400; Error Code: InvalidDefinition.

Any idea as to what might be wrong here?

Upvotes: 2

Views: 755

Answers (1)

Laurent Jalbert Simard
Laurent Jalbert Simard

Reputation: 6329

You can't use other intrinsic function inside Fn::Sub function. But Fn::Sub offers a way to solve this. It works a bit like a format function would work in other programming languages. Here's an exemple for your particular case:

ABCStateMachine:
Type: 'AWS::StepFunctions::StateMachine'
Properties:
  StateMachineName: 'AbcStateMachine_1.0'
  RoleArn: 
    Fn::GetAtt: [ AbcStateMachineRole, Arn ] 
  DefinitionString: 
    Fn::Sub:
      - |-
        {
          "StartAt": "DoStuff",
          "Version": "1.0",
          "States": {
            "DoStuff" : {
              "Type": "Task",
              "Comment": "Does some stuff.,
              "Resource": ${LambdaToImport}, # error here
              "Next": "IsStuffDone"
            }
            ...
          }
          ...
        }
      - LambdaToImport:
          Fn::ImportValue: LambdaA

Upvotes: 4

Related Questions