Cloudformation AWS CLI 查询具有多个嵌套堆栈的所有堆栈资源
Cloudformation AWS CLI Query ALL Stack resources with multiple nested stacks
我知道我可以通过以下方式获取堆栈资源:-
aws cloudformation describe-stack-resources \
--stack-name MYSTACKNAME \
--query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' \
--output table
如果我的堆栈仅包含 NESTED STACKS,我如何在 Cloudformation 中获取我的堆栈的所有嵌套堆栈的资源?
我可以看到如何查询父堆栈的所有堆栈。
aws cloudformation list-stacks \
--query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].{Stack:StackName}' \
--output json
我不知道如何使用它来提供 describe-stack-resources,它似乎只需要一个单独的值。
我可以将其构建到 python 脚本中,但我想我会先检查一下。
谢谢
您无法实现这一命令。而是获取属于父堆栈的所有资源的列表(嵌套堆栈详细信息),然后通过遍历列表来描述堆栈资源。下面是我写的获取所有资源的命令:
for stack in $(aws cloudformation list-stacks --output text --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].[StackName]') ; do aws cloudformation describe-stack-resources --stack-name $stack --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' --output table ; done
更通用的解决方案需要处理可变级别的嵌套。在我们的例子中,我们的许多(但不是全部)s3 存储桶是使用从我们的子模板调用的标准加密存储桶模板创建的。
我们在搜索需要在删除堆栈之前清空的桶时使用类似于以下的脚本:
findBuckets() {
aws cloudformation describe-stack-resources \
--stack-name \
--query "StackResources[][ResourceType, PhysicalResourceId]" \
--output text |
while read type value; do
if [[ $type == 'AWS::CloudFormation::Stack' ]]; then
findBuckets $value
else
echo $type $value
fi
done
}
然后可以这样调用,例如:
findBuckets my-stack-dev
AWS CLI 有一些更新。现在您可以直接以堆栈资源为目标。如果您有堆栈名称,则需要使用 StackResourcesSummaries
aws cloudformation list-stack-resources --stack-name soinshane-prd-app-ec2-stack --output text --query 'StackResourceSummaries[?(ResourceStatus!=`CREATE_COMPLETE`&&ResourceStatus!=`UPDATE_COMPLETE`)].[PhysicalResourceId, ResourceStatus]'
我知道我可以通过以下方式获取堆栈资源:-
aws cloudformation describe-stack-resources \
--stack-name MYSTACKNAME \
--query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' \
--output table
如果我的堆栈仅包含 NESTED STACKS,我如何在 Cloudformation 中获取我的堆栈的所有嵌套堆栈的资源?
我可以看到如何查询父堆栈的所有堆栈。
aws cloudformation list-stacks \
--query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].{Stack:StackName}' \
--output json
我不知道如何使用它来提供 describe-stack-resources,它似乎只需要一个单独的值。
我可以将其构建到 python 脚本中,但我想我会先检查一下。
谢谢
您无法实现这一命令。而是获取属于父堆栈的所有资源的列表(嵌套堆栈详细信息),然后通过遍历列表来描述堆栈资源。下面是我写的获取所有资源的命令:
for stack in $(aws cloudformation list-stacks --output text --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].[StackName]') ; do aws cloudformation describe-stack-resources --stack-name $stack --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' --output table ; done
更通用的解决方案需要处理可变级别的嵌套。在我们的例子中,我们的许多(但不是全部)s3 存储桶是使用从我们的子模板调用的标准加密存储桶模板创建的。
我们在搜索需要在删除堆栈之前清空的桶时使用类似于以下的脚本:
findBuckets() {
aws cloudformation describe-stack-resources \
--stack-name \
--query "StackResources[][ResourceType, PhysicalResourceId]" \
--output text |
while read type value; do
if [[ $type == 'AWS::CloudFormation::Stack' ]]; then
findBuckets $value
else
echo $type $value
fi
done
}
然后可以这样调用,例如:
findBuckets my-stack-dev
AWS CLI 有一些更新。现在您可以直接以堆栈资源为目标。如果您有堆栈名称,则需要使用 StackResourcesSummaries
aws cloudformation list-stack-resources --stack-name soinshane-prd-app-ec2-stack --output text --query 'StackResourceSummaries[?(ResourceStatus!=`CREATE_COMPLETE`&&ResourceStatus!=`UPDATE_COMPLETE`)].[PhysicalResourceId, ResourceStatus]'