简体   繁体   English

将堆栈标记传递给Cloudformation中的嵌套堆栈

[英]Pass stack tags to nested stack in Cloudformation

I'm easily able to pass parameters to a Nested Cloudformation Stack using AWS::CloudFormation::Stack , including referenced values: 我可以使用AWS::CloudFormation::Stack将参数传递给嵌套的Cloudformation AWS::CloudFormation::Stack ,包括引用的值:

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

But I'm not able to find any documentation how to pass the Stack tags applied to the parent stack down to the child (nested) stack. 但我无法找到任何文档如何将应用于父堆栈的Stack标记传递给子(嵌套)堆栈。

The original stack was called by: 原始堆栈被调用:

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

The Environment name and Name tags get applied to resources in the original stack, such as instances, but not to resources in the nested stack nor the nested stack itself. Environment nameName标签应用于原始堆栈中的资源(如实例),但不应用于嵌套堆栈中的资源,也不应用于嵌套堆栈本身。

AWS have implemented propagation of stack tags to child stacks. AWS已实现将堆栈标记传播到子堆栈。 I can't find an announcement or documentation reflecting this change, but it now works. 我无法找到反映此更改的公告或文档,但现在可以使用了。

The AWS CloudFormation Resource Tags Type page states: AWS CloudFormation资源标记类型页面指​​出:

All stack-level tags, including automatically created tags, are propagated to resources that AWS CloudFormation supports. 所有堆栈级标记(包括自动创建的标记)都会传播到AWS CloudFormation支持的资源。

In the below example Parent/Child stack templates the Stack Tags on the parent propagate to the EC2 instances in the parent stack, the child stack, the EC2 instance in the child stack. 在下面的示例父/子堆栈模板中,父对象上的堆栈标记传播到父堆栈中的EC2实例,子堆栈,子堆栈中的EC2实例。

Note: EC2 tags still don't propagate to volumes created from block device mappings. 注意:EC2标记仍然不会传播到从块设备映射创建的卷。

Parent Stack Example 父堆栈示例

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

Child Stack Example 儿童堆栈示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM