| 1 |
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto-ec2-example-regions-zones: |
| 12 |
|
| 13 |
################################################## |
| 14 |
Describe Amazon EC2 Regions and Availability Zones |
| 15 |
################################################## |
| 16 |
|
| 17 |
Amazon EC2 is hosted in multiple locations worldwide. These locations are composed of regions and |
| 18 |
Availability Zones. Each region is a separate geographic area. Each region has multiple, isolated |
| 19 |
locations known as Availability Zones. Amazon EC2 provides the ability to place instances and data |
| 20 |
in multiple locations. |
| 21 |
|
| 22 |
The scenario |
| 23 |
============ |
| 24 |
|
| 25 |
In this example, Python code is used to get details about regions and Availability Zones. The code uses the |
| 26 |
AWS SDK for Python to get the data by using these methods of the EC2 client class: |
| 27 |
|
| 28 |
* `describe_regions <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_regions>`_. |
| 29 |
|
| 30 |
* `describe_availability_zones <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_availability_zones>`_. |
| 31 |
|
| 32 |
For more information about regions and Availability Zones, see |
| 33 |
`Regions and Availability Zones <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html>`_ |
| 34 |
in the *Amazon EC2 User Guide for Linux Instances* or |
| 35 |
`Regions and Availability Zones <https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/concepts.html>`_ |
| 36 |
in the *Amazon EC2 User Guide for Windows Instances*. |
| 37 |
|
| 38 |
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>`_. |
| 39 |
|
| 40 |
Prerequisite tasks |
| 41 |
================= |
| 42 |
|
| 43 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 44 |
|
| 45 |
Describe Regions and Availability Zones |
| 46 |
======================================= |
| 47 |
|
| 48 |
* Describe one or more Regions that are currently available to you. |
| 49 |
|
| 50 |
* Describe one or more of the Availability Zones that are available to you. The results include zones |
| 51 |
only for the region you're currently using. If there is an event impacting an Availability Zone, |
| 52 |
you can use this request to view the state and any provided message for that Availability Zone. |
| 53 |
|
| 54 |
The example below shows how to: |
| 55 |
|
| 56 |
* Describe Regions using |
| 57 |
`describe_regions <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_regions>`_. |
| 58 |
|
| 59 |
* Describe Availability Zones using |
| 60 |
`describe_availability_zones <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_availability_zones>`_. |
| 61 |
|
| 62 |
Example |
| 63 |
------- |
| 64 |
|
| 65 |
.. code-block:: python |
| 66 |
|
| 67 |
import boto3 |
| 68 |
|
| 69 |
ec2 = boto3.client('ec2') |
| 70 |
|
| 71 |
# Retrieves all regions/endpoints that work with EC2 |
| 72 |
response = ec2.describe_regions() |
| 73 |
print('Regions:', response['Regions']) |
| 74 |
|
| 75 |
# Retrieves availability zones only for region of the ec2 object |
| 76 |
response = ec2.describe_availability_zones() |
| 77 |
print('Availability Zones:', response['AvailabilityZones']) |