Manage Elastic IP Addresses with the AWS CLI

Static public IP resource connected to an EC2 instance through its allocation and association lifecycle

What You’ll Learn

In this lesson, you will use the AWS CLI to manage an Elastic IP address for an Amazon EC2 instance. An Elastic IP is a static public IPv4 address that you can keep and move between resources.

  • Allocate an Elastic IP address.
  • Inspect its public IP, allocation ID, and association.
  • Associate the address with an EC2 instance.
  • Disassociate and release the address when it is no longer needed.

These commands assume that the AWS CLI is configured, you know the target EC2 instance ID, and your IAM permissions allow EC2 address operations.

Ad

The Concept

An EC2 instance can receive a public IPv4 address, but that address may change when the instance is stopped and started. An Elastic IP address is a static public IPv4 address allocated to your AWS account.

Elastic IP addresses are useful when a service must be reachable at a predictable address, such as a small web server, a mail server, or an allowlist entry in another system. You can associate an Elastic IP with an EC2 instance and later move it to another instance if necessary.

The AWS CLI uses several identifiers when working with Elastic IP addresses:

  • Public IP: The address users connect to, such as 203.0.113.25.
  • Allocation ID: AWS’s identifier for the allocated Elastic IP, such as eipalloc-0123456789abcdef0.
  • Association ID: AWS’s identifier for the connection between the Elastic IP and an EC2 resource.
  • Instance ID: The identifier of the EC2 instance receiving the address.

The usual workflow is:

  1. Allocate an Elastic IP with aws ec2 allocate-address.
  2. Inspect it with aws ec2 describe-addresses.
  3. Associate it with an instance using aws ec2 associate-address.
  4. Disassociate it with aws ec2 disassociate-address when needed.
  5. Release it with aws ec2 release-address so it is no longer allocated to your account.

For more EC2 administration after assigning an address, see AWS CLI EC2 instance management.

Basic Example

The following Bash session allocates an Elastic IP in a VPC, checks its details, associates it with an EC2 instance, and checks the result. Replace the example instance ID with an existing instance in the selected Region.

REGION="us-east-1"
INSTANCE_ID="i-0123456789abcdef0"

ALLOCATION_ID=$(aws ec2 allocate-address \
    --domain vpc \
    --region "$REGION" \
    --query "AllocationId" \
    --output text)

echo "Allocated Elastic IP with allocation ID: $ALLOCATION_ID"

aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].[PublicIp,AllocationId,AssociationId]" \
    --output table

aws ec2 associate-address \
    --instance-id "$INSTANCE_ID" \
    --allocation-id "$ALLOCATION_ID" \
    --region "$REGION"

aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].[PublicIp,InstanceId,AssociationId]" \
    --output table

Expected Output

The exact IDs and public IP address will be different in your account. Before association, the association value may be empty. After association, the output should show the target instance ID and an association ID.

Allocated Elastic IP with allocation ID: eipalloc-0123456789abcdef0

---------------------------------------------
|            DescribeAddresses             |
+---------------+---------------------------+
| 203.0.113.25  | eipalloc-0123456789abcdef0 |
| None          | None                      |
+---------------+---------------------------+

---------------------------------------------
|            DescribeAddresses             |
+---------------+---------------------------+
| 203.0.113.25  | i-0123456789abcdef0       |
| eipassoc-0123456789abcdef0               |
+---------------+---------------------------+

How the Code Works

A top-to-bottom process showing an Elastic IP being allocated, inspected, associated with an EC2 instance, verified, then disassociated and released when no longer needed.
An Elastic IP moves from allocation through inspection and EC2 association to verification, cleanup, and release.

REGION stores the AWS Region where the address will be created. Elastic IP addresses are Regional resources, so the address and the EC2 instance must be in the same Region.

INSTANCE_ID identifies the EC2 instance that will receive the address. The instance must exist in the selected Region.

This command allocates the address:

aws ec2 allocate-address --domain vpc --region "$REGION"

The --domain vpc option tells AWS to allocate an address for use in a VPC. The command returns a JSON response containing values such as PublicIp and AllocationId. The --query "AllocationId" option selects only the allocation ID, and --output text makes it convenient to store in the Bash variable.

The describe-addresses command inspects an allocated address. Its query selects the public IP, allocation ID, and association ID. Before association, the address does not have an instance association.

This command associates the address with the instance:

aws ec2 associate-address \
    --instance-id "$INSTANCE_ID" \
    --allocation-id "$ALLOCATION_ID" \
    --region "$REGION"

After this command succeeds, traffic sent to the Elastic IP is directed to the instance, subject to the instance’s security group, subnet routing, and operating system configuration.

Allocating an address does not automatically associate it with an instance. Also, associating an address does not configure an application to listen for traffic. Those are separate tasks.

Another Example

A common operational task is cleaning up an Elastic IP after a temporary server has been removed. First inspect the address, retrieve its association ID, disassociate it if necessary, and then release it.

Use the allocation ID of an address you own in the selected Region. Do not release an address that another running workload still needs.

REGION="us-east-1"
ALLOCATION_ID="eipalloc-0123456789abcdef0"

aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].[PublicIp,InstanceId,NetworkInterfaceId,AssociationId]" \
    --output table

ASSOCIATION_ID=$(aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].AssociationId" \
    --output text)

if [ "$ASSOCIATION_ID" != "None" ]; then
    aws ec2 disassociate-address \
        --association-id "$ASSOCIATION_ID" \
        --region "$REGION"
fi

aws ec2 release-address \
    --allocation-id "$ALLOCATION_ID" \
    --region "$REGION"

echo "Elastic IP released."

The conditional prevents the disassociate command from running when the address is already unassociated. After the address is disassociated, release-address returns it to AWS. Once released, you cannot assume that the same public IP can be allocated again.

Common Mistakes

  • Using the public IP where an allocation ID is required: Commands such as associate-address and release-address use the allocation ID. Use describe-addresses to find it.
  • Forgetting the Region: An Elastic IP and its target instance must be in the same Region. Set --region explicitly when working with multiple Regions.
  • Releasing instead of disassociating: Disassociation removes the connection to an instance but keeps the address allocated. Releasing permanently gives up the address.
  • Leaving unused addresses allocated: AWS may charge for Elastic IP addresses that are not associated with a running resource. Release temporary addresses when they are no longer needed.
  • Expecting association to open network traffic: Security groups, network ACLs, routes, and the server application still control whether traffic can reach the instance.

Try It Yourself

Choose an existing EC2 instance and complete this workflow in a test Region:

  1. Allocate a VPC Elastic IP.
  2. Use describe-addresses to display its public IP and allocation ID.
  3. Associate it with the instance.
  4. Run describe-addresses again and confirm that the instance ID appears.

After verifying the result, disassociate the address and release it so that it does not remain allocated unnecessarily.

Challenge

Write a Bash script that accepts one EC2 instance ID through the INSTANCE_ID variable and performs these tasks in the us-east-1 Region:

  • Allocate a new Elastic IP for a VPC.
  • Associate it with the instance.
  • Display the public IP and allocation ID after association.
  • Disassociate the address.
  • Release the address.

The script should save the allocation ID in a variable so that the same address is used for every later command.

Solution

REGION="us-east-1"
INSTANCE_ID="i-0123456789abcdef0"

ALLOCATION_ID=$(aws ec2 allocate-address \
    --domain vpc \
    --region "$REGION" \
    --query "AllocationId" \
    --output text)

aws ec2 associate-address \
    --instance-id "$INSTANCE_ID" \
    --allocation-id "$ALLOCATION_ID" \
    --region "$REGION"

aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].[PublicIp,AllocationId,InstanceId]" \
    --output table

ASSOCIATION_ID=$(aws ec2 describe-addresses \
    --allocation-ids "$ALLOCATION_ID" \
    --region "$REGION" \
    --query "Addresses[0].AssociationId" \
    --output text)

aws ec2 disassociate-address \
    --association-id "$ASSOCIATION_ID" \
    --region "$REGION"

aws ec2 release-address \
    --allocation-id "$ALLOCATION_ID" \
    --region "$REGION"

echo "Elastic IP workflow completed."

The script stores the allocation ID immediately after allocation, uses that ID to associate and inspect the address, retrieves the association ID for disassociation, and finally releases the same Elastic IP. Replace the example instance ID with a real instance that belongs to the selected Region.

Key Takeaways

  • Use allocate-address --domain vpc to create an Elastic IP allocation for a VPC.
  • Use describe-addresses to inspect the public IP, allocation ID, instance ID, and association ID.
  • Use associate-address to connect an Elastic IP to an EC2 instance.
  • Use disassociate-address to remove the connection while keeping the allocation.
  • Use release-address when the Elastic IP is no longer needed.

Once you are comfortable inspecting these resources, a natural next step is scheduled AWS CLI automation for recurring checks and cloud maintenance workflows.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Ad
Ad
Ad