Solve - Could not Connect to the Endpoint Url in AWS CLI


Borislav Hadzhiev

Wed Sep 15 20212 min read


Photo by Chad Madden

Solving AWS CLI Could not connect to Endpoint Url #

The reason the "Could not connect to the endpoint URL" error occurs is because an incorrect region code is set when executing the CLI command.

could not connect endpoint url

In order to solve the "Could not connect to the endpoint URL" error we have to set the region to a valid AWS region code:

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

with correct region parameter

To view all the valid region codes check out the Available AWS Regions table and look at the Region column.

The region we set is included in the endpoint URL, therefore if we specify an incorrect region we are making an HTTP request to an invalid endpoint URL.

There are multiple ways to set the region that applies to a CLI command. In order of precedence:

  1. Set the --region=us-east-1 parameter when executing the command. This approach has the highest precedence and overrides the other 2. However, you have to pass the parameter with every command.
  2. Set the AWS_DEFAULT_REGION environment variable. This approach often leads to confusion. It has lower precedence than the --region parameter, but overrides the region setting in the AWS config file.
  3. Set the region in your AWS config - has the lowest precedence. Applies to all commands executed by the AWS CLI profile when the --region parameter and the AWS_DEFAULT_REGION environment variable are not set.
The most intuitive way to manage your AWS region is to set the most commonly used region for a profile in your AWS config and override the value with the--region parameter when needed.

To update the region of an AWS CLI profile run the aws configure set region command:

# Update the region of the profile john aws configure set region us-east-1 --profile john # Update the region of the default profile aws configure set region us-west-1 --profile default

The aws configure set region command updates the setting in the following file:

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

aws cli config file

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

Having set a correct AWS region code for a profile in your config file, means that the next time you execute a command for that profile and don't pass the --region parameter the AWS CLI will look up the region value in your config file (assuming the AWS_DEFAULT_REGION variable is not set on your machine):

# Uses the region configured for the john profile aws ec2 describe-instances --profile john # Uses the region configured for the default profile aws ec2 describe-instances

Further Reading #

Add me on LinkedIn

I'm a Web Developer with TypeScript, React.js, Node.js and AWS experience.

Let's connect on LinkedIn

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee