Search code examples
amazon-web-servicesterraformelastic-ip

Retrieve a list of non associated (free) Elastic IPs that have already been allocated through terraform


I want to use the aws_eip terraform data source to retrieve a list of the Elastic IPs already allocated in my account; I want those that are free, i.e. they have not been associated with any resource.

Here is what I did:

data "aws_eip" "free_elastic_ips" {
  filter {
    name   = "association-id"
    values = ["null"]
  }
}

The problem is that this errors out:

│ Error: no matching EC2 EIP found
│
│   with data.aws_eip.free_elastic_ips,
│   on main.tf line 38, in data "aws_eip" "free_elastic_ips":
│   38: data "aws_eip" "free_elastic_ips" {
│

despite the fact that I have free Elastic IPs in my org.

Here is the output of aws ec2 describe-addresses for one such IP (values scrambled intentionally)

        {
            "PublicIp": "13.24.111.20",
            "AllocationId": "eipalloc-0d7ffe3iktk47aafkc",
            "Domain": "vpc",
            "PublicIpv4Pool": "amazon",
            "NetworkBorderGroup": "us-east-1"
        },

e.g.

enter image description here

(and this is just one of them)


Solution

  • Wrapping null in double quotes means you are asking it to search for Elastic IPs where the actual association ID is the literal string "null" as in lower case n u l l . Try removing the quotes values = [null] or perhaps try an empty values list values = [].