【问题标题】:Cloudformation AWS CLI Query ALL Stack resources with multiple nested stacksCloudformation AWS CLI 使用多个嵌套堆栈查询所有堆栈资源
【发布时间】:2018-10-27 15:06:00
【问题描述】:

我知道我可以通过以下方式获得堆栈资源:-

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 脚本中,但我想我会先检查一下。

谢谢

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    更通用的解决方案需要处理不同级别的嵌套。在我们的例子中,我们的许多(但不是全部)s3 存储桶是使用从我们的子模板调用的标准加密存储桶模板创建的。

    在搜索需要在删除堆栈之前清空的存储桶时,我们使用类似于以下的脚本:

    findBuckets() {
        aws cloudformation describe-stack-resources \
            --stack-name $1 \
            --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
    

    【讨论】:

      【解决方案2】:

      你无法实现这一命令。而是获取属于父堆栈的所有资源的列表(嵌套堆栈详细信息),然后通过遍历列表来描述堆栈资源。以下是我为获取所有资源而编写的命令:

      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
      

      【讨论】:

      猜你喜欢
      • 2021-05-24
      • 2018-06-21
      • 2020-08-30
      • 2020-03-02
      • 2020-01-17
      • 2019-02-27
      • 2021-07-06
      • 1970-01-01
      • 2017-11-25
      相关资源
      最近更新 更多