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
asked 5 months ago345 views
2 Answers
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
EXPERT
answered 5 months ago
profile picture
EXPERT
reviewed 5 months ago
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
EXPERT
Kallu
answered 5 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions