-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Comments
The console doesn't allow you to list all instances irrespective of region, but it does hide the region identifiers with more descriptive text. 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 Your default region is located in your configuration file ( |
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 |
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"
} |
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
Output using Format-TableBy 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 $InstanceList = @()
foreach ($Region in (Get-AWSRegion)) {
$InstanceList += Get-EC2Instance -Region $Region.Region -ProfileName CIDBTest
}
$InstanceList | Format-Table Results
|
You could use
|
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) |
Include tags value to better understand your EC2 instances
|
Just another solution I just wrote! #!/bin/bash echo "Getting AWS Avaiable Regions:" #Start executing #cleanup |
#!/bin/bash #------output------- #./get-instances.sh |
aws cli supports the "query" parameter, which does this stuff as a build-in. |
Thanks obijan! I I started using jq few months back and it changed my life :) |
Note that the AWS Cli Docs do still recommend JQ for more advanced usage:
IMHO, If you prefer to use the Sample Comparison of a similar queryThe following queries list the RDS instances in the default region using the aws cli, parsing the json output from it using JQ and JMESPath For example, a similar query seems to be far more readable OOTB in JMESPath:
|
If for region in `aws ec2 describe-regions --output text | cut -f4`
do
done |
Probably a more robust way is to use a JMESPath query as follows: for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text)
do
done |
You could also do some further parsing to filter to the regions that you want with list-instances =
!f() {
echo "Filtering by \`$1\`"
for region in $(aws ec2 describe-regions --query "Regions[*].RegionName" --output text | tr "\t" "\n" | grep "$1")
do
echo "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region \
--output table \
--query "Reservations[*].Instances[*].{
Instance:InstanceId,
Type:InstanceType,
AZ:Placement.AvailabilityZone,
KeyName:KeyName,
Name:Tags[?Key==\`Name\`]|[0].Value,
Project:Tags[?Key==\`project\`]|[0].Value,
IP:PublicIpAddress,
State:State.Name,
CPUcores:CpuOptions.CoreCount,
CPUThreads:CpuOptions.ThreadsPerCore
}"
done
}; f Usage: $ aws list-instances Apply a filter to the region code, e.g. $ aws list-instances 'us' |
The output format from
|
@laidig worked on Jul 2021, thank you for that!! But it will overwrite all output of each command due to My suggestion for region in `aws ec2 describe-regions --output text | cut -f4`;
do
echo -e "\nListing Instances in region:'$region'..." >> all_instances;
aws ec2 describe-instances --region $region | jq '.Reservations[] | ( .Instances[] | {state: .State.Name, name: .KeyName, type: .InstanceType, key: .KeyName})' >> all_instances ;
done And then
|
Yet another iteration on this theme:
|
hi @N0B0T-cedric when I try your code and that of @bugb AWS comes back with an error saying "you must specify a region. You can also configure your region by running "aws configure"." |
Thanks for this. I had to use AWS_DEFAULT_REGION=eu-west-1 aws ec2 describe-regions --output text
REGIONS ec2.ap-south-1.amazonaws.com opt-in-not-required ap-south-1
REGIONS ec2.eu-north-1.amazonaws.com opt-in-not-required eu-north-1
REGIONS ec2.eu-west-3.amazonaws.com opt-in-not-required eu-west-3
REGIONS ec2.eu-west-2.amazonaws.com opt-in-not-required eu-west-2
REGIONS ec2.eu-west-1.amazonaws.com opt-in-not-required eu-west-1
REGIONS ec2.ap-northeast-3.amazonaws.com opt-in-not-required ap-northeast-3
REGIONS ec2.ap-northeast-2.amazonaws.com opt-in-not-required ap-northeast-2
REGIONS ec2.ap-northeast-1.amazonaws.com opt-in-not-required ap-northeast-1
REGIONS ec2.ca-central-1.amazonaws.com opt-in-not-required ca-central-1
REGIONS ec2.sa-east-1.amazonaws.com opt-in-not-required sa-east-1
REGIONS ec2.ap-southeast-1.amazonaws.com opt-in-not-required ap-southeast-1
REGIONS ec2.ap-southeast-2.amazonaws.com opt-in-not-required ap-southeast-2
REGIONS ec2.eu-central-1.amazonaws.com opt-in-not-required eu-central-1
REGIONS ec2.us-east-1.amazonaws.com opt-in-not-required us-east-1
REGIONS ec2.us-east-2.amazonaws.com opt-in-not-required us-east-2
REGIONS ec2.us-west-1.amazonaws.com opt-in-not-required us-west-1
REGIONS ec2.us-west-2.amazonaws.com opt-in-not-required us-west-2 Works great, got a notice from AWS today saying they're going to can Node v16 in June 2024: for region in $(AWS_DEFAULT_REGION=eu-west-1 aws ec2 describe-regions --output text | cut -f4)
do
echo -e "\nListing deprecated lambdas in region:'${region}'..."
aws lambda list-functions --region="${region}" --query "Functions[?Runtime=='nodejs16.x'].FunctionArn"
done |
There are multiple ways to parse the output of
|
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?
The text was updated successfully, but these errors were encountered: