Using aws cli, what is best way to determine the current region
Amazon Web-ServicesRegionAws CliAmazon Web-Services Problem Overview
Interactively, I can use "aws configure" to change or see the default region. Is there a "pwd" like function, documented or not that allows me to determine or confirm the current region mid-script ? Even if AWS_DEFAULT_REGION is not defined ? I want a script to run under a number of profiles. I can scrape from aws configure list, but is there something neater ?
Amazon Web-Services Solutions
Solution 1 - Amazon Web-Services
aws configure get region
will get you the current region at that point in your script.
If you are using a profile, then type aws configure get region --profile $PROFILE_NAME
.
Solution 2 - Amazon Web-Services
Perhaps, AWS has not provide to get the current region. However, instead of getting the current region, They provide to get a current availability zone via an instance metadata. All availability zones include a current region, so you can determine the current region with you replace a part of the current availability zone in a script on the EC2 instance.
For example:
curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/'
Solution 3 - Amazon Web-Services
The following gives the region actually used by the CLI regardless of whether environment variables are or are not set:
aws ec2 describe-availability-zones --output text --query 'AvailabilityZones[0].[RegionName]'
The region that will be used by the CLI is determined by the following order of precedence:
- Command line
--region
option - Value of
AWS_DEFAULT_REGION
environment variable - Region specified in the 'Current profile', which is determined by the following order of precedence
- Command line
--profile
option - Value of
AWS_PROFILE
environment variable - Otherwise profile is [default]
- Command line
Unfortunately using aws configure get region
only returns the region as specified by your 'Current profile', and ignores the AWS_DEFAULT_REGION
environment variable.
Rather than assuming any precedence rules, you can just use the CLI to make an actual API call and get the region from the result. The only call I could find that should always work was to aws ec2 describe-availability-zones
. Another advantage of this method is that you can specify --region
or --profile
options and still get the right answer.
Solution 4 - Amazon Web-Services
aws configure get region
is neat but I wanted to be able to know the region even when AWS_DEFAULT_REGION
was set. Unfortunately, according to the documentation:
> Note that aws configure get only looks at values in the AWS configuration file. It does not resolve configuration variables specified anywhere else, including environment variables, command line arguments, etc.
Instead, assuming you have Python and boto3 installed, you can run:
python -c 'import boto3; print(boto3.Session().region_name)'
E.g.
$ AWS_DEFAULT_REGION=us-east-1 python -c 'import boto3; print(boto3.Session().region_name)'
us-east-1
Solution 5 - Amazon Web-Services
Taken from @RichVel's comment in this answer, to get the resolved region set from either AWS_DEFAULT_REGION
or the region in the aws config file (aws configure get region
only gives the value set in the config file) use:
aws configure list | grep region | awk '{print $2}'
Example:
with $AWS_DEFAULT_REGION
unset:
$ echo $AWS_DEFAULT_REGION
$ cat ~/.aws/config
[foo]
region = us-east-1
$ aws configure list | grep region | awk '{print $2}'
us-east-1
$ aws configure get region
us-east-1
with $AWS_DEFAULT_REGION
set:
$ export AWS_DEFAULT_REGION=us-west-2
$ echo $AWS_DEFAULT_REGION
us-west-2
$ cat ~/.aws/config
[foo]
region = us-east-1
$ aws configure list | grep region | awk '{print $2}'
us-west-2
$ aws configure get region
us-east-1
Solution 6 - Amazon Web-Services
The region is in the following:
curl http://169.254.169.254/latest/dynamic/instance-identity/document
So...
curl -s http://169.254.169.254/latest/dynamic/instance-identity/document|jq -r .region
Solution 7 - Amazon Web-Services
This grows upon the answers by Jeshan Babooa and Alastair McCormack which have these known limitations:
aws configure get region
doesn't return the region if the region is defined by the environment variableAWS_DEFAULT_REGION
.boto3
may not always be available, even thoughpython -c 'import boto3; print(boto3.Session().region_name)'
works when it is available.
To address the above limitations, consider in Bash:
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-$(aws configure get region)}
This uses the value of the AWS_DEFAULT_REGION
if available, otherwise it uses the output of aws configure get region
.
Solution 8 - Amazon Web-Services
Not sure if this works for you but I came to this page looking for similar advice. I am often switching between AWS Profiles which are configured to use different regions. In my case, the profile I am using is controlled by an environment variable - AWS_PROFILE
To get the current region, regardless of which profile I am using, I found this command useful:
aws configure get region --profile=$AWS_PROFILE
Solution 9 - Amazon Web-Services
If you have region info for your profiles in .aws/config
, you can do as follows:
Example of .aws/config
(note that dev
profile is called profile dev
). The profile
word is imporant.
[default]
region = ap-southeast-2
[profile dev]
region = us-east-1
Then using cli, you can:
aws configure get profile.default.region
which gives ap-southeast-2
and
aws configure get profile.dev.region
which gives us-east-1
.