Home > Software design >  Terraform ip range
Terraform ip range

Time:03-02

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("%sd", "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.

Thanks in advance.

CodePudding user response:

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

CodePudding user response:

Calling the variable and using the count.index was how it was fixed.

private_ip = local.windows_ip_list[count.index]
  • Related