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
preguntada hace 5 meses337 visualizaciones
2 Respuestas
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
EXPERTO
respondido hace 5 meses
profile picture
EXPERTO
revisado hace 5 meses
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
EXPERTO
Kallu
respondido hace 5 meses

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas