Identifying Unutilized AWS Elastic IPs to Optimize AWS Costs Using PowerShell

Ritik GuptaRitik Gupta
5 min read

When managing AWS resources, cost optimization is critical, especially when considering the hidden costs associated with resources that are allocated but not actively used. One such resource is Elastic IP (EIP) addresses, which can often be forgotten and left unattached, leading to unnecessary costs. In this blog, we will explore Elastic IPs, why they become orphaned, and how to identify and remove these unused EIPs to save costs in your AWS organization.

What is an Elastic IP in AWS?

An Elastic IP (EIP) is a static, public IPv4 address designed for dynamic cloud computing. It allows you to maintain a persistent address across EC2 instances even when they are stopped and started. AWS allows each account to have a limited number of Elastic IPs, and when not attached to a running instance, AWS charges for each unattached Elastic IP.

Key points about Elastic IPs:

  • Elastic IPs are free when associated with a running EC2 instance.

  • AWS charges for unused Elastic IPs that are not attached to any instance.

  • A single Elastic IP address can be re-mapped between cases.

Why Do Elastic IPs Become Orphaned?

Orphaned Elastic IPs occur when they are not attached to any running instance or network interface. This can happen for several reasons:

  1. Instances terminated: The EC2 instance that was using the EIP might have been terminated, but the EIP was not released.

  2. Mismanagement of resources: In complex architectures, especially in large-scale environments, EIPs may get allocated and forgotten over time.

  3. Testing or staging environments: EIPs might have been allocated for temporary testing or staging environments and left unattached afterward.

Regardless of the cause, orphaned Elastic IPs can accumulate, leading to unnecessary expenses.

Why is It Important to Clean Up Unutilized Elastic IPs?

AWS charges a small fee for every unattached Elastic IP. While the cost of a single unattached EIP may seem trivial, when scaled across multiple regions and accounts within an organization, these costs can quickly add up. Regularly auditing and cleaning up unused Elastic IPs can save your organization a significant amount of money in the long term.

How to Find Orphaned Elastic IPs

Identifying and removing orphaned Elastic IPs in AWS can be done using several methods, including the AWS Management Console, AWS CLI, and automated scripts. Here, we'll explore how to find these resources using both the AWS CLI and a PowerShell script to make the process more efficient.

Method 1: Using the AWS Management Console

  1. Go to the EC2 Dashboard in the AWS Management Console.

  2. On the left-hand navigation pane, click Elastic IPs under the “Network & Security” section.

  3. Check the Instance ID column to see if the Elastic IPs are associated with any running instances.

  4. For any EIPs that are not attached to an instance, evaluate whether they are still needed and release them if not.

Method 2: Using AWS CLI

The AWS CLI offers a faster way to identify unused Elastic IPs. You can run the following command to list all Elastic IPs and filter out those that are unattached:

aws ec2 describe-addresses --query 'Addresses[?AssociationId==null].[PublicIp,AllocationId]' --output table

This command lists all Elastic IP addresses that are not associated with any instance or network interface. It shows both the Public IP and Allocation ID, which are necessary to release the unused Elastic IPs.

To release an orphaned Elastic IP using its Allocation ID, run:

aws ec2 release-address --allocation-id <allocation-id>

Make sure you release only the IPs that are not needed, as this operation cannot be undone.

Method 3: Automating the Process with PowerShell

For organizations that need to audit Elastic IPs across multiple accounts or regions, automating the identification process can be a time-saver. Below is an example PowerShell script that finds unattached Elastic IPs and outputs them in a structured format.

$Regions = @(
    "ap-southeast-2",
    "eu-central-1",
    "eu-west-1",
    "us-east-1",
    "us-east-2",
    "us-west-1",
    "us-west-2"
)

# Loop through each region
foreach ($region in $Regions) {
    Write-Output "Processing region: $region"
    # Set the region for the AWS commands
    Set-DefaultAWSRegion -Region "$Region"

    # Fetch all Elastic IPs in the current region
    $Parameters = @{
        ProfileName = $Context.ProfileName
        Region      = $region
    }
    $elasticIPs = Get-EC2Address @Parameters

    # Loop through each Elastic IP
    foreach ($ip in $elasticIPs) {
        # Check if the Elastic IP is not associated with any instance, network interface, or other resources
        if (-not $ip.AssociationId) {
            # If not associated, consider it unattached and add to the list
            Write-Output "Orphaned Elastic IP in region $region: $($eip.PublicIp) - Allocation ID: $($eip.AllocationId)"      
        }
    }
}

This script will loop through all available regions and check for orphaned Elastic IPs. You can extend the script to automatically release unused EIPs or log them for manual review.

Best Practices for Managing Elastic IPs

To avoid orphaned Elastic IPs in the future, consider the following best practices:

  • Automated cleanup: Implement automation that periodically checks for and releases unused Elastic IPs.

  • Tagging: Apply clear tags to your resources, including Elastic IPs, to track their purpose and ownership.

  • Resource lifecycle management: When an EC2 instance is terminated, ensure that any associated Elastic IPs are also released unless explicitly required.

  • Use Elastic Load Balancers (ELBs): If your workload can tolerate dynamic IP changes, consider using Elastic Load Balancers instead of manually managing Elastic IPs.

Conclusion

By regularly auditing your AWS environment for orphaned Elastic IPs, you can prevent unnecessary costs and optimize your cloud spending. Using the AWS CLI, PowerShell scripts, and best practices for resource management can help ensure that Elastic IPs are used efficiently and released when no longer needed.

Stay tuned for the next blog in our AWS orphaned resources series, where we’ll explore another commonly overlooked resource that could be costing your organization!

Thanks for reading! I hope you understood these concepts and learned something.
If you have any queries, feel free to reach out to me on LinkedIn.

10
Subscribe to my newsletter

Read articles from Ritik Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritik Gupta
Ritik Gupta