Cloudformation - 无法导入资源

Cloudformation - Unable to Import resource

我正在创建 Step Functions 并想在 cloudformation 代码中引用 Lambda 函数。 lambda 已经从单独的堆栈创建,并从该堆栈导出为 LambdaA

当我尝试将 LambdaA 导入我的步骤函数代码时,我 运行 遇到了问题。

这是我的 cloudformation 片段。

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",
            ...
            ...

我在 Cloudformation 控制台中收到以下错误:

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

知道这里可能出了什么问题吗?

您不能在 Fn::Sub 函数中使用其他内部函数。但是 Fn::Sub 提供了一种方法来解决这个问题。它的工作方式有点像 format 函数在其他编程语言中的工作方式。这是您的特定案例的示例:

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