Sub topic 6
Setting and inspecting the Region from the CLI and SDK
This chapter assumes you have the AWS CLI installed, credentials configured, and a basic idea of what a Region is. You will learn how the CLI and boto3 decide which Region to use, and how to check it before you create anything.
Why the Region setting matters
Most AWS resources are regional. If a command runs against the wrong Region, you create resources where you did not intend, or you see an empty list and think your resources are gone. Knowing how the Region is chosen prevents both problems.
How the CLI picks a Region
The CLI looks for a Region in a fixed order and uses the first one it finds:
- The
--regionoption on the command. - The
AWS_REGIONenvironment variable (checked by the CLI and SDKs that support it). - The
AWS_DEFAULT_REGIONenvironment variable. - The
regionsetting in the active profile in~/.aws/config.
If none is set, many commands fail with an error saying that you must specify a Region.
Inspecting the current Region
Use aws configure get region to see what the CLI resolves from the environment and profile:
export AWS_DEFAULT_REGION=eu-west-1
aws configure get region
aws ec2 describe-availability-zones --query "AvailabilityZones[].ZoneName" --output textSetting the environment variable affects only the current shell. The second command then lists the Availability Zones in eu-west-1.
To save a default permanently, run aws configure set region eu-west-1. For a named profile, add --profile name.
Listing Regions
Two commands show which Regions you can use:
aws ec2 describe-regions --query "Regions[].RegionName" --output textThis lists the Regions enabled for your account. It leaves out opt-in Regions you have not enabled.
aws ec2 describe-regions --all-regions \
--query "Regions[?OptInStatus=='not-opted-in'].RegionName" \
--output textThis adds --all-regions and then filters, so you see only the opt-in Regions that are still disabled. Enabling them is covered in a later chapter.
Listing Availability Zones
Each Region has its own set of zones. Query a specific Region with --region:
aws ec2 describe-availability-zones --region us-east-1 \
--query "AvailabilityZones[].[ZoneName,ZoneId,State,ZoneType]" \
--output tableThe table shows the zone name, such as us-east-1a, next to its zone ID, such as use1-az1. Names are mapped differently for each account, while IDs identify the same physical zone for everyone.
Using boto3
boto3 follows a similar order. An explicit region_name argument wins. After that it uses AWS_REGION or AWS_DEFAULT_REGION, then the profile setting. Passing the Region in code makes a script's behavior clear to anyone reading it:
import boto3
ec2 = boto3.client("ec2", region_name="us-east-1")
response = ec2.describe_availability_zones(
Filters=[{"Name": "zone-type", "Values": ["availability-zone"]}]
)
for az in response["AvailabilityZones"]:
print(az["ZoneName"], az["ZoneId"], az["State"])The filter keeps only standard Availability Zones. Local Zones and Wavelength Zones are left out.
You can check which Region a client uses with ec2.meta.region_name.
Common errors
| Symptom | Likely cause | Fix | Column |
|---|---|---|---|
| Empty list of resources you know exist | Command ran in a different Region | Add `--region` or check `aws configure get region` | |
| "You must specify a region" | No Region from any source | Set `AWS_DEFAULT_REGION` or run `aws configure set region` | |
| Setting reverts in a new terminal | Variable was exported in only one shell | Save it in the profile with `aws configure set region` | |
| Wrong Region despite the profile | An environment variable overrides the profile | Run `env | grep AWS_` and unset the variable |
Placing a subnet by AZ ID
When you must place resources in the same physical zone across accounts, use the AZ ID instead of the name:
aws ec2 create-subnet \
--vpc-id vpc-0123456789abcdef0 \
--cidr-block 10.0.1.0/24 \
--availability-zone-id use1-az1The VPC ID here is a placeholder. Replace it with one from your account in the same Region.
Exercise
You run aws ec2 describe-instances and get an empty list, but you launched an instance yesterday. Your profile sets region = us-east-1. Name two things you should check.
Show answer
Check whether AWS_REGION or AWS_DEFAULT_REGION is set in your shell, since an environment variable overrides the profile. Also check whether you launched the instance in a different Region and pass --region to look there.