AWS SDK for Go V2 - Get Elastic IP consumed quota
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
And the current number of used Elastic IPs using the
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
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:
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.
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.
Thank you for this great answer.
I have two follow-up questions:
- 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
- What is the maximal number of results for this operation?
AppStream 2.0 Assign Elastic IPAccepted Answerasked 3 months ago
Can't migrate Classic Elastic IP with Reverse DNS to VPC Elastic IPasked 7 months ago
Do Elastic Beanstalk Web Server Environment Need A Public Elastic IPasked 15 days ago
AWS SDK for Go V2 - Get Elastic IP consumed quotaasked 3 months ago
Elastic IP for NAT GWasked 2 months ago
EC2 instance doesn't get a new public IP after Elastic IP is disassociatedasked 17 days ago
Verify foreign Elastic IPAccepted Answerasked 2 months ago
EC2 Instance IP keeps changing despite elastic IPasked 4 months ago
Elastic Graphics Quota exceededasked 2 months ago
Single elastic IP that can access multiple unique EC2 instancesasked 2 months ago