如何检查资源是否由 CloudFormation 创建?

Paw*_*wel 6 aws-cloudformation

我继承了一个拥有大量资源的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"
            },
]
...
Run Code Online (Sandbox Code Playgroud)

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

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

bha*_*hia 10

根据官方文档,除了您定义的任何标签之外,AWS CloudFormation 还会自动创建以下带有前缀 aws:: 的堆栈级标签:

aws:cloudformation:逻辑 ID

aws:cloudformation:堆栈 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