AWS

How to change your AWS Credential Region in CLI?

CK CK 3788 
Created at  
749 0 0

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 configure

This 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 Directly

The 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 Variables

Environment 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.

Tags AWS Access Key ID AWS Region AWS Secret Access Key aws configure Facebook X
Comments 0
Similar posts
  1. Synchronous online gaming architecutre in AWS - Highly Available, Scalable & Elastic to support Millions of Players
    10,414
  1. Architectural Diagram Gallery for Amazon MSK
    497
  2. Architectural Diagram Gallary for Amazon Kinesis
    517
  3. Real-time Data Streaming Solutions - Kafka vs. Kinesis Comparison
    847
  4. Building a Video Content Recommendation System with Amazon SageMaker
    521
  5. The Immense Scale and Management of Exabyte Data with AWS
    504
  6. Default Timeout for Amazon Bedrock invoke_model
    732
  7. When you face 504 Timeout Errors by Amazon ALB
    1,466
  8. When you face 504 Gateway Timeout while you wait a response from Bedrock in Fast API
    532
  9. How to overcome the prompt size limit when you use Claude via Bedrock - Chunking / Splitting the Input
    494
  10. Integrating VectorDB stored in S3 with Amazon Bedrock
    421
  11. When you need AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY for Amazon Bedrock
    532
  12. How Netflix Runs Recommendations for Hundreds of Millions of Users Using Amazon SageMaker & Metaflow
    171
  13. Why Amazon VPC satisfies with PCI-DSS and SOC 2 Type on Fintech Industry?
    641
  14. You shouldn't use Amazon CloudFront distribution as an origin
    630
  15. Amagi CLOUDPORT Architecture based on AWS for FAST for channel playout on AWS
    1,315
  16. Creating a serverless Apache Kafka publisher using AWS Lambda
    562
  17. AWS System Design Example - Instagram Architecture
    1,916
  18. A Python script to create an EC2 Spot instance using boto3
    1,598
  19. A Python/PHP code to deregister anormalous instaces in Amazon ECS
    1,855
  20. Where to get AWS API Key at AWS Management Console?
    1,062
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    27
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    24
  3. How do I create animated background for Google Meet?
    19,712
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    217
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    85
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    153
  7. Array of arguments passed to script in PHP
    85
  8. Invalid Auction Storms in Ad Tech
    93
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    198
  10. The Two Clocks of Ad Server Architecture
    63
  11. Frequency Cap Architecture Overview
    509
  12. Collecting Proof-of-Play from 100,000 Screens
    141
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    280
  14. What is RedisVL?
    105
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    224
  16. TPU & GPU Cluster Management Tools
    232
  17. The Concept of Scaffolding in AI Agents
    221
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    576
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    253
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    327
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    197
  22. Machine Learning: Training vs. Ranking
    213
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    217
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    189
  25. Designing a Data Curation System Around a Vector Database
    215
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    229
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    649
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    185
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    207
  30. Understanding Data Lineage
    252