Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Terraform ip range

I’m in the process of trying to create a list of ip’s using a combination of a for loop and the range function. I’m trying to get the loop to iterate through the range of numbers and appending that number as the last digits in an ip address.

locals {
  windows_ip_list = [for i in range(var.Number) : format("%s%02d", "10.16.0.1", i)]
}

Giving the variable var.Number a value of 5 creates a 5 element tuple but the following error is probvided.

│    9:   private_ip = local.windows_ip_list  #var.win_ip[count.index]
│     ├────────────────
│     │ local.windows_ip_list is tuple with 5 elements
│ 
│ Inappropriate value for attribute "private_ip": string required.
resource "aws_instance" "Windows" {
  ami = "ami-02c1f4de3809f0050"
  instance_type   = "t2.large"
  #subnet_id       = aws_subnet.Engineering[count.index].id 
  subnet_id = aws_subnet.windows.id 
  security_groups = [aws_security_group.Engineering.id]
  key_name = aws_key_pair.ENG-DEV.id
  count    = var.Number
  private_ip = local.windows_ip_list  #var.win_ip[count.index]
  associate_public_ip_address = false

Any help creating the desired list would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

You should reference the index of the private ip in the windows_ip_list tuple with local.windows_ip_list[count.index] instead of local.windows_ip_listas the private_ip attribute require a string value not a list

to have a clear view try to echo the values that local.windows_ip_list contains by :

terraform console

then type local.windows_ip_list and press enter

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading