AWS SDK for Go V2 - Get Elastic IP consumed quota

0

Hello,

I'm trying to use the SDK to figure out what is the available quota for Elastic IP (excluding used Elastic IPs). I can get the Elastic IP limit using the github.com/aws/aws-sdk-go/service/servicequotas client, And the current number of used Elastic IPs using the github.com/aws/aws-sdk-go-v2/service/ec2 client. Then calculate the available quota using subtraction.

The challenge I'm facing is that in case the account contains a large number of IP addresses, fetching all of them can be time-consuming. While I'm interested only in the number of available Elastic IP addresses.

I was hoping there is another function or endpoint I can query to get the number of available Elastic IPs instead of going through this calculation.

Thank you, Ori Adler

2 Answers
0

Hi Ori,

I understand that you are looking for way to find number of available (unused) EIPs in your account for a given region. In general for Elastic IPs that are not used in a VPC do not have the following properties (fields) or the value of these fields is set to Null : "AssociationId" , "NetworkInterfaceId", "NetworkInterfaceOwnerId", "PrivateIpAddress"

You can run this API "describe-addresses" to get list of EIP in your account for the given region. example command to do so using AWS CLI is "aws ec2 describe-addresses --region us-west-2" Here are the AWS documents on topic : Describe address API : https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeAddresses.html AWS CLI for Describe API : https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-addresses.html

Using the above information you can run a CLI command to query AWS EC2 endpoint to just provide list of Elastic IPs that are not associated to any resources (which also mean not used). Save the output in a .txt file and run grep to count the unused EIPs for that region.

You will have to customize this using SDK but here is how I accomplish this using AWS CLI in my test environment:

  1. Run this command on CLI to get list of unused EIPs : "aws ec2 describe-addresses --region us-west-2 --query 'Addresses[?AssociationId==null]' >> test.txt". Instead of AssocationID you can other fields "NetworkInterfaceId", "NetworkInterfaceOwnerId", "PrivateIpAddress" as well. In this command I am saving my result in test.txt file.

  2. Now use grep to count number of time Allocation-ID is repeated in my output (as this field is present in output once for every IP). Here is the command : "grep -o -i AllocationId test.txt | wc -l" , "grep -i -c AllocationId test.txt" both the commands will give same output. If you also want list of Allocation-ID as well you can run this command : "grep -i AllocationId test.txt"

With the steps mentioned above you will get number of Unused EIPs in your account for that region.

If you are looking for a way to get number of EIPs that are not yet allocated to your account (or get the number of EIP you can allocated to your account before you hit the service quota limit) then as of now we do not have any direct API call which can perform this action. All API calls related to EIP are made to AWS EC2 endpoint and EC2 endpoint do not provide information on number of "utilized" or "not utilized" value for this service quota as of now. To get this information you can either write a script which will get both "service quota" and "current EIP count" do the calculation on your behalf and provide the information. Which will be similar approach as you are currently taking.

I hope this information helps.

AWS
SUPPORT ENGINEER
answered 2 years ago
0

Thank you for this great answer.

I have two follow-up questions:

  1. Because I'm interested only in the total number of addresses, is there a way to fetch the total number of addresses without their content? I'm just interested in the length of the array returned from this API call []types.addresses of describeAddressesOutput.
  2. What is the maximal number of results for this operation?
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions