How to export Amazon EC2 instances to a CSV file


Amazon website is limited to 50 instances per page. Viewing lots of instances is a pain and it doesn’t support exporting to CSV/TSV/Excel/other out of the box. The only fix is to use the CLI.

Requirements

  • An AWS account with access rights to see your servers
  • A pair of AWS keys (Users -> [username] -> Security Credentials -> Create Access Key)
# Install AWS packages
sudo apt-get install -y python python-pip
sudo pip install aws-shell

Listing Instances

# aws-shell will show a wizard to configure your account and region the first time you use it
aws-shell
ec2 describe-instances --output text --query 'Reservations[*].Instances[*].[InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName, [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`purpose`].Value] [0][0], [Tags[?Key==`environment`].Value] [0][0], [Tags[?Key==`team`].Value] [0][0] ]' > instances.tsv
# open instances.tsv with Excel
# enjoy

You can modify the command to pick the information you want. Refer to the official AWS command line reference.

Advertisement

5 thoughts on “How to export Amazon EC2 instances to a CSV file

  1. thanks that works. If you want to get instances with name in tabular format use the below command

    aws ec2 describe-instances –output text –query ‘Reservations[*].Instances[*].[InstanceId, State.Name, [Tags[?Key==`Name`].Value] [0][0] ]’ –output table

    Like

  2. Thank you very much HFT guy for this tip! I was asked to generate a list our AWS instances for coporate SecOps team, and found I could not simply export from console. I had to change ‘ to ” before Reservations and at the end before the >. But this worked for me. I ran it from from a windows 10 laptop with AWS CLI installed. Note also, I used –profile to supply the aws credentials needed for our awsprod environment (we have a test/dev environment too :
    aws –profile awsprod ec2 describe-instances –output text –query “Reservations[*].Instances[*].[InstanceId, InstanceType, ImageId, State.Name, LaunchTime, Placement.AvailabilityZone, Placement.Tenancy, PrivateIpAddress, PrivateDnsName, PublicDnsName, [Tags[?Key==`Name`].Value] [0][0], [Tags[?Key==`purpose`].Value] [0][0], [Tags[?Key==`environment`].Value] [0][0], [Tags[?Key==`team`].Value] [0][0] ]” > c:\instances.tsv

    Like

    • aws ec2 describe-instances –query ‘Reservations[*].Instances[*].{Internal_IP:NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress,Instance_ID:InstanceId,Instance_name:Tags[?Key==`Name`]|[0].Value,Region:Placement.AvailabilityZone,PublicDNS:PublicDnsName,KeyName:KeyName,Type_of_instance:InstanceType,Public_IP:PublicIpAddress}’ –output table

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s