AWS SDK for Ruby
Developer Guide

Using Elastic IP Addresses in Amazon EC2

An Elastic IP address is a static IP address, designed for dynamic cloud computing, that is associated with your AWS account. It's a public IP address, which is reachable from the internet. If your instance doesn't have a public IP address, you can use an Elastic IP address with your instance so that it can communicate with the internet.

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.

In this example, you use the AWS SDK for Ruby with Amazon EC2 to:

  1. Allocate an Elastic IP address by using the Aws::EC2::Client#allocate_address method.

  2. Associate the address with an Amazon EC2 instance by using the Aws::EC2::Client#associate_address method.

  3. Get information about addresses associated with the instance by using the Aws::EC2::Client#describe_addresses method.

  4. Release the address by using the Aws::EC2::Client#release_address method.

The complete code for this example is available on GitHub.

Prerequisites

Before working with the example code, you need to install and configure the AWS SDK for Ruby, as described in:

You also need to launch an EC2 instance and note the instance ID.

Note

Before you run the following code, you must replace the INSTANCE-ID string with your actual instance ID. This will be something like i-0a123456b7c8defg9.

Example

require 'aws-sdk-ec2' # v2: require 'aws-sdk' ec2 = Aws::EC2::Client.new(region: 'us-east-1') instance_id = "INSTANCE-ID" # For example, "i-0a123456b7c8defg9" def display_addresses(ec2, instance_id) describe_addresses_result = ec2.describe_addresses({ filters: [ { name: "instance-id", values: [ instance_id ] }, ] }) if describe_addresses_result.addresses.count == 0 puts "No addresses currently associated with the instance." else describe_addresses_result.addresses.each do |address| puts "=" * 10 puts "Allocation ID: #{address.allocation_id}" puts "Association ID: #{address.association_id}" puts "Instance ID: #{address.instance_id}" puts "Public IP: #{address.public_ip}" puts "Private IP Address: #{address.private_ip_address}" end end end puts "Before allocating the address for the instance...." display_addresses(ec2, instance_id) puts "\nAllocating the address for the instance..." allocate_address_result = ec2.allocate_address({ domain: "vpc" }) puts "\nAfter allocating the address for instance, but before associating the address with the instance..." display_addresses(ec2, instance_id) puts "\nAssociating the address with the instance..." associate_address_result = ec2.associate_address({ allocation_id: allocate_address_result.allocation_id, instance_id: instance_id, }) puts "\nAfter associating the address with the instance, but before releasing the address from the instance..." display_addresses(ec2, instance_id) puts "\nReleasing the address from the instance..." ec2.release_address({ allocation_id: allocate_address_result.allocation_id, }) puts "\nAfter releasing the address from the instance..." display_addresses(ec2, instance_id)

On this page: