Could not Connect to the Endpoint Url in AWS CLI [Solved]

avatar
Borislav Hadzhiev

Last updated: Feb 26, 2024
3 min

banner

# Could not Connect to the Endpoint Url in AWS CLI [Solved]

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

could not connect endpoint url

In order to solve the "Could not connect to the endpoint URL" error, set the region to a valid AWS region code, e.g. us-east-1.

shell
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 running 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 a 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. It applies to all commands that are run by the AWS CLI profile when the --region parameter and the AWS_DEFAULT_REGIONenvironment 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.

shell
# 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 run 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).

shell
# 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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.