.. Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
.. SPDX-License-Identifier: Apache-2.0

.. _aws-boto-ec2-example-regions-zones:

##################################################
Describe Amazon EC2 Regions and Availability Zones
##################################################

Amazon EC2 is hosted in multiple locations worldwide. These locations are composed of regions and 
Availability Zones. Each region is a separate geographic area. Each region has multiple, isolated 
locations known as Availability Zones. Amazon EC2 provides the ability to place instances and data 
in multiple locations.

The scenario
============

In this example, Python code is used to get details about regions and Availability Zones. The code uses the 
AWS SDK for Python to get the data by using these methods of the EC2 client class:
 
* `describe_regions <https://docs.aws.amazon.com/boto3/latest/reference/services/ec2/client/describe_regions.html>`_. 

* `describe_availability_zones <https://docs.aws.amazon.com/boto3/latest/reference/services/ec2/client/describe_availability_zones.html>`_. 
 
For more information about regions and Availability Zones, see 
`Regions and Availability Zones <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html>`_ 
in the *Amazon EC2 User Guide for Linux Instances* or 
`Regions and Availability Zones <https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/concepts.html>`_ 
in the *Amazon EC2 User Guide for Windows Instances*.

All the example code for the Amazon Web Services (AWS) SDK for Python is available `here on GitHub <https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/python/example_code>`_.

Prerequisite tasks
=================

To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`.

Describe Regions and Availability Zones
=======================================

* Describe one or more Regions that are currently available to you. 

* Describe one or more of the Availability Zones that are available to you. The results include zones 
  only for the region you're currently using. If there is an event impacting an Availability Zone, 
  you can use this request to view the state and any provided message for that Availability Zone.

The example below shows how to:
 
* Describe Regions using 
  `describe_regions <https://docs.aws.amazon.com/boto3/latest/reference/services/ec2/client/describe_regions.html>`_.

* Describe Availability Zones using 
  `describe_availability_zones <https://docs.aws.amazon.com/boto3/latest/reference/services/ec2/client/describe_availability_zones.html>`_.
 
Example
-------

.. code-block:: python

        import boto3

        ec2 = boto3.client('ec2')

        # Retrieves all regions/endpoints that work with EC2
        response = ec2.describe_regions()
        print('Regions:', response['Regions'])

        # Retrieves availability zones only for region of the ec2 object
        response = ec2.describe_availability_zones()
        print('Availability Zones:', response['AvailabilityZones'])
