Using Elastic IP addresses in Amazon EC2 Boto3 Docs 1.16.24 documentation

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:

For more information about Elastic IP addresses in Amazon EC2, see Elastic IP Addresses in the Amazon EC2 User Guide for Linux Instances or Elastic IP Addresses 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.

Prerequisite tasks

To set up and run this example, you must first configure your AWS credentials, as described in 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:

Example

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:

Example

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 AuthFailure error if the address is already allocated to another AWS account.

The example below shows how to:

Example

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) 

ncG1vNJzZmian6m8dHrAppizp56WxLR6wqikaK5hZLGwr9SmnKeskam2sLqOmqeiZ2Fjfnd6kW1moK2ZmbJwscJrZJ6wkaK9rbGMnqOaq6SesG61z2aYnZyimsC0sdJnn62lnA%3D%3D