Home > Blockchain >  terrafor from - create 3 server over 2 subnets using count
terrafor from - create 3 server over 2 subnets using count

Time:04-18

In terraform, I wish to create 3 servers while I'm having 2 subnets. Creating 2 servers according to the below code will route both server and subnet ID according to the count - But what if I want 3 servers? I don't mind on which of the subnet the third server will be located.

resource "aws_instance" "consul_server" {
  count = 2
  ami           = "ami-00ddb0e5626798373"
  instance_type = t2.micro
  subnet_id = var.private_subnet_id[count.index]
  vpc_security_group_ids = [aws_security_group.consul_server.id]
  tags = {
    Name = "consul-server-${count.index   1}-${var.project_name}"
    tag_enviroment= var.tag_enviroment
    project_name = var.project_name
    consul_server = "true"
    role = "consul-server"
  }
}

CodePudding user response:

Normally you would use element to wrap-around indexing:

subnet_id = element(var.private_subnet_id, count.index)
  • Related