[英]Passing parameters of type List<AWS::EC2::Subnet::Id> to nested CloudFormation template
I'm trying to nest a CloudFormation template into another by using the AWS::CloudFormation::Stack
resource type.我正在尝试使用
AWS::CloudFormation::Stack
资源类型将一个 CloudFormation 模板嵌套到另一个模板中。 The nested template has a parameter of type List<AWS::EC2::Su.net::Id>
.嵌套模板有一个类型为
List<AWS::EC2::Su.net::Id>
的参数。
Individually, the nested template runs just fine.单独地,嵌套模板运行得很好。 But when embedding AWS Console says Encountered unsupported property ELBSu.netList .
但是当嵌入 AWS 控制台时,会显示 Encountered unsupported property ELBSu.netList 。
Changing the parameter's type to String/CommaSeparated list seems to be a workaround, as dicussed here but I'd loose the fancy UI when creating the template interactivly via AWS Console.将参数的类型更改为 String/CommaSeparated 列表似乎是一种解决方法,正如此处讨论的那样,但在通过 AWS 控制台交互式创建模板时,我会放弃花哨的 UI。
Do you have any idea how to pass the list of su.net ids as a paramter?你知道如何将 su.net id 列表作为参数传递吗?
This is the embedded template:这是嵌入式模板:
{
"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"
}
}
}
}
}
And the template that embedds:以及嵌入的模板:
{
"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" }
}
}
}
}
}
Successfully built in YAML using the following excerpt:使用以下摘录在 YAML 中成功构建:
Parameters:
pSubnetIDs:
Description: The array of Subnet IDs for the Subnet group
Type: List<AWS::EC2::Subnet::Id>
Resources:
rDBSubnetGroup:
Type: "AWS::RDS::DBSubnetGroup"
Properties:
DBSubnetGroupDescription: The subnet group for the RDS instance
SubnetIds: !Ref pSubnetIDs
I tried a bunch of variations of !Join and !Ref with no success.我尝试了一堆 !Join 和 !Ref 的变体,但没有成功。 Turns out it is just straightforward !Ref of the list.
事实证明它很简单! Ref 列表。
In YAML you need to "split" the Subnets by using Select.在 YAML 中,您需要使用 Select 来“拆分”子网。 For example with two subnets :
例如有两个子网:
!Join [",", [!Select [0, !Ref Subnets], !Select [1, !Ref Subnets]]]
Lists can be converted into Strings and vice versa.列表可以转换为字符串,反之亦然。 So the working invocation is
所以工作调用是
{
"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" }]}
}
}
}
}
}
To convert a list of SubnetIds to a list of Strings, use a combination of both JOIN and SPLIT .要将 SubnetId 列表转换为字符串列表,请结合使用JOIN和SPLIT 。
TLDR; TLDR;
In YAML, add在 YAML 中,添加
!Split [',', !Join [',', !Ref SubnetIds]]
The full answer is 2 parts.完整的答案是 2 部分。
Here SubnetIds
is a list of type Subnet.Id
.这里
SubnetIds
是类型为Subnet.Id
的列表。 JOIN
will combine all IDs to be one string. JOIN
会将所有 ID 组合为一个字符串。 For example, a list of subnet ids JOIN
ed with ,
as the delimiter will be as follows:例如,子网 id 列表
JOIN
ed with ,
作为分隔符将如下所示:
[abc, def, hij]
=> "abc,def,hij"
. [abc, def, hij]
=> "abc,def,hij"
。
Now let's take the output from part 1, and SPLIT
on the delimiter ,
.现在让我们获取第 1 部分的输出,并在分隔符
,
上进行SPLIT
。
"abc,def,hij"
=> ["abc", "def", "hij"]
"abc,def,hij"
=> ["abc", "def", "hij"]
Here is an example of my use case with creating a Scheduled Task:这是我创建计划任务的用例示例:
AWSTemplateFormatVersion: '2010-09-09'
Parameters
SubnetIds:
Type: List<AWS::EC2::Subnet::Id>
Description: Select at least two subnets in your selected VPC.
ScheduleTask:
Type: AWS::Events::Rule
Properties:
Description: !Sub 'Trigger Sitemap Generation according to the specified schedule'
ScheduleExpression: !Ref CronExpression
State: ENABLED
Targets:
- Id: 'targetId'
Arn: !GetAtt ECSCluster.Arn
RoleArn: !GetAtt ECSEventsRole.Arn
EcsParameters:
TaskDefinitionArn: !Ref TaskDefinition
TaskCount: 1
LaunchType: 'FARGATE'
PlatformVersion: 'LATEST'
NetworkConfiguration:
AwsVpcConfiguration:
AssignPublicIp: ENABLED
SecurityGroups:
- !Ref SecurityGroup
Subnets: !Split [',', !Join [',', !Ref SubnetIds]]
Maybe help to another person: but for me works this:也许对另一个人有帮助:但对我来说是这样的:
In child template:在子模板中:
SubnetIds:
Description: Choose which subnets should be deployed to
Type: List<AWS::EC2::Subnet::Id>
but in the parent template但在父模板中
SubnetIds:
Description: Choose which subnets should be deployed to
Type: CommaDelimitedList
Stack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: {s3}
Parameters:
SubnetIds: !Join [',', !Ref SubnetIds]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.