Using a Bash Script to Associate an Elastic IP Address to an EC2 instance

In previous articles, I explained how to create an AMI from an EC2 instance and then launch the instance with scripts. Next, you probably want to assign an Elastic IP (IPv4 address) to the instance. In this post, you will learn how to allocate, release, associate, and disassociate an Elastic IP with the AWS CLI and bash scripts.

An Elastic IP address in EC2 is a static IPv4 address that can be reassigned to different AWS resources for flexible IP management. Before you can assign an Elastic IP address, you have to allocate it in the AWS Console or with the AWS CLI. In the AWS Console, you can find the Elastic IP addresses on the EC2 tab under Networking and Security.

Please note that since February 2024 Amazon charges $0.005 per hour for in-use public IPv4 address usage. Public IPv4 addresses that are not in use have always incurred a cost, and the rate is $0.005 as well.

In this guide, I assume that have you installed the AWS CLI.

Allocate an Elastic IP address

The command below allocates a public Elastic IP address and displays it (–output text).

aws ec2 allocate-address --query 'PublicIp' --output text

And this command will list all your Elastic IPs:

aws ec2 describe-addresses --query 'Addresses[*].[AllocationId,PublicIp]' --output text

Release an Elastic IP address

Releasing an Elastic IP is a bit more complicated because we need an allocation ID, a unique identifier to an Elastic IP (EIP) address when allocated, used to manage and reference the EIP within your AWS environment.

The following script lets you pass the IP you wish to release.

#!/bin/bash

# Check if an IP address is passed as an argument

if [ $# -eq 0 ]; then

echo "Usage: $0 YOUR_PUBLIC_IP"

exit 1

fi

# Assign the first argument to YOUR_PUBLIC_IP

YOUR_PUBLIC_IP=$1

# Get the Allocation ID for the given public IP address

allocation_id=$(aws ec2 describe-addresses --query "Addresses[?PublicIp=='$YOUR_PUBLIC_IP'].AllocationId" --output text)

# Check if the allocation ID was found

if [ -n "$allocation_id" ]; then

# Release the EIP using the allocation ID

aws ec2 release-address --allocation-id $allocation_id

echo "Released EIP: $YOUR_PUBLIC_IP"

else

echo "Allocation ID not found for IP: $YOUR_PUBLIC_IP"

fi

Associate and disassociate an Elastic IP address

Associate an Elastic IP address

Once you have allocated an Elastic IP, you can use the bash script below to assign the IP (in AWS lingo, associate the IP) to an EC2 instance.

#!/bin/bash

# Check for the correct number of arguments

if [ $# -ne 2 ]; then

echo "Usage: $0 INSTANCE_NAME EIP_ADDRESS"

exit 1

fi

# Assign the arguments to variables

INSTANCE_NAME=$1

EIP_ADDRESS=$2

# Retrieve the instance ID based on the name

INSTANCE_ID=$(aws ec2 describe-instances

--query "Reservations[*].Instances[*].{ID:InstanceId}"

--filters "Name=tag:Name,Values=$INSTANCE_NAME"

--output text)

# Check if the instance ID was found

if [ -z "$INSTANCE_ID" ]; then

echo "No instance found with the name: $INSTANCE_NAME"

exit 1

fi

echo "Instance ID: $INSTANCE_ID"

# Retrieve the allocation ID of the Elastic IP

EIP_ALLOC_ID=$(aws ec2 describe-addresses

--query "Addresses[?PublicIp=='$EIP_ADDRESS'].AllocationId"

--output text)

# Check if the allocation ID was found

if [ -z "$EIP_ALLOC_ID" ]; then

echo "No Elastic IP found with the address: $EIP_ADDRESS"

exit 1

fi

echo "Elastic IP Allocation ID: $EIP_ALLOC_ID"

# Associate the Elastic IP with the instance

ASSOCIATION_ID=$(aws ec2 associate-address

--instance-id $INSTANCE_ID

--allocation-id $EIP_ALLOC_ID

--query 'AssociationId'

--output text)

# Check if the EIP was successfully associated

if [ -z "$ASSOCIATION_ID" ]; then

echo "Failed to associate Elastic IP with the instance."

exit 1

fi

echo "Elastic IP associated with the instance successfully. Association ID: $ASSOCIATION_ID"

Disassociate an Elastic IP address

The following script removes the IP address from the EC2 instance. Since an Elastic IP can only be assigned to one instance, there is no need to pass the instance name.

#!/bin/bash

if [ $# -eq 0 ]; then

echo "Usage: $0 EIP_ADDRESS"

exit 1

fi

EIP_ADDRESS=$1

ASSOCIATION_ID=$(aws ec2 describe-addresses

--query "Addresses[?PublicIp=='$EIP_ADDRESS'].AssociationId"

--output text)

if [ -z "$ASSOCIATION_ID" ]; then

echo "No association found for IP: $EIP_ADDRESS"

exit 1

fi

aws ec2 disassociate-address --association-id $ASSOCIATION_ID

if [ $? -eq 0 ]; then

echo "EIP disassociated successfully."

else

echo "Failed to disassociate EIP."

fi

Please note that the IP remains allocated even after disassociating it from the instance. This means you will continue to incur charges for the idle IP until it is released. You can use the provided script above to release the IP address if you no longer need it.

Associate and disassociate an Elastic IP address

Subscribe to 4sysops newsletter!

If you are frustrated due to the need of paying for actively used public IPv4 addresses, you might find interest in one of my subsequent articles wherein I elucidate how you can allocate an IPv6 address to an EC2 instance.


Posted

in

, , ,

by

Tags: