I want to provision 2 domain controller instances in two subnets. VPC, subnets and other networking sections are already created.
main.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}"
}
}
variables.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" {}
performance.tfvars
.
.
instance_count = "2"
.
.
Question: How do I variablize subnet_id such as DC01 is provisioned on pvtsub_a and DC02 is provisioned on pvtsub_b ?
Use
for_each
instead ofcount
like this:tfvars:
This allows you to migrate one of instances to a new AMI.