【问题标题】:aws cloudformation use Fn::Join in a listaws cloudformation 在列表中使用 Fn::Join
【发布时间】:2017-03-01 14:16:31
【问题描述】:

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

“订阅”:[“Fn::Join”:[“:”,[“a”,“b”,“c”]]]

调用 CreateStack 时发生客户端错误(ValidationError) 操作:模板格式错误:JSON 格式不正确。

Cloudformation sn-p:-

  "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"
                                      ] ] ]
    }
}     },

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

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

    使用 ['Fn::Join' : [...]] 之类的数组是无效的 JSON 语法,而它必须是 {"Fn::Join" : [...]} 的形式

    文档将语法描述为

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

    因此,您的 Cloud Formation 模板应使用以下内容

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

    【讨论】:

    • “ref”又做了什么?
    【解决方案2】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多