Invalid Endpoint Error in AWS CLI [Solved]

avatar

Borislav Hadzhiev

2 min

banner

Photo from Unsplash

# Invalid Endpoint Error in AWS CLI [Solved]

The "Invalid endpoint" error occurs when the region that applies to the AWS CLI command is not set properly and most often is set to an empty string:

invalid endpoint error

To solve the "Invalid endpoint" error set the --region parameter to a valid region code when executing the command:

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

region parameter set

To view all the available region codes, check out the AWS Region Availability List and look at the Region column.

The region we specify is used when composing the URL for the API request (the endpoint), therefore if we get the region code incorrect the whole endpoint becomes invalid.

To specify a region that applies to all commands executed by an AWS CLI profile we can use the aws configure set region command:

shell
# configure the region for the john profile aws configure set region us-east-1 --profile john # configure the region for the default profile aws configure set region us-west-1 --profile default

In the code sample, we set the region for the AWS profile john to us-east-1.

This means that any time this profile executes a CLI command without passing the --region parameter, the AWS CLI uses the configured region. For example:

shell
# use the john profile aws ec2 describe-instances --profile john # use the default profile aws ec2 describe-instances

The setting for the region is stored in the following files:

  • on Linux and macOS: ~/.aws/config

aws cli config file

  • on windows: C:\Users\USERNAME\.aws\config

There are 2 ways to override the region that's set in your config file:

  1. Pass the --region=another-region parameter when executing the command
  2. Set the AWS_DEFAULT_REGION environment variable in your shell. The AWS_DEFAULT_REGION environment variable has higher precedence than the specified region in your config file, but gets overridden when the --region parameter is passed.
The environment variable approach introduces confusion and should be avoided. It's best to use the aws configure set region command to set the most commonly used region for a profile and then override it by passing the --region parameter for one-off commands.

# Further Reading

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.