Check Availability Zones using AWS CLI

How to check AWS availability zones using AWS CLI

When you create an instance in EC2 or Lightsail, you would like to select the right availability zone for the instance like us-east-1-a. You want to create an instance in the region that is closest to your audience. For that, you must the region where you will create the instance and then select an availability zone from that region. To know the availability zone for your instance, you will first want to know the region like us-east-1 or Us-west-2.

Once you know the right region for your instance, you can find out the availability zones that are enabled in your account or all of them in that particular region.

Suppose, you want to create an instance that can serve audiences in London or NewYork. For that purpose let’s check out the regions that are best suited for the job.

Make sure you have the aws cli installed locally and configured for use. To install the AWS CLI on your windows machine, just run the following command in windows powershell:

$ msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi

You can confirm the installation by typing:

$ aws –version

An output like the following will show that the CLI is installed:

$ aws-cli/2.10.3 Python/3.9.11 Windows/10 exe/AMD64 prompt/off

Now configure it with aws-configure:

$ aws configure

Provide the necessary information when prompted including access key id, secret access key and the default region name for your account:

AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE

AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

Default region name [None]: us-west-2

Default output format [None]: json

Now, that you have the aws cli installed and configured, you can start using it for creating instances and doing other things.

To check out regions, run the following command:

$ aws ec2 describe-regions

It will output a list of regions in the following manner:

{

    “Regions”: [

        {

            “Endpoint”: “ec2.ap-south-1.amazonaws.com”,

            “RegionName”: “ap-south-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.eu-north-1.amazonaws.com”,

            “RegionName”: “eu-north-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.eu-west-3.amazonaws.com”,

            “RegionName”: “eu-west-3”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.eu-west-2.amazonaws.com”,

            “RegionName”: “eu-west-2”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.eu-west-1.amazonaws.com”,

            “RegionName”: “eu-west-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.ap-northeast-3.amazonaws.com”,

            “RegionName”: “ap-northeast-3”,

            “OptInStatus”: “opt-in-not-required”

},

        {

            “Endpoint”: “ec2.ap-northeast-2.amazonaws.com”,

            “RegionName”: “ap-northeast-2”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.ap-northeast-1.amazonaws.com”,

            “RegionName”: “ap-northeast-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.ca-central-1.amazonaws.com”,

            “RegionName”: “ca-central-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.sa-east-1.amazonaws.com”,

            “RegionName”: “sa-east-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.ap-southeast-1.amazonaws.com”,

            “RegionName”: “ap-southeast-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.ap-southeast-2.amazonaws.com”,

            “RegionName”: “ap-southeast-2”,

     “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.eu-central-1.amazonaws.com”,

            “RegionName”: “eu-central-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.us-east-1.amazonaws.com”,

            “RegionName”: “us-east-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.us-east-2.amazonaws.com”,

            “RegionName”: “us-east-2”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.us-west-1.amazonaws.com”,

            “RegionName”: “us-west-1”,

            “OptInStatus”: “opt-in-not-required”

        },

        {

            “Endpoint”: “ec2.us-west-2.amazonaws.com”,

            “RegionName”: “us-west-2”,

            “OptInStatus”: “opt-in-not-required”

        }

    ]

}

If your audience in located in the United States, you will select a region from the United States and if in EU or Asia Pacific then select the ones enabled for EU or AP.

Now to know, the availability zones in a particular region like eu-central-1 or us-east-1 run the command describe-availability-zones with region flag.

$ aws ec2 describe-availability-zones --region us-east-1

Or

$ aws ec2 describe-availability-zones --region eu-central-1

Check out the sample output below:

{

    “AvailabilityZones”: [

        {

            “State”: “available”,

            “OptInStatus”: “opt-in-not-required”,

            “Messages”: [],

            “RegionName”: “eu-central-1”,

            “ZoneName”: “eu-central-1a”,

            “ZoneId”: “euc1-az2”,

            “GroupName”: “eu-central-1”,

            “NetworkBorderGroup”: “eu-central-1”,

            “ZoneType”: “availability-zone”

        },

        {

            “State”: “available”,

            “OptInStatus”: “opt-in-not-required”,

            “Messages”: [],

            “RegionName”: “eu-central-1”,

            “ZoneName”: “eu-central-1b”,

            “ZoneId”: “euc1-az3”,

            “GroupName”: “eu-central-1”,

            “NetworkBorderGroup”: “eu-central-1”,

            “ZoneType”: “availability-zone”

        },

        {

            “State”: “available”,

            “OptInStatus”: “opt-in-not-required”,

            “Messages”: [],

            “RegionName”: “eu-central-1”,

            “ZoneName”: “eu-central-1c”,

            “ZoneId”: “euc1-az1”,

— More  —

You can see in the above output that every availability zone has a zone name like eu-central-1c which is the region name followed by a letter identifier and a zone id like euc1-az1.

One more command that can be helpful at learning more about geographical regions enabled for your account is the ‘aws lightsail get-regions’ command. It will give you a detailed output highlighting which region is best suited for which geographical areas like the following:

{

    “regions”: [

        {

            “continentCode”: “NA”,

            “description”: “This region is recommended to serve users in the eastern United States”,

            “displayName”: “Virginia”,

            “name”: “us-east-1”,

            “availabilityZones”: [],

            “relationalDatabaseAvailabilityZones”: []

        },

        {

            “continentCode”: “NA”,

            “description”: “This region is recommended to serve users in the eastern United States”,

            “displayName”: “Ohio”,

            “name”: “us-east-2”,

            “availabilityZones”: [],

            “relationalDatabaseAvailabilityZones”: []

        },

        {

            “continentCode”: “NA”,

            “description”: “This region is recommended to serve users in the northwestern United States, Alaska, and western Canada”,            “displayName”: “Oregon”,

            “name”: “us-west-2”,

            “availabilityZones”: [],

            “relationalDatabaseAvailabilityZones”: []

        },

        {

            “continentCode”: “EU”,

            “description”: “This region is recommended to serve users in Ireland, the United Kingdom, and Iceland”,

            “displayName”: “Ireland”,

            “name”: “eu-west-1”,

— More  —

In the above output, you can see that AWS provides a description for the region with the geographical areas you can serve from there. If you want an instance for your audience in London, then eu-west-1 region might be most suitable.