List of ec2 instances from all regions

0

Is there a way to export the list of all my EC2 instances in all regions to a .csv file

Sarath
已提问 5 个月前337 查看次数
2 回答
0

Hello.

I think it's probably not possible to do this from the management console, so I think it's better to use the AWS CLI.
Therefore, I think it is better to use a shell script like the one below.
This is the script I used in the past when I wanted to check the number of instances in my AWS account.

#!/bin/bash

output="ec2_instances.csv"
echo "Region,InstanceId" > $output

regions=$(aws ec2 describe-regions --output text --query 'Regions[].RegionName')

for region in $regions; do
        instances=$(aws ec2 describe-instances --region $region)
        for instance_id in $(echo "$instances" | jq -r -c '.Reservations[].Instances[].InstanceId'); do
                echo "$region,$instance_id" >> $output
        done
done

By the way, how many EC2 machines are there in one region?
If there are a large number of EC2 machines, the response to "describe-instances" will include a field called "NextToken", so you will need to add a process that repeats until there is no field left.

profile picture
专家
已回答 5 个月前
profile picture
专家
已审核 5 个月前
0

Above should work without a need to worry about pagination (NextToken).

... by default the AWS CLI automatically makes multiple calls to return all possible results to create pagination.

[from https://docs.aws.amazon.com/cli/latest/userguide/cli-usage-pagination.html ]

profile picture
专家
Kallu
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则