Terraform: Como variar sub-redes VPC

Terraform: Como variar sub-redes VPC

Quero provisionar 2 instâncias de controlador de domínio em duas sub-redes. VPC, sub-redes e outras seções de rede já foram criadas.

principal.tf

  resource "aws_instance" "PerformanceDC01" {
  count         = var.instance_count
  ami           = var.aws_ami
  ebs_optimized = true
  instance_type = var.aws_instance_type
  subnet_id     = var.pvtsub_a
  key_name      = var.aws_key_name
  vpc_security_group_ids = [
    var.base_sg,
    var.perfdc_sg
  ]
  root_block_device {
    volume_type = "gp2"
    volume_size = "80"
    encrypted   = true
    kms_key_id  = "10c07c9d-ede7-43d5-b633-75a2482848aa"
  }
  tags = {
    Name = "PerformanceDC0-${count.index + 1}"
  }
} 

variáveis.tf

variable "aws_region" {}
variable "aws_profile" {}
variable "instance_count" {}
variable "aws_vpc" {}
variable "pvtsub_a" {}
variable "pvtsub_b" {}
variable "pvtsub_c" {}
variable "pubsub_a" {}
variable "pubsub_b" {}
variable "pubsub_c" {}
variable "aws_ami" {}
variable "aws_instance_type" {}
variable "aws_key_name" {
  description = "Key Name"
  default     = "Performance_B_KP"
}
variable "base_sg" {}
variable "perfdc_sg" {}

desempenho.tfvars

.
.
instance_count    = "2"
.
.

Pergunta: Como faço para variarsubnet_idcomoDC01é provisionado empvtsub_aeDC02é provisionado empvtsub_b?

Responder1

Use for_eachem vez de countassim:

resource "aws_instance" "PerformanceDC" {
  for_each         = var.instances
  ami              = each.value.ami
  subnet_id        = each.value.subnet_id
  ...
  tags = {
    Name = "PerformanceDC0-${each.key + 1}"
  }
}

tfvars:

instances = [
  {
    ami       = "brad",
    subnet_id = "sg1",
  },
  {
    ami       = "pitt",
    subnet_id = "sg2",
  } 
]

Isso permite migrar uma das instâncias para uma nova AMI.

informação relacionada