问题
Is there a reliable way in boto3 to determine what CloudFormation stack an AWS resource belongs to? Or if it belongs to a stack at all? Say I have a DynamoDB table or an EC2 instance, how do I find out what stack it is a member of? The boto3 API for CloudFormation gets pretty vague at the resource level, or so it appears. Any help is much appreciated.
回答1:
You can pass PhysicalResourceId
of a resource to desribe_stack_resources
and get the stack information if it belongs to a CF stack
To find an EC2 host for example
cf = boto3.client('cloudformation')
cf.describe_stack_resources(PhysicalResourceId="i-07bd92638b049ccc4")
AWS Documentation on this http://boto3.readthedocs.io/en/latest/reference/services/cloudformation.html#CloudFormation.Client.describe_stack_resources
回答2:
Yes. Boto3 CF client has methods to get the information you want.
cf = boto3.client('cloudformation'
stacks = cf.list_stacks(StackStatusFilter=['CREATE_COMPLETE'])['StackSummaries']
will return the stack summary for completed stacks. Change the filter to suit your needs.
Get the names of the stacks
names = [stack['StackName'] for stack in stacks]
Then get all stack resources for a given stack
for name in names:
resources = cf.describe_stack_resources(StackName=name)['StackResources']
来源:https://stackoverflow.com/questions/39644127/how-to-determine-what-cloudformation-stack-an-aws-resource-belongs-to