【问题标题】:How can I check if a resource was created by CloudFormation?如何检查资源是否由 CloudFormation 创建?
【发布时间】:2018-08-22 18:42:51
【问题描述】:

我继承了一个拥有大量资源的 AWS 账户。其中一些是手动创建的,另一些是由 CloudFormation 创建的。

如何检查资源(在我的情况下为安全组)是否由 CloudFormation 创建并属于堆栈?

对于某些安全组aws ec2 describe-security-groups --group-ids real_id 会导致:

...
"Tags": [
            {
                "Value": "REAL_NAME",
                "Key": "aws:cloudformation:logical-id"
            },
            {
                "Value": "arn:aws:cloudformation:<REAL_ID>",
                "Key": "aws:cloudformation:stack-id"
            },
]
...

其他安全组没有任何标签。

它是唯一的指标吗?我的意思是,有人可以轻松地从 CloudFormation 创建的 SG 中删除标签。

【问题讨论】:

    标签: amazon-cloudformation


    【解决方案1】:

    根据official documentation,除了您定义的任何标签外,AWS CloudFormation 会自动创建以下堆栈级标签,前缀为 aws::

    aws:cloudformation:逻辑 ID

    aws:cloudformation:stack-id

    aws:cloudformation:堆栈名称

    所有堆栈级标签(包括自动创建的标签)都会传播到 AWS CloudFormation 支持的资源。 目前,标签不会传播到通过块储存设备映射创建的 Amazon EBS 卷。

    --

    这应该是一个很好的起点,但由于 CF 不强制堆栈状态,所以如果有人手动删除了某些内容,那么你永远不会知道。

    如果我是你,我会通过 Cloudformer 导出所有内容(支持)并按照我的方式重新设计整个设置。

    另一种方式:

    您可以将资源的PhysicalResourceId传递给describe_stack_resources,如果它属于CF堆栈,则可以获取堆栈信息。这是一个例子:

    cf = boto3.client('cloudformation') cf.describe_stack_resources(PhysicalResourceId="i-0xxxxxxxxxxxxxxxx")

    https://boto3.readthedocs.io/en/latest/reference/services/cloudformation.html#CloudFormation.Client.describe_stack_resources

    【讨论】:

    • 那么如果没有标签就无法判断资源是否由CF管理?
    • 也没有为 CMK 创建 cloudformation 标签
    猜你喜欢
    • 2021-10-30
    • 2020-01-04
    • 2020-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2017-03-01
    • 2017-06-12
    • 2019-07-26
    相关资源
    最近更新 更多