| 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-elastic-ip-addresses: |
| 12 |
|
| 13 |
######################################## |
| 14 |
Using Elastic IP addresses in Amazon EC2 |
| 15 |
######################################## |
| 16 |
|
| 17 |
This Python example shows you how to: |
| 18 |
|
| 19 |
* Get descriptions of your Elastic IP addresses |
| 20 |
|
| 21 |
* Allocate an Elastic IP address |
| 22 |
|
| 23 |
* Release an Elastic IP address |
| 24 |
|
| 25 |
The scenario |
| 26 |
============ |
| 27 |
|
| 28 |
An Elastic IP address is a static IP address designed for dynamic cloud computing. An Elastic IP |
| 29 |
address is associated with your AWS account. It is a public IP address, which is reachable from the |
| 30 |
Internet. If your instance does not have a public IP address, you can associate an Elastic IP address |
| 31 |
with your instance to enable communication with the Internet. |
| 32 |
|
| 33 |
In this example, Python code performs several Amazon EC2 operations involving Elastic IP addresses. |
| 34 |
The code uses the AWS SDK for Python to manage IAM access keys using these methods of the EC2 |
| 35 |
client class: |
| 36 |
|
| 37 |
* `describe_addresses <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_addresses>`_. |
| 38 |
|
| 39 |
* `allocate_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.allocate_address>`_. |
| 40 |
|
| 41 |
* `release_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.release_address>`_. |
| 42 |
|
| 43 |
For more information about Elastic IP addresses in Amazon EC2, see |
| 44 |
`Elastic IP Addresses <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html>`_ |
| 45 |
in the *Amazon EC2 User Guide for Linux Instances* or |
| 46 |
`Elastic IP Addresses <http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/elastic-ip-addresses-eip.html>`_ in the *Amazon EC2 User Guide for Windows Instances*. |
| 47 |
Prerequisite Tasks |
| 48 |
|
| 49 |
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>`_. |
| 50 |
|
| 51 |
Prerequisite tasks |
| 52 |
================= |
| 53 |
|
| 54 |
To set up and run this example, you must first configure your AWS credentials, as described in :doc:`quickstart`. |
| 55 |
|
| 56 |
Describe Elastic IP addresses |
| 57 |
============================= |
| 58 |
|
| 59 |
An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. An Elastic IP |
| 60 |
address is associated with your AWS account. With an Elastic IP address, you can mask the failure of |
| 61 |
an instance or software by rapidly remapping the address to another instance in your account. |
| 62 |
|
| 63 |
The example below shows how to: |
| 64 |
|
| 65 |
* Describe Elastic IP addresses using |
| 66 |
`describe_addresses <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_addresses>`_. |
| 67 |
|
| 68 |
Example |
| 69 |
------- |
| 70 |
|
| 71 |
.. code-block:: python |
| 72 |
|
| 73 |
import boto3 |
| 74 |
|
| 75 |
|
| 76 |
ec2 = boto3.client('ec2') |
| 77 |
filters = [ |
| 78 |
{'Name': 'domain', 'Values': ['vpc']} |
| 79 |
] |
| 80 |
response = ec2.describe_addresses(Filters=filters) |
| 81 |
print(response) |
| 82 |
|
| 83 |
Allocate and associate an Elastic IP address with an Amazon EC2 instance |
| 84 |
======================================================================== |
| 85 |
|
| 86 |
An *Elastic IP address* is a static IPv4 address designed for dynamic cloud computing. An Elastic IP |
| 87 |
address is associated with your AWS account. With an Elastic IP address, you can mask the failure of |
| 88 |
an instance or software by rapidly remapping the address to another instance in your account. |
| 89 |
|
| 90 |
The example below shows how to: |
| 91 |
|
| 92 |
* Acquire an Elastic IP address using |
| 93 |
`allocate_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.allocate_address>`_. |
| 94 |
|
| 95 |
Example |
| 96 |
------- |
| 97 |
|
| 98 |
.. code-block:: python |
| 99 |
|
| 100 |
import boto3 |
| 101 |
from botocore.exceptions import ClientError |
| 102 |
|
| 103 |
ec2 = boto3.client('ec2') |
| 104 |
|
| 105 |
try: |
| 106 |
allocation = ec2.allocate_address(Domain='vpc') |
| 107 |
response = ec2.associate_address(AllocationId=allocation['AllocationId'], |
| 108 |
InstanceId='INSTANCE_ID') |
| 109 |
print(response) |
| 110 |
except ClientError as e: |
| 111 |
print(e) |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
Release an Elastic IP address |
| 116 |
============================= |
| 117 |
|
| 118 |
After releasing an Elastic IP address, it is released to the IP address pool and might be unavailable |
| 119 |
to you. Be sure to update your DNS records and any servers or devices that communicate with the address. |
| 120 |
If you attempt to release an Elastic IP address that you already released, you'll get an :code:`AuthFailure` |
| 121 |
error if the address is already allocated to another AWS account. |
| 122 |
|
| 123 |
The example below shows how to: |
| 124 |
|
| 125 |
* Release the specified Elastic IP address using |
| 126 |
`release_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.release_address>`_. |
| 127 |
|
| 128 |
Example |
| 129 |
------- |
| 130 |
|
| 131 |
.. code-block:: python |
| 132 |
|
| 133 |
import boto3 |
| 134 |
from botocore.exceptions import ClientError |
| 135 |
|
| 136 |
|
| 137 |
ec2 = boto3.client('ec2') |
| 138 |
|
| 139 |
try: |
| 140 |
response = ec2.release_address(AllocationId='ALLOCATION_ID') |
| 141 |
print('Address released') |
| 142 |
except ClientError as e: |
| 143 |
print(e) |
| 144 |
|