代码之家  ›  专栏  ›  技术社区  ›  Simon Taylor

Cloudformation AWS CLI查询具有多个嵌套堆栈的所有堆栈资源

  •  5
  • Simon Taylor  · 技术社区  · 6 年前

    我知道我可以通过以下方式获取资源:-

    aws cloudformation describe-stack-resources \
                        --stack-name MYSTACKNAME \
                        --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' \
                        --output table
    

    如果我的堆栈仅由嵌套堆栈组成,那么如何获取Cloudformation中所有嵌套堆栈的资源?

    我可以查看如何查询父堆栈的所有堆栈。

    aws cloudformation list-stacks \
                        --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].{Stack:StackName}' \
                        --output json
    

    我不知道如何使用这个feed-description堆栈资源,它似乎只接受一个单独的值。

    我可以将其构建到python脚本中,但我想我会先检查一下。

    谢谢

    3 回复  |  直到 6 年前
        1
  •  5
  •   krishna_mee2004    6 年前

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

    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
    
        2
  •  1
  •   DeadlyChambers    2 年前

    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]'
    

    Great Resource for more info

    AWS CLI of Course another one

        3
  •  0
  •   Typhlosaurus    5 年前

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

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

    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