“From Cost Drain to Cost Savings: Automate Your AWS Elastic IP Cleanup”

Managing AWS Elastic IP Costs: A Simple Shell Script Solution

The Hidden Cost Problem

Amazon Web Services (AWS) charges for Elastic IP addresses (EIPs) that are allocated but not attached to running instances. At approximately $3.65 per month per unattached EIP, these seemingly small charges can accumulate into significant costs, especially in organizations with multiple AWS accounts and regions.

Many AWS users unknowingly leave Elastic IPs unattached after:

  • Terminating EC2 instances without releasing the associated EIPs
  • Experimental deployments that were never cleaned up
  • Infrastructure changes where EIPs were reassigned but old ones left behind
  • Development and testing environments that were abandoned

Why This Matters

Financial Impact: A single forgotten EIP costs $43.80 annually. In large organizations, dozens of unattached EIPs can result in hundreds or thousands of dollars in unnecessary charges.

Resource Management: Unattached EIPs also consume your AWS Elastic IP quota (5 per region by default), potentially blocking legitimate deployments.

Compliance: Many organizations require regular cost optimization audits, making EIP management a compliance necessity.

The Solution: A Simple Detection Script

Our shell script provides a straightforward solution for identifying unattached Elastic IPs across AWS regions. Here’s what makes it effective:

Key Features

Smart Detection: The script properly identifies truly unattached EIPs by checking multiple AWS attachment fields:

  • AssociationId – EC2 instance attachments
  • NetworkInterfaceId – NAT Gateway and Load Balancer attachments
  • InstanceId – Direct instance associations

This prevents false positives that could lead to accidentally identifying EIPs attached to critical infrastructure like NAT Gateways.

Regional Flexibility: Supports checking specific regions or using environment defaults, making it suitable for multi-region deployments.

Cost Calculation: Provides immediate financial impact assessment, showing exactly how much money is being wasted monthly.

Usage Examples

# Check default region
./find_eips.sh

# Check specific region
./find_eips.sh us-west-2

# Use environment variable
export AWS_DEFAULT_REGION=eu-central-1
./find_eips.sh

Best Practices for EIP Management

1. Regular Auditing

Run this script monthly as part of your AWS cost optimization routine. Consider integrating it into automated reporting systems.

2. Infrastructure as Code

When using tools like Terraform or CloudFormation, always include explicit EIP cleanup in your destroy/teardown procedures.

3. Tagging Strategy

Implement consistent tagging for EIPs to track their purpose and ownership, making cleanup decisions easier.

4. Automation Integration

Consider integrating this script into CI/CD pipelines or AWS Lambda functions for continuous monitoring.

Implementation in Organizations

Development Teams

  • Include EIP checks in sprint retrospectives
  • Add EIP cleanup to definition-of-done checklists
  • Train developers on proper EIP lifecycle management

Operations Teams

  • Incorporate into monthly cost reviews
  • Set up alerts for EIP count thresholds
  • Document EIP cleanup procedures

Management

  • Include EIP costs in departmental charge-backs
  • Set policies requiring EIP justification for long-term allocations
  • Regular reporting on cost optimization efforts

Technical Considerations

Prerequisites:

  • bc package for cost calculations
  • AWS CLI installed and configured with appropriate IAM permissions (ec2:DescribeAddresses)

Installing bc Package:

# Ubuntu/Debian
sudo apt-get install bc

# CentOS/RHEL/Amazon Linux
sudo yum install bc

Security: The script only reads EIP information – it cannot accidentally delete resources, making it safe for regular use.

Performance: Lightweight execution with minimal API calls, suitable for frequent automated runs.

The Complete Working shell script :

#!/bin/bash

# Simple script to find unattached Elastic IP addresses

# Set region - change this or pass as environment variable
REGION=${1:-${AWS_DEFAULT_REGION:-us-east-1}}

echo "Finding unattached Elastic IPs in region: $REGION"
echo

# Get unattached EIPs (no InstanceId, AssociationId, or NetworkInterfaceId)
aws ec2 describe-addresses \
    --region "$REGION" \
    --query 'Addresses[?!AssociationId && !NetworkInterfaceId && !InstanceId].[PublicIp,AllocationId]' \
    --output table

# Count them
COUNT=$(aws ec2 describe-addresses \
    --region "$REGION" \
    --query 'Addresses[?!AssociationId && !NetworkInterfaceId && !InstanceId]' \
    --output json | jq length)

echo
if [ "$COUNT" -gt 0 ]; then
    echo "Found $COUNT unattached Elastic IP(s)"
    echo "These are costing ~\$$(echo "$COUNT * 3.65" | bc) per month"
    echo
    echo "To delete an EIP: aws ec2 release-address --allocation-id <allocation-id>"
else
    echo "No unattached Elastic IPs found!"
fi

Cost Optimization Impact

Organizations typically see:

  • Immediate savings: 10-30% reduction in EIP-related costs
  • Ongoing benefits: Monthly cost avoidance through regular monitoring
  • Resource availability: Freed EIP quota for legitimate use cases

Conclusion

This simple shell script represents a powerful example of how basic automation can drive significant cost savings in cloud environments. By implementing regular EIP auditing, organizations can eliminate waste, improve resource management, and maintain better control over their AWS spending.

The script’s simplicity is its strength – it requires minimal maintenance, has no complex dependencies, and provides immediate actionable insights. In the world of cloud cost management, sometimes the most effective solutions are the simplest ones.

Start using this script today, and turn those hidden EIP costs into visible savings.

Leave a Comment

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

Stay up to date with our blogs.

Subscribe to receive email notifications for new blog posts.