如何一次从多个 AWS 账户获取 JSON 格式的信息?

How can I get information in JSON format from many AWS accounts at once?

我需要同时获取多个 AWS 账户的 VPC、子网、安全组等信息。我该怎么做?

一种解决方案是在 AWS CLI 中使用 for 循环。查看 CLI Documentation 您想要为其收集信息的服务并找到适当的命令,然后使用 for 循环遍历 ~/.aws/credentials 文件中的配置文件。

例如,如果您想要获取 VPC、子网和安全组,这些都在 EC2 CLI docs 中进行了描述。

下面是获取有关这些资源的信息并将其作为 .json 输出到当前目录的示例(假设您在使用 aws configure[=16 时没有更改默认输出格式=]

#!/usr/bin/env bash    
region=us-east-1
for profile in `grep [[] ~/.aws/credentials | tr -d '[]'`
do
        echo "getting vpcs, subnets, and security groups for $profile"
        aws ec2 describe-vpcs --region $region --profile $profile > "$profile"_vpcs.json
        aws ec2 describe-subnets --region $region --profile $profile > "$profile"_subnets.json
        aws ec2 describe-security-groups --region $region --profile $profile > "$profile"_security_groups.json
done