Cloudformation 内部函数 Fn::Sub 映射

Cloudformation 内部函数 Fn::Sub 映射

我不明白为什么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

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

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

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

答案1

您给该Fn::Sub函数提供了 3 个参数:

  1. 细绳
  2. 映射Lambda2
  3. 映射Lambda1

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

  DefinitionString: !Sub 
    - |-
      {
         [...]
      }
    - Lambda2: !GetAtt LambdaSubmitJob.Arn
      Lambda1: !GetAtt LambdaJobStatusPoll.Arn

希望有帮助:)

答案2

如果你在元数据中编写 bash 脚本,则示例代码中的选项 1 和 2 有效,但选项 3 无效,因为“- |”告诉子内部函数期望另一个“- |”如选项 1 所示,它应该提供值的映射(例如- {某种映射})替换里面的字符串{啦啦啦}当第二个映射没有找到时,会抛出错误。

Metadata:
  AWS::CloudFormation::Init:
   files:
   ##### use
    '/dir/subdir':
      content: !Sub
        - |
          {blablabla}
        - {A mapping of some sort}
    ###### or 

    '/dir2/subdir2':
      content: !Sub |
          {blablabla}

    ##### But don't mix like so

    '/dir3/subdir3':
      content: !Sub
        - |
          {blablabla}

相关内容