如何确定AWS资源所属的CloudFormation堆栈?

kol*_*nos 7 amazon-web-services aws-cloudformation boto3

boto3中是否存在可靠的方法来确定AWS资源所属的CloudFormation堆栈?还是它完全属于一个堆栈?假设我有一个DynamoDB表或EC2实例,如何确定它属于哪个堆栈?用于CloudFormation的boto3 API在资源级别上变得非常模糊,因此它似乎出现了。任何帮助深表感谢。

小智 6

您还可以使用 AWS CLI 确定资源属于哪个堆栈:

aws cloudformation describe-stack-resources --physical-resource-id "resourceId"


hel*_*loV 1

是的。Boto3 CF 客户端有方法来获取您想要的信息。

cf = boto3.client('cloudformation'
stacks = cf.list_stacks(StackStatusFilter=['CREATE_COMPLETE'])['StackSummaries'] 
Run Code Online (Sandbox Code Playgroud)

将返回已完成堆栈的堆栈摘要。更改过滤器以满足您的需要。

获取堆栈的名称

names = [stack['StackName'] for stack in stacks]
Run Code Online (Sandbox Code Playgroud)

然后获取给定堆栈的所有堆栈资源

for name in names:
  resources = cf.describe_stack_resources(StackName=name)['StackResources']
Run Code Online (Sandbox Code Playgroud)