aws-cloudformation - 扫描 cloudformation 堆栈层次结构的资源

标签 aws-cloudformation boto3

我对递归扫描层次结构的尝试感到沮丧 AWS CloudFormation 堆栈。我需要看看他们所有的 资源。

回想一下,一个 CloudFormation 堆栈可以包含另一个堆栈。

看来这些子堆栈的描述是由 describe_stack_resources 没有用。

我的问题的示例如下:

使用 Boto3 我们可以看到该堆栈有八个子堆栈:

In [22]: resources = cfn.describe_stack_resources(StackName=my_stack_name)['StackResources']

In [23]: len(resources)
Out[23]: 8

In [24]: [r['ResourceType'] for r in resources]
Out[24]: 
['AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack',
 'AWS::CloudFormation::Stack']

奇怪的是,所有这些子堆栈都被报告为具有 创建它们的父级,尽管它们具有不同的 PhysicalResourceId

In [25]: [r['StackName'] == my_stack_name for r in resources]
Out[25]: [True, True, True, True, True, True, True, True]

In [29]: resources[0]['PhysicalResourceId'] == resources[1]['PhysicalResourceId']
Out[29]: False

如果我使用物理 ID 请求子堆栈的资源,这是行不通的,我会获取父堆栈的资源:

In [32]: p_id_0 = resources[0]['PhysicalResourceId']

In [33]: child_resources = cfn.describe_stack_resources(PhysicalResourceId=p_id_0)['StackResources']

In [34]: child_resources == resources
Out[34]: True

有解决办法吗?

最佳答案

此命令将列出所有嵌套堆栈的资源:

map(lambda x: cfn.describe_stack_resources(StackName=x['PhysicalResourceId'])['StackResources'], cfn.describe_stack_resources(StackName=my_stack_name)['StackResources'])

请参阅DescribeStackResources API 文档,了解有关此 API 的请求参数/响应元素的详细信息。

在请求中,StackName 参数接受堆栈的名称​​或其唯一的堆栈 ID。在堆栈内部调用中,您可以提供AWS::CloudFormation::StackPhysicalResourceId。资源,因为物理 ID(Ref 内部函数返回的字符串)是 defined作为此资源类型的堆栈 ID。

请注意,在响应中,StackResources.StackName属性表示与堆栈关联的名称,而不是所描述的资源的堆栈名称。

另请注意,使用 PhysicalResourceId 参数调用 DescribeStackResources 将返回包含该物理资源 ID 的父堆栈的所有资源,而不是由该物理资源 ID 表示的堆栈资源,如 API 文档中所述:

Returns AWS resource descriptions for running and deleted stacks. If StackName is specified, all the associated resources that are part of the stack are returned. If PhysicalResourceId is specified, the associated resources of the stack that the resource belongs to are returned.

关于aws-cloudformation - 扫描 cloudformation 堆栈层次结构的资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41595784/

相关文章:

amazon-web-services - 通过Cloudformation创建SQS队列及其访问策略时出错

python-3.x - 如何使用boto和python3在CloudFront中使对象无效?

python - 如何使用 Content-MD5 将对象放入 s3

python - Boto3:WAITINGS3流式上传完成

amazon-web-services - CloudFormation IAM 角色 -- AssumeRolePolicyDocument

amazon-web-services - 如何在无服务器中引用堆栈输出值?

python - 如何使用 boto3 获取 aws 中存在的实例和卷以及负载均衡器的总数?

python - 使用 Boto3 回调,显示上传进度

amazon-web-services - AWS 云形成 : "The key pair ___ does not exist" error for newly-created EC2 key pair

aws-cloudformation - CloudFormation findInMap 返回 null