A
Arun's Blog
← Back to all posts

Elastic IP Removal: A Guide to Releasing Unused AWS Elastic IP Addresses

CLIQuickBytesCost Optimization
TL;DR

Use this bash script to automatically find and release unassociated Elastic IP addresses across all AWS regions. Unattached EIPs cost money, so regular cleanup directly impacts your AWS bill.

Introduction

In the dynamic world of cloud computing, efficient resource management is essential for businesses to optimize their infrastructure and control costs. Amazon Web Services (AWS) offers Elastic IP addresses, a valuable tool for providing static, publicly accessible IPs to your instances. However, it's not uncommon for organizations to accumulate unused Elastic IP addresses over time, leading to unnecessary expenses and resource inefficiencies. In this blog post, we explore the significant benefits of releasing those unused Elastic IP addresses in AWS. By adopting this practice, you can enhance cost optimization, streamline resource allocation, contribute to environmental sustainability, maintain accurate inventories, and adhere to best practices in AWS infrastructure management. Let's delve into the advantages and discover how releasing unused Elastic IP addresses can positively impact your AWS environment.

Welcome to this step-by-step guide on 'Elastic IP Address Spring Cleaning'. This post will delve into the significance of removing unused Elastic IP Addresses.

Warning

Once an Elastic IP is released, you cannot get the same IP address back. If any external systems or DNS records reference this IP, they will need to be updated. Document your EIPs before running cleanup scripts.

Use This

  • AWS CloudShell
  • Bash Script

Do This

  1. Open CloudShell
  2. Create bash file: touch removeEIP.sh
  3. Create contents of bash file: vi removeEIP.sh
  4. Type in code below
  5. Save the file
  6. Change the file to an executable file: chmod +x removeEIP.sh
  7. Run the script: ./removeEIP.sh
Note

AWS charges approximately $0.005 per hour for each unattached Elastic IP address. This adds up to roughly $3.60 per month per unused EIP - a small amount that can become significant at scale.

Write This

#!/bin/bash

# Fetch all AWS regions
for region in $(aws ec2 describe-regions --output text --query 'Regions[].RegionName')
do
  echo "Checking region $region"

  # Fetch all Elastic IPs not allocated to a network interface
  for allocation in $(aws ec2 describe-addresses --region "$region" --query "Addresses[?AssociationId==null].AllocationId" --output text)
  do
    echo "Releasing unattached Elastic IP with allocation id $allocation in region $region"
    aws ec2 release-address --region "$region" --allocation-id "$allocation"
  done

  # Add other services and their corresponding commands here
  # ...

done
Pro Tip

Before releasing EIPs, export a list of all your Elastic IPs with their allocation IDs and associated resources using: aws ec2 describe-addresses --output json > eip-backup.json. This creates a record you can reference if needed.

What The Heck Does This Code Do

  1. Gets a list of all AWS regions
  2. For each of the region found, find all Elastic IP Addresses
  3. Find any Elastic IP Addresses that are not associated to anything
  4. Relase the unused Elastic IP Addresses

Troubleshooting

Common Issues and Solutions

AuthFailure Error

  • Problem: Cannot release Elastic IP due to authentication failure
  • Solution: Ensure your IAM user/role has ec2:ReleaseAddress permission. Check if the EIP was allocated in a different account or if there are SCPs blocking the action.

EIP Associated with NAT Gateway

  • Problem: Script shows EIP as unattached but release fails
  • Solution: EIPs attached to NAT Gateways may show as unassociated in some queries. Check NAT Gateway associations before releasing.

DisassociateAddress Required

  • Problem: EIP cannot be released because it's still associated
  • Solution: The script filters for unassociated EIPs. If you need to release an associated EIP, first disassociate it using aws ec2 disassociate-address.

EIP Limit Reached After Release

  • Problem: Cannot allocate new EIP after releasing old ones
  • Solution: AWS has regional EIP limits (default is 5). Request a limit increase through Service Quotas if needed.

Released EIP Still Appearing in Billing

  • Problem: Charges continue after releasing EIP
  • Solution: Billing is updated at the end of the billing cycle. Check Cost Explorer to confirm the EIP was released and charges stopped from that point forward.

Till Next Time

Efficient resource management is a key factor in maintaining a well-optimized and cost-effective AWS environment. Releasing unused Elastic IP addresses in AWS presents a range of benefits that go beyond cost savings. By reclaiming these idle resources, organizations can maximize cost optimization, free up valuable IP addresses for other users, support environmental sustainability by reducing resource consumption, maintain accurate inventories, and adhere to best practices in AWS infrastructure management.

Regularly reviewing and releasing unused Elastic IP addresses should be an integral part of your resource management strategy. By doing so, you not only save costs but also ensure efficient utilization of AWS resources. Take the initiative to examine your infrastructure, identify any unused Elastic IP addresses, and release them accordingly. By embracing this practice, you can unlock the full potential of your AWS environment, maintain control over your expenses, and contribute to a more sustainable and streamlined infrastructure.

In the dynamic and often complex world of cloud computing, it's the small things that can make a big difference. And remember, consistent cloud cleanliness is next to digital godliness. So, keep your digital broom at the ready, sweep away unnecessary objects, and keep your cloud environment running at its peak potential. Until next time, happy 'Spring Cleaning'!