92

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 ?

2
  • is this script running on EC2?
    – Mircea
    Commented Jul 11, 2015 at 4:33
  • No, Ubuntu outside from AWS.
    – mckenzm
    Commented Jul 11, 2015 at 16:05

11 Answers 11

126

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.

4
  • 24
    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
    Commented Jan 4, 2018 at 18:47
  • 1
    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. Commented Sep 7, 2018 at 19:22
  • correct. I don't know of any better way. Try the accepted answer
    – user7401700
    Commented Sep 10, 2018 at 10:48
  • If you need to change the region of the current cli session: aws configure set region us-east-1 (put the region you want). Commented Sep 14, 2023 at 18:52
55

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.

3
  • 1
    What does it mean if I call this with --region, and it just shows the value that was passed in on that switch? Commented Jun 22, 2021 at 22:39
  • 1
    @GaTechThomas You are right: it simply returns the region. I agree it’s trivial, but vaguely useful in the sense that the above command correctly interprets the same arguments that you intend to supply to the actual commands you want to run. This could be useful if your scripts are dynamically composing commands so you can’t anticipate exactly what arguments will be supplied.
    – John Rees
    Commented Jun 22, 2021 at 22:49
  • Note that this does not work if your profile isn't authorized to execute the DescribeAvailabilityZones operation. You'll get an UnauthorizedOperation error.
    – user167019
    Commented Jan 22 at 22:52
44

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/'
4
  • 13
    Using AWS CLI, you could use aws configure list | grep region - this takes account of environment variables and ~/.aws/config
    – RichVel
    Commented Mar 29, 2018 at 7: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
    Commented Oct 22, 2019 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
    Commented Jan 26, 2020 at 1:20
  • 3
    If using the metadata service anyhow, /latest/meta-data/placement/region has been added since NSR posted the answer.
    – Ti Strga
    Commented Oct 8, 2022 at 17:10
19

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
13

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
2
  • Not from inside a CLI script on a client machine. This works on the instance. Thanks anyway.
    – mckenzm
    Commented May 2, 2019 at 0: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
    Commented Nov 13, 2019 at 22:58
12

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
6

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.

1
  • 1
    This seems a good enough answer that covers most "good faith" setup. Not sure whether it is absolutely foolproof because things like .aws/config profile setup for MFA, AWS SSO, or env vars AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, etc., may potentially derail the logic. I haven't tested, but it's too mind-boggling to be certain that we've covered all scenarios. In the end, the only foolproof method is the Python boto3 test (which of course has its own baggage.) Commented Jun 9, 2022 at 15:52
5

The region can be obtained using:

curl -s http://169.254.169.254/latest/meta-data/placement/region
1
  • Yeah, that sounds obvious, but that tells you the region of the "client". The client may be a laptop. This does not work on a laptop. The original question's intent was to test for the region a given scripted command would be applied in. This is easier nowadays.
    – mckenzm
    Commented Oct 23, 2022 at 20:57
2

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.

1
  • 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
    Commented Jan 24, 2020 at 2:09
1

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
-3

Just execute vi ~/.aws/config to see your config file.

1
  • 1
    This works if you pick one and stay with it. The config file generally has the default, and it may not be the same for all profiles. I may use the same snippet after the profile has been changed in the including script. This was some time ago now, when capacity was more of an unknown, it still applies if you can't get the quota uplift or are logging this to a fixed location for audit or restart purposes.
    – mckenzm
    Commented Jun 29, 2022 at 19:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.