skip to Main Content

I am fairly new to AWS CLI. I am trying to find out the limit(quota) of elastic IP addresses for a region(e.g. us-east-1). From that limit, how many have been used and how many are remaining for my terraform script to use. I need to use this to conditionalise the further flow. Need help. Right now I have tried to use the below but it gives me wrong answer.

Available_Elastic_IPs=$(AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN aws ec2 describe-addresses --query "Addresses[?NetworkInterfaceId == null ].PublicIp" --region "$REGION" --output "text" | wc -w)

This tells me where the network interface ID are null. Even if the count is 0, I can still go ahead and create EC2 instances with elastic IPs. So I think the query needs slight adjustment.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Lorenzo's answer, I was able to get through my problem. I combined all this and ran on Jenkins shell script and it worked.

    Here is my EIP_Get_Quota.tf file

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }
    
    provider "aws" {
      region =  var.region
      access_key = var.aws_access_key_id
      secret_key = var.aws_secret_access_key 
    
    }
    
    data "aws_servicequotas_service_quota" "ip_addresses" {
      service_code = "ec2"
      quota_name = "EC2-VPC Elastic IPs"
    }
    
    output "vpc_quota" {
      value = data.aws_servicequotas_service_quota.ip_addresses
    }
    
    
    
    
    variable "aws_access_key_id" {
    
    }
    variable "aws_secret_access_key" {
    
    }
    
    variable "region"{
      default="us-east-1"
    }
    

    extracted the output with the help of below code in shell script

    terraform init
    cmd1="terraform apply --auto-approve -var     aws_access_key_id="$AWS_ACCESS_KEY_ID" -var region="$REGION" -var     aws_secret_access_key="$AWS_SECRET_ACCESS_KEY""
    eval "$cmd1"
    terraform output -json > $outputFileName
    total_quota=$(cat "$outputFileName" | jq -r '.vpc_quota.value.value')
    

    Then I ran the below in AWS command line (CLI)

    Created_Elastic_IPs_Temp=$(AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY aws ec2 describe-addresses --query "Addresses[*].PublicIp" --region "$REGION" --output "text" )
    
    Created_Elastic_IPs=$(echo $Created_Elastic_IPs_Temp | wc -w) #Separated for error checking
    
    #This is where you get the remaining number of EIPs. 
    Total_Available_EIPs="$((total_quota - Created_Elastic_IPs))"
    

  2. This is a minimal working example that I used to achieve what you’re trying to :

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 5.0"
        }
      }
    }
    
    provider "aws" {}
    
    data "aws_servicequotas_service_quota" "ip_addresses" {
      service_code = "ec2"
      quota_name = "EC2-VPC Elastic IPs"
    }
    
    output "vpc_quota" {
      value = data.aws_servicequotas_service_quota.ip_addresses
    }
    

    The output I added will look something like this

    vpc_quota = {
      "adjustable" = true/false
      "arn" = "arn:aws:servicequotas:<REGION>:<ACCOUNT>:ec2/L-0263D0A3"
      "default_value" = 5
      "global_quota" = true/false
      "id" = "arn:aws:servicequotas:<REGION>::ec2/L-0263D0A3"
      "quota_code" = "L-0263D0A3"
      "quota_name" = "EC2-VPC Elastic IPs"
      "service_code" = "ec2"
      "service_name" = "Amazon Elastic Compute Cloud (Amazon EC2)"
      "usage_metric" = tolist([<...>])
      "value" = <THE VALUE YOU'RE LOOKING FOR>
    }
    

    Some docs that I used that may be helpful to you:

    To get the quota name I followed the AWS docs instructions on the console and copy/pasted the quota_name that was shown there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search