Skip to content

Instantly share code, notes, and snippets.

@junaidk
Last active July 7, 2023 04:55
Show Gist options
  • Select an option

  • Save junaidk/433345da5bf8e580680d9109792d3169 to your computer and use it in GitHub Desktop.

Select an option

Save junaidk/433345da5bf8e580680d9109792d3169 to your computer and use it in GitHub Desktop.
Get List of EC2 instances from All regions
# install aws cli first and configure it with credentials and default region
# the script will iterate over all regions of AWS
for region in `aws ec2 describe-regions --output text | cut -f4`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --query "Reservations[*].Instances[*].{IP:PublicIpAddress,ID:InstanceId,Type:InstanceType,State:State.Name,Name:Tags[0].Value}" --output=table --region $region
done
@baragona

Copy link
Copy Markdown

it works!

@dgacias

dgacias commented Feb 9, 2018

Copy link
Copy Markdown

It works as expected, but i had to execute first "aws configure" with empty values on everything except the region, because if you do not have any default region, the CLI complaints it does not know in which region work even when executing "aws ec2 describe-regions" alone.
I also tried to pass as argument --region eu-west-1 but it does not work, you have to do the aws configure thing first to stablish a default region.

@eguven

eguven commented Aug 13, 2018

Copy link
Copy Markdown

@dgacias just export values before the loop

export AWS_PROFILE=<YOUR-PROFILE>
export AWS_DEFAULT_REGION=<SOME-REGION>

@royharoush

Copy link
Copy Markdown

you're d man !

@jake9696

Copy link
Copy Markdown

FYI, you need to change the "cut -f3" to "cut -f4" now as AWS added another field to the describe-regions output.

@junaidk

junaidk commented Nov 12, 2019

Copy link
Copy Markdown
Author

@jake9696
Thanks for pointing it out.
I have updated it.

@fcp999

fcp999 commented Nov 17, 2019

Copy link
Copy Markdown

If you don't have the defaults set cut f3 works, if you do use cut f4

@santhosh007K

Copy link
Copy Markdown

its works thanks

@mkaurin

mkaurin commented Mar 19, 2020

Copy link
Copy Markdown

aws ec2 describe-regions --output text --query 'Regions[].RegionName'

@Cyreex

Cyreex commented Jun 16, 2021

Copy link
Copy Markdown

Name:Tags[0].Value > Name:Tags[?Key=='Name']|[0].Value

The first tag may be different.

@santhumonster

Copy link
Copy Markdown

Hi Everyone,
How to export that output to excel format?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment