skip to Main Content

This seems like an obvious question but I’ve had no luck finding an answer.
Using Terraform (v1.3.5) and given a private AWS subnet (defined by aws_subnet), how can I get the next available IP address within the subnet?

I’m sure I’m just looking at the problem wrong. I would expect a resource-type that would take a subnet-id and return an IP address, but can’t find one that matches. I’ve also looked for tutorials on the question. Many tutorials talk about assigning an EIP, but that is for public subnets. I need to allocate several instances within a private subnet and need to allocate addresses for each.

My next question, of course, will be: how to request IPs across a set of subnets with an even allocation across the subnets. But that may need another posting.

Edit #1:
In response to the questions from @Marcin and @MattBlaha. I need the IP address to satisfy the requirement for an ENI which is needed to create the instance (see docs here). I’ve copied the sample code from the docs. Note that a "private_ips" address is required to create the ENI. And the ENI is needed to create the instance:

resource "aws_subnet" "my_subnet" {
  vpc_id            = aws_vpc.my_vpc.id
  cidr_block        = "172.16.10.0/24"
  availability_zone = "us-west-2a"

  tags = {
    Name = "tf-example"
  }
}

resource "aws_network_interface" "foo" {
  subnet_id   = aws_subnet.my_subnet.id
  private_ips = ["172.16.10.100"]

  tags = {
    Name = "primary_network_interface"
  }
}

resource "aws_instance" "foo" {
  ami           = "ami-005e54dee72cc1d00" # us-west-2
  instance_type = "t2.micro"

  network_interface {
    network_interface_id = aws_network_interface.foo.id
    device_index         = 0
  }
}

Is there some way to create an ENI with out specifying the IP?

2

Answers


  1. Chosen as BEST ANSWER

    Many thanks to @Manchin for his clarification.

    I now believe that I was looking at the problem wrong and I also believe the documentation is less than clear on this matter. The docs provide an example where a dedicated ENI resource is created using a static IP address (example shown above) which is used to create the instance. However, a dedicated ENI resource block is NOT actually necessary. The attribute list for the aws_instance resource allows for a direct subnet assignment. I think this would be a primary use-case and should have an example in the documentation, but regardless I missed it.

    Instead one can specify the subnet directly within the aws_instance resource block. This relieves the coder from having to assign an IP address directly and allows one to be selected from the available subnet pool. I'm sure an ENI is created somewhere, but I am no longer need to be involved in it.

    Here is sample code the resolve the original query:

    resource "aws_subnet" "my_subnet" {
      vpc_id            = aws_vpc.my_vpc.id
      cidr_block        = "172.16.10.0/24"
      availability_zone = "us-west-2a"
    }
    
    resource "aws_instance" "foo" {
      ami           = "ami-005e54dee72cc1d00" # us-west-2
      instance_type = "t2.micro"
      subnet_id   = aws_subnet.my_subnet.id
      <snip>
    }
    

  2. There is no such functionality in TF nor even AWS API. You have to implement your own custom solution to get "next free IP address" from a given subnet. This will be changeling, as you can have a pre-existing resources in the subnet, so you have to get their IP addresses first, before you can find which one is "next" or "free".

    The custom solution can be implemented as an external data source in a programing language of your choice, e.g. bash script, python.

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