CloudFormation模板错误Fn :: Sub内部函数未指定期望的参数

时间:2019-04-15 03:25:11

标签: python amazon-cloudformation

下面处理Lambda函数的Cloudformation模板的部分。期望我可以使用Sub函数在堆栈创建过程中添加用户提供的SNS主题。

"GetApiKeyValueLambdaFunction": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
      "Code": {
        "ZipFile": {"Fn::Sub":[
            {
                "Fn::Join": ["\n", [
                  "import json",
                  "import boto3",
                  "client = boto3.client('apigateway')",
                  "sns_client = boto3.client('sns')",
                  "def lambda_handler(event, context):",
                  "    response = client.get_api_key(",
                  "        apiKey=event['apiKeyId'],",
                  "        includeValue=True",
                  "    )",
                  "    response_message = {'key_name' : response['name'], 'key_value' : response['value']}",
                  "    sns_client.publish(",
                  "        TargetArn='/${__snstopic__}',",
                  "        Message=json.dumps({'default': json.dumps(response_message,indent=1)}),",
                  "        MessageStructure='json'",
                  "    )",
                  "    return {",
                  "        'statusCode': 200,",
                  "        'body': response['value']}"
                          ]   ] 
                      },
                {
                    "__snstopic__":{"Ref" : "SnsOutputTopic"}
                }
                ]}
      },
      "Handler": "index.lambda_handler",
      "Runtime": "python3.6",
      "Timeout": 30,
      "Role": {
        "Fn::GetAtt": [
          "CustomLambdaExecutionRole", "Arn"
        ]
      }
    }
}

但是模板验证失败,并出现以下错误:

  

4/15/2019,1:20:22 PM-模板包含错误。:模板错误:   一个或多个Fn :: Sub内部函数未指定预期的   论点。指定一个字符串作为第一个参数,第二个可选参数   参数,用于指定要在字符串中替换的值的映射

我想知道是否有确定的顺序才能一起使用Fn Sub和Fn Join?但是我在这里有点迷路了。

2 个答案:

答案 0 :(得分:0)

无需使用sub并连接在一起。这是一个代码段(我刚刚将其转换为yaml)

GetApiKeyValueLambdaFunction:
  Type: AWS::Lambda::Function
  Properties:
    Code:
      ZipFile:
        !Sub |
          import json, boto3
          client = boto3.client('apigateway')
          sns_client = boto3.client('sns')
          def lambda_handler(event, context):
            response = client.get_api_key(apiKey=event['apiKeyId'], includeValue=True)
            response_message = {'key_name' : response['name'], 'key_value': response['value']}
            sns_client.publish( TargetArn='/${! Ref SnsOutputTopic}', Message=json.dumps({'default': json.dumps(response_message,indent=1)}), MessageStructure='json')
            return { 'statusCode': 200, 'body': response['value'] }
    Handler: index.lambda_handler
    Runtime: python3.6
    Timeout: 30
    Role:
      Fn::GetAtt:
      - CustomLambdaExecutionRole
      - Arn

答案 1 :(得分:0)

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

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}