| Your AWS credentials (Access Key ID and Secret Access Key) are global, but the services you interact with are regional. When you make an API call, you need to specify which region you want to operate in. Here are the primary ways to change your AWS CLI region, listed roughly in order of precedence (from lowest to highest): 1. Using aws configure (for default profile or named profiles) 2. Editing Configuration Files (~/.aws/config or ~/.aws/credentials) 3. Setting Environment Variables 4. Using the --region flag (per-command override) Let's break down each method: 1. Using aws configureThis is the most common and recommended way to set or change your default region for a profile. For your default profile:If you just run aws configure without specifying a profile, it will modify the settings for your default profile. 1. Open your terminal or command prompt. 2. Run the command: aws configure
3. It will prompt you for four pieces of information. If you only want to change the region, you can press Enter to keep your existing Access Key ID, Secret Access Key, and Output Format, and just type your desired region name when prompted for "Default region name". AWS Access Key ID [****************ABCD]:
AWS Secret Access Key [****************ABCD]:
Default region name [us-east-1]: eu-west-1 <-- Type your new region here
Default output format [json]:
In this example, the default region for your default profile would be changed to eu-west-1. For a named profile:If you're using named profiles (e.g., dev, prod, marketing), you need to specify the profile name. 1. Run the command with the --profile flag: aws configure --profile my-dev-profile
2. Again, it will prompt you. Enter your new region when prompted: AWS Access Key ID [****************EFGH]:
AWS Secret Access Key [****************EFGH]:
Default region name [us-west-2]: ap-southeast-2 <-- Type your new region here
Default output format [json]:
This changes the region only for my-dev-profile. When you use aws --profile my-dev-profile ..., it will now target ap-southeast-2. 2. Editing Configuration Files DirectlyThe AWS CLI stores its configuration in plain text files in the ~/.aws/ directory (or %USERPROFILE%\.aws\ on Windows). ~/.aws/config: This is where regions are typically configured.~/.aws/credentials: This file primarily stores Access Key IDs and Secret Access Keys, but you can technically add a region here too. However, it's best practice to keep region settings in ~/.aws/config.
Example ~/.aws/config:1. Open this file in a text editor (e.g., nano ~/.aws/config or notepad %USERPROFILE%\.aws\config). 2. You'll see sections like this: [default]
region = us-east-1
output = json
[profile my-dev-profile]
region = us-west-2
output = json
3. To change a region, simply edit the region = line under the desired profile. Before: [default]
region = us-east-1
After: [default]
region = eu-central-1
4. Save the file. 3. Setting Environment VariablesEnvironment variables provide a way to temporarily override the region set in your configuration files for the current shell session. AWS_REGION: This variable takes precedence over AWS_DEFAULT_REGION and any configuration file settings (except the --region flag).AWS_DEFAULT_REGION: This is an older variable, but still supported. It's generally better to use AWS_REGION.
For Linux/macOS (Bash/Zsh):export AWS_REGION=us-west-2
# Or:
export AWS_DEFAULT_REGION=us-west-2
For Windows (Command Prompt):set AWS_REGION=us-west-2
REM Or:
set AWS_DEFAULT_REGION=us-west-2
For Windows (PowerShell):$env:AWS_REGION="us-west-2"
# Or:
$env:AWS_DEFAULT_REGION="us-west-2"
Note: Environment variables only last for the current shell session. If you close your terminal and open a new one, the variable will be gone. To make it persistent, you'd add it to your shell's profile file (e.g., ~/.bashrc, ~/.zshrc, ~/.profile). 4. Using the --region flag (Per-Command Override)This is the highest precedence method and allows you to specify a region for a single, specific AWS CLI command, overriding all other settings. aws s3 ls --region ap-south-1
In this example, even if your default region is us-east-1 and you have AWS_REGION set to eu-west-1, this specific s3 ls command will execute against the ap-south-1 region. Order of Precedence (Highest to Lowest):1. --region flag (on the command line) 2. Environment variables (AWS_REGION or AWS_DEFAULT_REGION) 3. Profile setting in ~/.aws/config (e.g., [profile my-profile] region = ...) 4. Default profile setting in ~/.aws/config (e.g., [default] region = ...) How to check your current effective region:You can use the aws configure list command to see what region the CLI is currently using based on your active profile and environment variables: aws configure list
This will show you the configured region, access key, etc., and tell you where it's getting that value from (e.g., config, env, cli-arg). Alternatively, you can query a service that gives you region information, like STS: aws sts get-caller-identity --query 'Account'
# If this command fails due to region, it implies the region is not set correctly.
# Or:
aws ec2 describe-regions --query "Regions[?RegionName == '$(aws configure get region)'].{RegionName:RegionName}" --output text
Choose the method that best fits your workflow, whether it's setting a long-term default, a profile-specific default, a temporary override, or a one-off command. |