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
gefragt vor 5 Monaten338 Aufrufe
2 Antworten
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
EXPERTE
beantwortet vor 5 Monaten
profile picture
EXPERTE
überprüft vor 5 Monaten
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
EXPERTE
Kallu
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen