aws cloudformation 在列表中使用 Fn::Join

aws cloudformation use Fn::Join in a list

我有一个 cloudformation 模板,它使用由 lambda 函数支持的自定义资源。 lambda 函数的参数之一是字符串列表。我只有一项要传递到列表中,并且想使用 Fn:Join 来连接创建字符串。但是,使用 Fn::Join 会产生错误,因为它会导致 json 无效。感谢任何输入。

"Subscriptions": ["Fn::Join": [":", ["a", "b", "c"]]]

A client error (ValidationError) occurred when calling the CreateStack operation : Template format error: JSON not well-formed.

Cloudformation 片段:-

  "Resources": {
"MyCustomRes": {
      "Type": "Custom::CustomResource",
      "Properties": {
        "ServiceToken": { "Fn::Join": [ "", [
                                        "arn:aws:lambda:",
                                        { "Ref": "AWS::Region" },
                                        ":",
                                        { "Ref": "AWS::AccountId" },
                                        ":function:LambdaFn"
                                      ] ] },
        "Version": 1,
        "ResourceName": { "Ref": "ResourceName" },
        "Subscriptions"       : [ "Fn::Join": [ "", [
                                        "arn:aws:sns:",
                                        { "Ref": "AWS::Region" },
                                        ":",
                                        { "Ref": "AWS::AccountId" },
                                        ":Topic1"
                                      ] ] ]
    }
}     },

用于为 Subscriptions 属性 构建值的 Fn::Join Intrinsic Function 必须是对象而不是数组。

声明像 ['Fn::Join' : [...]] 这样的数组是无效的 JSON 语法,它必须采用 {"Fn::Join" : [...]}

形式

文档将语法描述为

{ "Fn::Join" : [ "delimiter", [ comma-delimited list of values ] ] }

因此您的 CloudFormation 模板应使用以下内容

{
  "Subscriptions": {
    "Fn::Join": [
      ":",
      [
        "arn:aws:sns",
        {
          "Ref": "AWS::Region"
        },
        {
          "Ref": "AWS::AccountId"
        },
        "Topic1"
      ]
    ]
  }
}

存在使用 Fn::Sub Intrinsic Function.

构建 ARN 的更具可读性的解决方案
{
  "Fn::Sub": [
    "arn:${AWS::Partition}:sns:${AWS::Region}:${AWS::AccountId}:Topic1"
  ]
}

我来到这里是为了在 YAML 文件中寻找相同的语法。令我震惊的是需要有两个参数列表:一个包含 2 个要加入的项目的列表,第二个是一个列表本身。完整的 YAML 语法如下所示:

  SourceArn: 
    Fn::Join: 
    - ""
    - - 'arn:aws:execute-api:'
      - !Ref AWS::Region
      - ':'
      - !Ref AWS::AccountId
      - ':'
      - !Ref ApiGatewayRestApiResource
      - '/*'