gpt4 book ai didi

amazon-web-services - 如何在同一个CloudFormation模板中声明多个相似的资源组?

转载 作者:行者123 更新时间:2023-12-03 07:26:26 29 4
gpt4 key购买 nike

我有一个具有三层的服务,每层都有一个带有一些实例的自动缩放组。这些层(在启动配置、实例数量等方面)的不同之处在于由单个参数(“角色”)控制。因此,我使用如下参数调用模板:

{
...
"Parameters": {
...
"Role": {
"AllowedValues": [
"inbound",
"filter",
"outbound"
],
...
},
},
"Conditions" : {
"IsStaging" : { "Fn::Equals" : [ { "Ref": "Environment" }, "eu-staging"] },
"IsInbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "inbound"] },
"IsFilter" : { "Fn::Equals" : [ { "Ref": "Role" }, "filter"] },
"IsOutbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "outbound"] }
},
"Resources": {
...
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
...
"SecurityGroupEgress": [
{"Fn::If": ["IsFilter", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
],
"Tags": [
{
"Key": "Name",
"Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
},
...
],
}
},
}
}

现在我想在同一个 CloudFormation 模板中声明这三个层(以便它与现有管道作业透明地集成,并受益于 CloudFormation 功能,例如回滚)。此外,我想避免重写三倍资源,每一层一次,如下所示:

{
...
"Parameters": {
...
"Role": {
"AllowedValues": [
"inbound",
"filter",
"outbound"
],
...
},
},
"Conditions" : {
"IsStaging" : { "Fn::Equals" : [ { "Ref": "Environment" }, "eu-staging"] },
"IsInbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "inbound"] },
"IsFilter" : { "Fn::Equals" : [ { "Ref": "Role" }, "filter"] },
"IsOutbound" : { "Fn::Equals" : [ { "Ref": "Role" }, "outbound"] }
},
"Resources": {
...
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
...
"SecurityGroupEgress": [
{"Fn::If": ["IsInbound", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
],
"Tags": [
{
"Key": "Name",
"Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
},
...
],
}
},

...
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
...
"SecurityGroupEgress": [
{"Fn::If": ["IsFilter", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
],
"Tags": [
{
"Key": "Name",
"Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
},
...
],
}
},

...
"SecurityGroup": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
...
"SecurityGroupEgress": [
{"Fn::If": ["IsOutbound", { "CidrIp": "10.243.74.0/24", "IpProtocol": "tcp", "FromPort": "9094", "ToPort": "9094" }, { "Ref": "AWS::NoValue" } ]}
],
"Tags": [
{
"Key": "Name",
"Value": { "Fn::Join" : [ "-", [ "smtp-service-security-group", {"Ref": "Role"} ] ] }
},
...
],
}
},
}
}

如何才能做到这一点而不需要我为三层重写相同的代码?

最佳答案

没有 CloudFormation {CFN) macros 就无法做到这一点如果您只想使用单个模板。

处理重复的常用方法是使用 nested stacks 。这需要您将三个服务的通用代码放在一个单独的模板中,并且它们将它们包含在父模板中三次及其特定参数。

关于amazon-web-services - 如何在同一个CloudFormation模板中声明多个相似的资源组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71536641/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com