36

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 ?

Acumenus
  • 41,481
  • 14
  • 116
  • 107
mckenzm
  • 1,001
  • 1
  • 8
  • 15

9 Answers9

55

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 --profile $PROFILE_NAME.

Jeshan
  • 1,353
  • 9
  • 14
  • 13
    This will return the region of the configuration, not the region that your aws cli invocation was performed from. (e.g rm ~/.aws/config; aws configure get region and notice it is empty (or remove your ENV vars or other locations of aws config settings). – cgseller Jan 04 '18 at 18:47
  • 411 Unfortunately this does not always work. e.g. Say my profile is configured to eu-west-1, but I do: aws --region us-east-1 configure get region then this returns eu-west-1, even though using aws --region us-east-1 will operate upon the us-east-1 region. – Asfand Qazi Sep 07 '18 at 19:22
  • correct. I don't know of any better way. Try the accepted answer – Jeshan Sep 10 '18 at 10:48
36

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/'
NSR
  • 759
  • 6
  • 19
  • 9
    Using AWS CLI, you could use `aws configure list | grep region` - this takes account of environment variables and `~/.aws/config` – RichVel Mar 29 '18 at 07:32
  • As an aside, the curl example is good on the instance, I was looking for something for a bash include 'fragment' on a CLI client. – mckenzm Oct 22 '19 at 22:30
  • the above address returns nothing. It really shouldn't be this hard to get the region as the AWS CLI is going to use it. – Paul S Jan 26 '20 at 01:20
10

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
Acumenus
  • 41,481
  • 14
  • 116
  • 107
Alastair McCormack
  • 23,069
  • 7
  • 60
  • 87
10

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
Acumenus
  • 41,481
  • 14
  • 116
  • 107
kleaver
  • 619
  • 8
  • 14
8

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:

  1. Command line --region option
  2. Value of AWS_DEFAULT_REGION environment variable
  3. 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]

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.

John Rees
  • 1,163
  • 11
  • 22
5

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
vy32
  • 24,271
  • 30
  • 100
  • 197
J Roysdon
  • 174
  • 1
  • 4
  • Not from inside a CLI script on a client machine. This works on the instance. Thanks anyway. – mckenzm May 02 '19 at 00:19
  • I'm not sure your intent for confirming the current Region setting, but why not just set AWS_DEFAULT_REGION in your script and force it to be what you want it? You can then confirm it is set. – J Roysdon Nov 13 '19 at 22:58
5

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 variable AWS_DEFAULT_REGION.
  • boto3 may not always be available, even though python -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.

Acumenus
  • 41,481
  • 14
  • 116
  • 107
2

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
Yani
  • 1,142
  • 2
  • 11
  • 22
0

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.

Marcin
  • 108,294
  • 7
  • 83
  • 138
  • Possibly, but this is a bit rigid. I could also store the current region in an environment variable if I was careful to maintain it rigorously. My needs were more along the lines of "region shopping" for an instance where availability ebbs and flows. – mckenzm Jan 24 '20 at 02:09