You must specify a region error AWS

You must specify a region error occurs when a default AWS Region is not configured.

Error:

1
You must specify a region. You can also configure your region by running "aws configure"

How to fix the You must specify a region error

Method 1: Set the AWS_DEFAULT_REGION environment variable

1
export AWS_DEFAULT_REGION="us-east-1"

Method 2: Include the default region in the ~/.aws/config file or run aws configure

1
2
3
4
5
> aws configure
  AWS Access Key ID [None]:
  AWS Secret Access Key [None]:
  Default region name [None]: us-east-1
  Default output format [None]:

The ~/.aws/config file will be generated with the following configuration:

1
2
[default]
region = us-east-1

Method 3: Explicity specify the region with the AWS API

AWS CLI:

1
aws ec2 describe-instances --region us-east-1

Python Boto3:

1
2
3
import boto3

kms = boto3.client("kms", region_name="us-east-1")

JavaScript AWS SDK

1
2
3
4
5
import { S3Client } from '@aws-sdk/client-s3';

const s3Client = new S3Client({
  region: 'us-east-1'
});