New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to describe instances from all regions? #1777

Closed
ApproximateIdentity opened this Issue Feb 8, 2016 · 12 comments

Comments

Projects
None yet
@ApproximateIdentity
Copy link

ApproximateIdentity commented Feb 8, 2016

I'm trying to switch my workflow over to the cli from the aws control panel, but I've run into a very odd issue. I know how to describe instances within a region, but how do I describe all of my instances irrespective of regions? Or at least get an output of all ec2 regions so I can do this programmatically one by one (i.e. something like aws ec2 list-regions).

It's very odd because the cli seems to use different names for regions than the control panel. For one, apparently all of my instances are being started in region "us-west-2" (at least so far), but I cannot find that string anywhere in the control panel. In fact, I can't seem to find anywhere listing my default region at all.

Is there a command inside of aws that does this?

@JordonPhillips

This comment has been minimized.

Copy link
Member

JordonPhillips commented Feb 8, 2016

The console doesn't allow you to list all instances irrespective of region, but it does hide the region identifiers with more descriptive text. us-west-2 is the datacenter located in Oregon, for instance. There's a drop-down in the top right of the console page that lets you select a region (pictured below).

For most services, there is not currently a programmatic way of discovering which regions are available, and so you will need to refer to this list. EC2 is one of the exceptions, however, as it provides the describe-regions function. Since the CLI is a wrapper over service APIs, we can't do multi-region searches unless the service supports it (S3, for instance, is mostly region-agnostic). You will have to provide the region with the --region argument to query in a different region.

Your default region is located in your configuration file (~/.aws/config on UNIX systems), and can be both shown and set with the aws configure command.

regions

@nonbeing

This comment has been minimized.

Copy link

nonbeing commented Sep 9, 2016

For people arriving here from Google, here's one way to list all your instances across all regions with the AWS CLI (based on the discussion above):

for region in `aws ec2 describe-regions --output text | cut -f3`
do
     echo -e "\nListing Instances in region:'$region'..."
     aws ec2 describe-instances --region $region
done
@Fitblip

This comment has been minimized.

Copy link

Fitblip commented Mar 5, 2017

For those of you who also don't want to parse JSON with your eyeballs, here's an appended version that uses jq to give you a bit more-to-the-point summary.

for region in `aws ec2 describe-regions --output text | cut -f3`
do
     echo -e "\nListing Instances in region:'$region'..."
     aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, type: .InstanceType, key: .KeyName})'
done

Example output:

Listing Instances in region:'us-west-1'...
{
  "state": "stopped",
  "name": "scanner-1",
  "type": "t2.micro",
  "key": "scan1"
}
{
  "state": "stopped",
  "name": "cors",
  "type": "t2.micro",
  "key": "cors"
}
{
  "state": "stopped",
  "name": "scanner-2",
  "type": "t2.medium",
  "key": "scan2"
}
@JordonPhillips

This comment has been minimized.

Copy link
Member

JordonPhillips commented Mar 6, 2017

@nonbeing Nice! If you use that often, you should consider making it an alias.

@pcgeek86

This comment has been minimized.

Copy link

pcgeek86 commented Jun 16, 2017

Another option would be to use the AWS PowerShell .NET Core module, which runs natively on Linux, Mac, and Windows.

foreach ($Region in (Get-AWSRegion)) {
  Get-EC2Instance -Region $Region.Region
}

Results

GroupNames    : {}
Groups        : {}
Instances     : {asdf}
OwnerId       : 123
RequesterId   : 123
ReservationId : r-asdf

GroupNames    : {}
Groups        : {}
Instances     : {asdf}
OwnerId       : 123
RequesterId   : 123
ReservationId : r-asdf

GroupNames    : {}
Groups        : {}
Instances     : {asdf}
OwnerId       : 123
RequesterId   : 123
ReservationId : r-asdf

Output using Format-Table

By default, in the current version of the AWS PowerShell module, the EC2 instances are returned in the PowerShell "list" format. However, this is easily changed to tabular formatting, using the Format-Table command.

$InstanceList = @()
foreach ($Region in (Get-AWSRegion)) {
  $InstanceList += Get-EC2Instance -Region $Region.Region -ProfileName CIDBTest
}
$InstanceList | Format-Table

Results

GroupNames Groups Instances   OwnerId      RequesterId  ReservationId       RunningInstance
---------- ------ ---------   -------      -----------  -------------       ---------------
{}         {}     {asdf222}   123          123                  r-123               {asdf222}
{}         {}     {asdf222}   123          123                  r-123               {asdf222}
{}         {}     {asdf222}   123          123                  r-123               {asdf222}
@jimdigriz

This comment has been minimized.

Copy link

jimdigriz commented Jun 25, 2017

You could use jq, if you like adding more dependencies, or just use the built in --query in AWS CLI:

aws --profile whatever ec2 describe-instances --query 'Reservations[].Instances[].[State.Name,InstanceType]'
[
    [
        "running", 
        "t2.small"
    ], 
    [
        "running", 
        "t2.small"
    ], 
    [
        "running", 
        "m3.medium"
    ]
]
@ygeo

This comment has been minimized.

Copy link

ygeo commented Aug 8, 2017

while read -r r ; echo start region : $r ; do while read -r i ; do echo instance-id: $i 2>&1 ; done < <(aws ec2 describe-instances --query "Reservations[].Instances[].InstanceId" --region $r --profile prd|perl -nl -e 's/\s+/\n/g;print'); echo "stop region: $r" ; done < <(aws ec2 describe-regions --output text --profile prd| cut -f 3)

@Selimcse98

This comment has been minimized.

Copy link

Selimcse98 commented Dec 7, 2017

Include tags value to better understand your EC2 instances


for region in `aws ec2 describe-regions --output text | cut -f3`
do
     echo -e "\nListing Instances in region:'$region'..."
     aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, Tag_Name: .Tags[].Value,type: .InstanceType, key: .KeyName})'
done

@AhmedAG80

This comment has been minimized.

Copy link

AhmedAG80 commented Feb 13, 2018

Just another solution I just wrote!
This small script can execute commands across all regions
Usage examples:
sudo ./aws-x.sh ec2 describe-instances
sudo ./aws-x.sh ec2 describe-instances | grep PrivateDnsName

#!/bin/bash

echo "Getting AWS Avaiable Regions:"
sudo aws ec2 describe-regions --region eu-west-2 |grep RegionName |cut -d '"' -f 4 > /tmp/regions.txt
cat /tmp/regions.txt

#Start executing
while read region; do
echo "Executing " $@ " on " $region ":"
aws --region $region $@
done</tmp/regions.txt

#cleanup
rm /tmp/regions.txt

@nlsko

This comment has been minimized.

Copy link

nlsko commented Aug 19, 2018

#!/bin/bash
echo Grabbing instances in all regions, please wait..
for region in aws ec2 describe-regions --output text | cut -f3
do
aws ec2 describe-instances --region $region --query 'Reservations[].Instances[].[State.Name,InstanceType,PublicIpAddress,Placement.AvailabilityZone]' --output text
done

#------output-------

#./get-instances.sh
#Grabbing instances in all regions, please wait..
#running t2.micro 54.93.120.174 eu-central-1b
#running t2.micro 54.86.91.202 us-east-1d

@obijan42

This comment has been minimized.

Copy link

obijan42 commented Dec 6, 2018

For those of you who also don't want to parse JSON with your eyeballs, here's an appended version that uses jq to give you a bit more-to-the-point summary.

aws cli supports the "query" parameter, which does this stuff as a build-in.

@AhmedAG80

This comment has been minimized.

Copy link

AhmedAG80 commented Dec 6, 2018

Thanks obijan! I I started using jq few months back and it changed my life :)

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