繁体   English   中英

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

[英]How can I check if a resource was created by 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"
            },
]
...

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

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

根据官方文档 ,除您定义的任何标签外,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

我遇到过同样的问题。 在没有找到答案后,我做了一个快速的 PowerShell 脚本,它只会在所有堆栈中查找资源名称。
当 CF 被引入时,堆栈没有标记资源,即使是现在我对 CloudFormation 可靠地标记资源有问题,有时它仍然会标记一个资源而不标记另一个资源,即使资源类型相同且在同一堆栈中。 此外,CloudWatch Alarms 等一些资源没有标签。

$resourceName = "*MyResource*" #Part of the resource name, surrounded by asterisks (*)
$awsProfile = "Dev" #AWS Profile to use
$awsRegion = "us-east-1" #Region to query
Get-CFNStack -ProfileName $awsProfile -Region $awsRegion | Get-CFNStackResourceList -ProfileName $awsProfile -Region $awsRegion | Where-Object {$_.PhysicalResourceId -ilike $resourceName} | Select-Object StackName,PhysicalResourceId

暂无
暂无

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

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