You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何通过AWS CLI查询含嵌套栈的CloudFormation所有栈资源

How to Fetch Resources for All Nested CloudFormation Stacks Without a Python Script

Great question! Writing a Python script is totally valid, but there are simpler shell/PowerShell-based approaches that let you reuse your existing AWS CLI commands without full scripting. Here are a couple of straightforward options:

Option 1: Bash/Zsh with AWS CLI (no extra tools needed)

You can use the AWS CLI's --output text to get a space-separated list of stack names, then loop through each one to fetch its resources:

# Retrieve all relevant nested stack names (space-separated)
STACKS=$(aws cloudformation list-stacks \
  --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].StackName' \
  --output text)

# Loop through each stack and print its resources
for STACK in $STACKS; do
  echo -e "\n=== Resources for Stack: $STACK ==="
  aws cloudformation describe-stack-resources \
    --stack-name "$STACK" \
    --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' \
    --output table
done

This works without any additional tools since we're using --output text to format the stack names into a loop-friendly list.

Option 2: Bash/Zsh with jq (for cleaner JSON parsing)

If you have jq installed (a common JSON processing tool), you can parse the stack names more reliably—especially useful if stack names contain spaces:

# Get stack names as a newline-separated list using jq
STACKS=$(aws cloudformation list-stacks \
  --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].StackName' \
  --output json | jq -r '.[]')

# Loop through each stack
while IFS= read -r STACK; do
  echo -e "\n=== Resources for Stack: $STACK ==="
  aws cloudformation describe-stack-resources \
    --stack-name "$STACK" \
    --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' \
    --output table
done <<< "$STACKS"

Option 3: PowerShell (for Windows users)

If you prefer PowerShell, the approach is similar—parse the JSON output and loop through each stack:

# Fetch stack names and convert JSON to PowerShell objects
$stacks = aws cloudformation list-stacks `
  --query 'StackSummaries[?contains(StackName, `MYSTACKNAME`) && (StackStatus==`CREATE_COMPLETE`||StackStatus==`UPDATE_COMPLETE`)].StackName' `
  --output json | ConvertFrom-Json

# Iterate over each stack and retrieve resources
foreach ($stack in $stacks) {
  Write-Host "`n=== Resources for Stack: $stack ==="
  aws cloudformation describe-stack-resources `
    --stack-name $stack `
    --query 'StackResources[*].{Type:ResourceType,LogicalID:LogicalResourceId}' `
    --output table
}

Notes for deeper nested stacks

If your nested stacks have their own nested stacks (multi-level hierarchy), these basic loops won't recurse automatically. For that scenario, a Python script (or a more advanced shell function with recursion) would be better, but for single-level nested stacks, the above methods are quick and easy.

内容的提问来源于stack exchange,提问作者Simon Taylor

火山引擎 最新活动