Terraform: Configuring Private IP Addresses on aws instance

Created on 24 Jan 2015  ยท  6Comments  ยท  Source: hashicorp/terraform

Hi,

Suppose I configure aws_instance with count = 5,
how can I assign ip addresses on these instances ?

Most helpful comment

I copied this code directly into my code and it didn't work and I realize this issue is 2 years old.

For someone who got here from google searching the syntax that does work here is:

private_ip = "${lookup(var.data_node_ips,count.index)}"

All 6 comments

I have not personally done this, but I believe this is how something like that is meant to be done. You'll use a variable that has a map of values:

variable "ips" {
    default = {
        0 = "10.1.1.5"
        1 = "10.1.1.6"
        ...
    }
}

and then look them up via an interpolation function within your aws_instance provider:

resource "aws_instance" "example" {
  ...
  count = 5
  private_ip = "lookup(ips,count.index)"
  ...
}

The syntax above might not be exactly right, but it's close.

@knuckolls: Thanks a lot! It works !!!

For the record, the correct syntax is like this:

variable "ips" {
    default = {
        "0" = "10.1.1.5"
        "1" = "10.1.1.6"
        ...
    }
}

And

resource "aws_instance" "example" {
  ...
  count = 5
  private_ip = "${lookup(ips,count.index)}"
  ...
}

Should I close this ? I think this should go to the docs though..

Glad you got this figured out!

I think this should go to the docs though..

Agreed. The pieces are mentioned under Interpolation Syntax and called out when describing count meta-parameter but it seems like the pattern is common enough to call out specifically somewhere.

I copied this code directly into my code and it didn't work and I realize this issue is 2 years old.

For someone who got here from google searching the syntax that does work here is:

private_ip = "${lookup(var.data_node_ips,count.index)}"

Just wanted comment for those who run across this currently what works now (terraform 0.12.x) is:

lookup(var.ips,count.index)

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rjinski picture rjinski  ยท  3Comments

zeninfinity picture zeninfinity  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

rnowosielski picture rnowosielski  ยท  3Comments

thebenwaters picture thebenwaters  ยท  3Comments