File: ec2-example-elastic-ip-addresses.rst

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (144 lines) | stat: -rw-r--r-- 5,559 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.

   This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
   International License (the "License"). You may not use this file except in compliance with the
   License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.

   This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
   either express or implied. See the License for the specific language governing permissions and
   limitations under the License.
   
.. _aws-boto-ec2-example-elastic-ip-addresses:

########################################
Using Elastic IP addresses in Amazon EC2
########################################

This Python example shows you how to:

* Get descriptions of your Elastic IP addresses

* Allocate an Elastic IP address

* Release an Elastic IP address

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

An Elastic IP address is a static IP address designed for dynamic cloud computing. An Elastic IP 
address is associated with your AWS account. It is a public IP address, which is reachable from the 
Internet. If your instance does not have a public IP address, you can associate an Elastic IP address 
with your instance to enable communication with the Internet.

In this example, Python code performs several Amazon EC2 operations involving Elastic IP addresses. 
The code uses the AWS SDK for Python to manage IAM access keys using these methods of the EC2
client class:

* `describe_addresses <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_addresses>`_.

* `allocate_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.allocate_address>`_.

* `release_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.release_address>`_.

For more information about Elastic IP addresses in Amazon EC2, see 
`Elastic IP Addresses <http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html>`_ 
in the *Amazon EC2 User Guide for Linux Instances* or 
`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*.
Prerequisite Tasks

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 Elastic IP addresses
=============================

An Elastic IP address is a static IPv4 address designed for dynamic cloud computing. An Elastic IP 
address is associated with your AWS account. With an Elastic IP address, you can mask the failure of 
an instance or software by rapidly remapping the address to another instance in your account. 

The example below shows how to:
 
* Describe Elastic IP addresses using 
  `describe_addresses <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_addresses>`_.
 
Example
-------

.. code-block:: python

    import boto3


    ec2 = boto3.client('ec2')
    filters = [
        {'Name': 'domain', 'Values': ['vpc']}
    ]
    response = ec2.describe_addresses(Filters=filters)
    print(response)

Allocate and associate an Elastic IP address with an Amazon EC2 instance
========================================================================

An *Elastic IP address* is a static IPv4 address designed for dynamic cloud computing. An Elastic IP 
address is associated with your AWS account. With an Elastic IP address, you can mask the failure of 
an instance or software by rapidly remapping the address to another instance in your account. 

The example below shows how to:
 
* Acquire an Elastic IP address using 
  `allocate_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.allocate_address>`_.
 
Example
-------

.. code-block:: python

    import boto3
    from botocore.exceptions import ClientError

    ec2 = boto3.client('ec2')

    try:
        allocation = ec2.allocate_address(Domain='vpc')
        response = ec2.associate_address(AllocationId=allocation['AllocationId'],
                                         InstanceId='INSTANCE_ID')
        print(response)
    except ClientError as e:
        print(e)


 
Release an Elastic IP address
=============================

After releasing an Elastic IP address, it is released to the IP address pool and might be unavailable 
to you. Be sure to update your DNS records and any servers or devices that communicate with the address. 
If you attempt to release an Elastic IP address that you already released, you'll get an :code:`AuthFailure` 
error if the address is already allocated to another AWS account.

The example below shows how to:
 
* Release the specified Elastic IP address using 
  `release_address <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.release_address>`_.
 
Example
-------

.. code-block:: python

    import boto3
    from botocore.exceptions import ClientError


    ec2 = boto3.client('ec2')

    try:
        response = ec2.release_address(AllocationId='ALLOCATION_ID')
        print('Address released')
    except ClientError as e:
        print(e)