All Linux HowTo's

Export and List your EC2 IP Addresses and other details The Easy Way

Amazon doesn’t allow you to list or export your EC2 details using their web interface, at least not yet. Until then you have to use the “aws” command. Otherwise known as the “aws cli”. But it’s easier than you think.

Get the “aws” cli tool from here:

Windows:

http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-msi-on-windows

Mac OS / OSX / Linux:

http://docs.aws.amazon.com/cli/latest/userguide/installing.html#install-bundle-other-os

Once installed you’ll need to get your authentication details from the AWS IAM section. Log into AWS (aws.amazon.com) and go to the “IAM” section. Go to “Users” and then click on your “user”. Go to the “Security Credentials” TAB. Click the “Create Access Key” button. Note the “Key ID” and “Secret”.

Now it’s time to configure the “aws” tool. You’ve already installed it so now run the following command to configure it.

aws configure

You will be asked for your “Key ID” and “Secret”. Enter those in when asked. You will also be asked for your default region and output format. The format should be “json” and the region is whatever you most often access. If you’re not sure, google it “https://www.google.com.au/webhp?#q=aws%20region%20names”.

Now you have it configured and ready to go. Here’s how you can list the EC2’s and their basic details:

aws ec2 describe-instances --region=ap-southeast-2 --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress,PublicIpAddress]' --output table

The above can be broken down into the following parts:

ec2 describe-instances = the sub-command that the "aws" command will run. 
--region=ap-southeast-2 = The region i'm interested in. 
--query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress,PublicIpAddress]' = The query/filter. 
--output table = The output format which would default to "json". 

You can sort by the second (or any) column using the following:

aws ec2 describe-instances --region=ap-southeast-2 --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress,PublicIpAddress]' --output text | sort -k2f

Where “-k2” means “sort by the second column” and the “f” ignores case.

I hope this has been a time saver for you.

Similar Posts: