Chatwithcloud logo

ChatWithCloud

AWS GenAI Tools

JS Code to Find and Deprovision Unallocated Elastic IP Addresses

This article explains how to use JavaScript (JS) code to efficiently find and deprovision AWS Elastic IPs that are not associated with any running instances. The aim of this script is to minimize costs and effectively manage resources.

Code

const AWS = require('aws-sdk'); const EC2 = new AWS.EC2(); (async function deprovisionUnassociatedEIPs() { const allEIPs = await EC2.describeAddresses({}).promise(); const unassociatedEIPs = allEIPs.Addresses.filter(eip => !eip.InstanceId); for (let eip of unassociatedEIPs) { await EC2.releaseAddress({ AllocationId: eip.AllocationId }).promise(); console.log(`Released Elastic IP: ${eip.PublicIp}`); } })();

Detailed Code Explanation

We begin by importing the aws-sdk module and initializing an EC2 object.

The deprovisionUnassociatedEIPs function describes all Elastic IP addresses available in your AWS environment using EC2.describeAddresses({}).promise(). This returns a promise to an object containing a list of addresses.

We then filter for all IP addresses that are not associated with any instance using the filter method, returning a new array unassociatedEIPs.

For each unassociated Elastic IP in this array, we release it using EC2.releaseAddress with the AllocationId of the unassociated EIP.

Each release triggers a console message logging the released Elastic IP.

Expected JSON output

The output given out after the execution of the JS code will be in the form of console log statements. As such, they won't strictly be in the JSON format. However, if executed successfully, you can expect outputs similar to this:

Released Elastic IP: 203.0.113.0
Released Elastic IP: 203.0.113.1
...

where 203.0.113.x represents the public IP address of the released Elastic IP.

Considerations & Caveats

  1. The code provided does not handle throttling or "Request Limit Exceeded" errors. You may want to consider adding error handling or exponential backoff in the case of large IP addresses pool.
  2. The code provided does not consider regional considerations. Elastic IPs are region-specific and this script only works within the region you've configured in your AWS SDK.
  3. Releasing an Elastic IP that a service is still trying to use could impact that service.

Required IAM Permissions and Example Policy

To run this script, the AWS credentials used need the ec2:DescribeAddresses and ec2:ReleaseAddress IAM permissions. Below is an example policy:

{ "Version": "2012-10-17", "Statement": [ { "Sid": "EIPPermissions", "Effect": "Allow", "Action": [ "ec2:DescribeAddresses", "ec2:ReleaseAddress" ], "Resource": "*" } ] }

FAQ

  1. What happens when I release an IP?

    • When you release an IP, it's disassociated from your account and returned to the IP pool for AWS to assign to other users.
  2. Will I be charged for Elastic IPs that are not associated with any instance?

    • Yes, AWS charges for any Elastic IP that is not associated with a running instance.
  3. Can I specify a region for the IP addresses to be released?

    • The function, as provided, will only operate in the region currently configured in your AWS SDK.
  4. What if I accidentally release an Elastic IP that is in use?

    • Releasing an Elastic IP that a service is still trying to use could impact that service. Therefore, ensure the IP is not associated with an instance before you release it.




Related articles
Monitor and Notify When Approaching Service LimitsGet Current IAM Identity TypeScriptCalculate the Size of Each S3 Bucket and Find the One with the Most Data