Cloudformation 内在函数 Fn::Sub 映射

tit*_*tus 5 amazon-web-services amazon-cloudformation

我不明白为什么Fn::Sub在这个模板中不起作用。我收到以下错误:

模板包含错误。:模板错误:一个或多个 Fn::Sub 内部函数未指定预期参数。指定一个字符串作为第一个参数,并指定一个可选的第二个参数来指定要在字符串中替换的值的映射

Resources:
  LambdaSubmitJob:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.lambda_handler
      Runtime: python2.7
      Timeout: 10
      Code:
        ZipFile: |
          import json
          import boto3
  LambdaJobStatusPoll:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.lambda_handler
      Runtime: python2.7
      Timeout: 10
      Code:
        ZipFile: |
          import json
          import boto3
  MyStepF:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      DefinitionString: !Sub 
        - |-
          {
            "Comment": "A state machine that submits a Job to AWS Batch and monitors the Job until it completes.",
            "StartAt": "Submit Job",
            "States": {
              "Submit Job": {
                "Type": "Task",
                "Resource": "${Lambda1}",
                "ResultPath": "$.guid",
                "Next": "Wait 30 seconds"
              },
              {
                "Type": "Task",
                "Resource": "${Lambda2}",
                "ResultPath": "$.guid",
                "Next": "Wait 30 seconds"
              }
            }
          }
        - Lambda2: !GetAtt 
            - LambdaSubmitJob
            - Arn
        - Lambda1: !GetAtt 
            - LambdaJobStatusPoll
            - Arn
Run Code Online (Sandbox Code Playgroud)

但是如果我只有一个映射,那么它就可以工作。

Resources:
  LambdaSubmitJob:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.lambda_handler
      Runtime: python2.7
      Timeout: 10
      Code:
        ZipFile: |
          import json
          import boto3
  LambdaJobStatusPoll:
    Type: 'AWS::Lambda::Function'
    Properties:
      Handler: index.lambda_handler
      Runtime: python2.7
      Timeout: 10
      Code:
        ZipFile: |
          import json
          import boto3
  MyStepF:
    Type: 'AWS::StepFunctions::StateMachine'
    Properties:
      DefinitionString: !Sub 
        - |-
          {
            "Comment": "A state machine that submits a Job to AWS Batch and monitors the Job until it completes.",
            "StartAt": "Submit Job",
            "States": {
              "Submit Job": {
                "Type": "Task",
                "Resource": "${Lambda1}",
                "ResultPath": "$.guid",
                "Next": "Wait 30 seconds"
              }
        - Lambda1: !GetAtt 
            - LambdaJobStatusPoll
            - Arn
Run Code Online (Sandbox Code Playgroud)

我正在使用 CloudFormation Designer 来验证这两个示例。

MLu*_*MLu 5

你给Fn::Sub函数 3 个参数:

  1. 字符串
  2. 映射为 Lambda2
  3. 映射为 Lambda1

将两个映射移动到单个列表项,它将起作用(为了简单起见,我还使用了“点符号”!GetAtt,但这是可选的)。

  DefinitionString: !Sub 
    - |-
      {
         [...]
      }
    - Lambda2: !GetAtt LambdaSubmitJob.Arn
      Lambda1: !GetAtt LambdaJobStatusPoll.Arn
Run Code Online (Sandbox Code Playgroud)

希望有帮助:)