传递List类型的参数 嵌套的CloudFormation模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了传递List类型的参数 嵌套的CloudFormation模板相关的知识,希望对你有一定的参考价值。

我正在尝试使用AWS::CloudFormation::Stack资源类型将CloudFormation模板嵌套到另一个模板中。嵌套模板的参数类型为List<AWS::EC2::Subnet::Id>

单独地,嵌套模板运行得很好。但是当嵌入AWS Console时遇到遭遇不支持的属性ELBSubnetList。

将参数的类型更改为String / Comma Separated列表似乎是一种解决方法,正如qazxsw poi所讨论的那样,但是当通过AWS Console以交互方式创建模板时,我放弃了花哨的UI。

您是否知道如何将子网ID列表作为参数传递?

这是嵌入式模板:

here

以及嵌入的模板:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters" : {
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources" : {
        "ELB" : {
            "Type" : "AWS::ElasticLoadBalancing::LoadBalancer",
            "Properties" : {
                "Subnets": { "Ref": "ELBSubnetList" },
                "CrossZone" : "true",
                "SecurityGroups": { "Ref": "ELBSecurityGroupList" },
                "LBCookieStickinessPolicy" : [ {
                    "PolicyName" : "CookieBasedPolicy",
                    "CookieExpirationPeriod" : "30"
                }],
                "Listeners" : [ {
                    "LoadBalancerPort" : "80",
                    "InstancePort" : "80",
                    "Protocol" : "HTTP",
                    "PolicyNames" : [ "CookieBasedPolicy" ]
                } ],
                "HealthCheck" : {
                    "Target" : "HTTP:80/wordpress/wp-admin/install.php",
                    "HealthyThreshold" : "2",
                    "UnhealthyThreshold" : "5",
                    "Interval" : "10",
                    "Timeout" : "5"
                }
            }
        }
    }
}
答案

在YAML中,您需要使用Select来“拆分”子网。例如,有两个子网:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters": {
        "ChildTemplate": {
            "Type": "String",
            "Default": "https://s3.eu-central-1.amazonaws.com/cf-templates-xxxxxxxxxxx-eu-central-1/sample_child.template"
        },
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources": {
        "Child": {
            "Type": "AWS::CloudFormation::Stack",
            "Properties": {
                "TemplateURL": { "Ref": "ChildTemplate" },
                "Parameters": {
                    "ELBSubnetList": { "Ref": "ELBSubnetList" },
                    "ELBSecurityGroupList": { "Ref": "ELBSecurityGroupList" }
                }
            }
        }
    }
}
另一答案

列表可以转换为字符串,反之亦然。所以工作调用是

!Join [",", [!Select [0, !Ref Subnets], !Select [1, !Ref Subnets]]]
另一答案

使用以下摘录在YAML中成功构建:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Parameters": {
        "ChildTemplate": {
            "Type": "String",
            "Default": "https://s3.eu-central-1.amazonaws.com/cf-templates-xxxxxxxxxxx-eu-central-1/sample_child.template"
        },
        "ELBSubnetList" : {
            "Type" : "List<AWS::EC2::Subnet::Id>",
            "Description" : "Subnet List for Elastic Loadbalancer"
        },
        "ELBSecurityGroupList": {
            "Type": "List<AWS::EC2::SecurityGroup::Id>",
            "Description": "Security Group List for Elastic Loadbalancer"
        }
    },
    "Resources": {
        "Child": {
            "Type": "AWS::CloudFormation::Stack",
            "Properties": {
                "TemplateURL": { "Ref": "ChildTemplate" },
                "Parameters": {
                    "ELBSubnetList": {"Fn::Join": [",", { "Ref": "ELBSubnetList" }]},
                    "ELBSecurityGroupList": {"Fn::Join": [",", { "Ref": "ELBSecurityGroupList" }]}
                }
            }
        }
    }
}

我尝试了很多变化!加入和!Ref没有成功。事实证明它只是简单!参考列表。

以上是关于传递List类型的参数 嵌套的CloudFormation模板的主要内容,如果未能解决你的问题,请参考以下文章

Python参数传递(传值&传引用)

结构体(结构体嵌套结构体指针结构体参数传递)

一脚踩进java之基础篇34——可变参数Collection工具类集合嵌套

一脚踩进java之基础篇34——可变参数Collection工具类集合嵌套

PYTHON 参数传递

mybatis 集合、数组类型参数语法