【问题标题】:Pass stack tags to nested stack in Cloudformation将堆栈标签传递给 Cloudformation 中的嵌套堆栈
【发布时间】:2015-02-21 05:05:30
【问题描述】:

我可以使用AWS::CloudFormation::Stack 轻松地将参数传递给嵌套 Cloudformation 堆栈,包括引用值:

"MyNestedStack" : {
    "Type" : "AWS::CloudFormation::Stack",
    "Condition" : "MyCondition",
    "Properties" : {
        "TemplateURL" : {
            "Fn::Join" : ["", ["https://mybucket.s3.amazonaws.com/", {
                "Ref" : "S3BucketLocation"
            }, "/MyNestedStack.template"]]
        },
        "Parameters": {
            "MyVPC" : {
                "Ref" : "VPC"
            },
            "MySubnet" : {
                "Ref" : "ManagementSubnet"
            },
            "MySubnetAZ" : {
                "Fn::GetAtt" : [ "ManagementSubnet", "AvailabilityZone" ]
            }
            "InstanceType" : "m3.large",
            "KeyName" : "MyKey",
        }
    }
}

但我找不到任何文档如何将应用于父堆栈的堆栈标记传递到子(嵌套)堆栈。

原始堆栈的调用者:

#Create Stack
aws cloudformation create-stack --parameters ${parms} --tags Key='Environment Name',Value=${name} Key=Name,Value=${env} --stack-name ${env} --template-url ${url}

Environment nameName 标记应用于原始堆栈中的资源,例如实例,但不适用于嵌套堆栈中的资源或嵌套堆栈本身。

【问题讨论】:

  • AWS 支持确认堆栈标签不会自动传播:您是绝对正确的,堆栈级标签不会从顶级堆栈传播到其嵌套堆栈。我们确实对此功能提出了功能要求,我肯定已经代表您添加了 +1。但是,与其他功能请求一样,我们无法确定何时启动此功能。
  • 如果其他人想要这个选项亚马逊已经提到通过支持案例提出请求的人越多(对该功能的 +1);此功能可能越快获得实施。
  • 同时,一种解决方法是运行一个创建后操作(在此示例中使用 PowerShell),该操作可以采用嵌套堆栈名称,并检索该堆栈的所有 AWS 资源。然后,您可以在结果中添加任何您需要的标签:$a = Get-EC2Tag -Filter @{Name="tag:aws:cloudformation:stack-name"; Value="MyStack-ChildStack-ZNJZH7EML46V"}; New-EC2Tag -Resources $a.ResourceId -Tags @(@{Key='Project'; Value='My Project'}, @{Key='Owner'; Value='Me'})

标签: json amazon-web-services tags nested amazon-cloudformation


【解决方案1】:

AWS 已将堆栈标签传播到子堆栈。我找不到反映此更改的公告或文档,但它现在可以使用。

AWS CloudFormation Resource Tags Type 页面声明:

所有堆栈级标签,包括自动创建的标签,都会传播到 AWS CloudFormation 支持的资源。

在下面的示例父/子堆栈模板中,父堆栈标签传播到父堆栈中的 EC2 实例、子堆栈、子堆栈中的 EC2 实例。

注意:EC2 标签仍然不会传播到从块储存设备映射创建的卷。

父堆栈示例

{
    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "Test Child Stack Tag Propagation (Parent Stack)",

    "Parameters" : {
        "KeyName": {
            "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
            "Type": "AWS::EC2::KeyPair::KeyName"
        },

        "Subnet": {
            "Type": "AWS::EC2::Subnet::Id"
        },

        "VPC": {
            "Type": "AWS::EC2::VPC::Id"
        },

        "AMI": {
            "Type": "AWS::EC2::Image::Id",
            "Default": "ami-f2210191"
        },

        "ChildTemplateUrl": {
            "Type" : "String"
        }
    },

    "Resources" : {
        "EC2Instance" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
                "InstanceType" : "t2.nano",
                "SecurityGroupIds" : [{"Ref" : "InstanceSecurityGroup"}],
                "SubnetId" : { "Ref" : "Subnet" },
                "KeyName" : { "Ref" : "KeyName" },
                "ImageId" : {"Ref": "AMI"}
            }
        },

        "InstanceSecurityGroup" : {
            "Type" : "AWS::EC2::SecurityGroup",
            "Properties" : {
                "GroupDescription" : "Enable SSH access via port 22",
                "VpcId" : { "Ref": "VPC"},
                "SecurityGroupIngress" : [ {
                    "IpProtocol" : "tcp",
                    "FromPort" : "22",
                    "ToPort" : "22",
                    "CidrIp" : "0.0.0.0/0"
                } ]
            }
        },

        "MyNestedStack" : {
            "Type" : "AWS::CloudFormation::Stack",
            "Properties" : {
                    "TemplateURL" : {"Ref": "ChildTemplateUrl"},
                    "Parameters": {
                            "Subnet" : {"Ref": "Subnet"},
                            "KeyName" : {"Ref": "KeyName"},
                            "AMI" : {"Ref": "AMI"},
                            "SecurityGroup": {"Ref" : "InstanceSecurityGroup"},
                            "VPC": {"Ref": "VPC"}
                    }
            }
        }
    },

    "Outputs" : {
        "InstanceId" : {
            "Description" : "InstanceId of the newly created EC2 instance",
            "Value" : { "Ref" : "EC2Instance" }
        },
        "IP" : {
            "Description" : "Private IP address of the newly created VPC EC2 instance",
            "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PrivateIp" ] }
        }
    }
}

子栈示例

{
    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "Test Child Stack Tag Propagation (Child Stack)",

    "Parameters" : {
        "KeyName": {
            "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
            "Type": "AWS::EC2::KeyPair::KeyName"
        },

        "Subnet": {
            "Type": "AWS::EC2::Subnet::Id"
        },

        "VPC": {
            "Type": "AWS::EC2::VPC::Id"
        },

        "AMI": {
            "Type": "AWS::EC2::Image::Id"
        },

        "SecurityGroup": {
            "Type": "AWS::EC2::SecurityGroup::Id"
        }
    },

    "Resources" : {
        "EC2Instance" : {
            "Type" : "AWS::EC2::Instance",
            "Properties" : {
                "InstanceType" : "t2.nano",
                "SecurityGroupIds" : [{"Ref" : "SecurityGroup"}],
                "SubnetId" : { "Ref" : "Subnet" },
                "KeyName" : { "Ref" : "KeyName" },
                "ImageId" : {"Ref": "AMI"}
            }
        }
    },

    "Outputs" : {
        "InstanceId" : {
            "Description" : "InstanceId of the newly created EC2 instance",
            "Value" : { "Ref" : "EC2Instance" }
        },
        "IP" : {
            "Description" : "Private IP address of the newly created VPC EC2 instance",
            "Value" : { "Fn::GetAtt" : [ "EC2Instance", "PrivateIp" ] }
        }
    }
}

【讨论】:

  • Tags 到底在哪里使用?我没有看到这个被使用docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/…
  • @Paolo 随堆栈创建命令 aws cloudformation create-stack 提供的标签。从 OP 的问题来看,他们是Key='Environment Name',Value=${name} Key=Name,Value=${env}
  • 我明白了,我错过了。我没有使用 CLI,我正在寻找一种将标签从父堆栈传递到子堆栈的方法。这可以通过使用 Tags 字段
  • 即使您使用的是控制台。您在 UI 中应用于父堆栈的标签将传递给子堆栈。这是实际 CFN 堆栈标签的传播。
  • 我使用的是 CloudFormation 模板
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 2020-06-28
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 2019-03-09
  • 2018-06-15
相关资源
最近更新 更多