Использование Terraform for_each
(доступное начиная с v0.12.x) может фактически выполнить это.
Это создает 3 экземпляра AWS:
variable "hosts" {
default = {
"one" = {
"name" = "one",
"machine" = "t2.micro",
"ami" = "ami-009d6802948d06e52",
"subnet" = "subnet-002df68a36948517d"
},
"two" = {
"name" = "two",
"machine" = "t3.micro",
"ami" = "ami-009d6802948d06e52",
"subnet" = "subnet-01c13b00a5531828e"
},
"three" = {
"name" = "three",
"machine" = "t2.nano",
"ami" = "ami-009d6802948d06e52",
"subnet" = "subnet-0166523e6bd98ebd8"
}
}
}
resource "aws_instance" "instances" {
for_each = var.hosts
ami = each.value.ami
instance_type = each.value.machine
subnet_id = each.value.subnet
tags = {
Name = each.value.name
}
}
Когда я закомментирую two
Я получаю план Terraform ниже
variable "hosts" {
default = {
"one" = {
"name" = "one",
"machine" = "t2.micro",
"ami" = "ami-009d6802948d06e52",
"subnet" = "subnet-002df68a36948517d"
},
# "two" = {
# "name" = "two",
# "machine" = "t3.micro",
# "ami" = "ami-009d6802948d06e52",
# "subnet" = "subnet-01c13b00a5531828e"
# },
"three" = {
"name" = "three",
"machine" = "t2.nano",
"ami" = "ami-009d6802948d06e52",
"subnet" = "subnet-0166523e6bd98ebd8"
}
}
}
Terraform will perform the following actions:
# aws_instance.instances["two"] will be destroyed
- resource "aws_instance" "instances" {
- ami = "ami-009d6802948d06e52" -> null
- arn = "arn:aws:ec2:us-east-1:XXXXXXXX:instance/i-03a8285fc49f48a69" -> null
- associate_public_ip_address = true -> null
- availability_zone = "us-east-1b" -> null
- cpu_core_count = 1 -> null
- cpu_threads_per_core = 2 -> null
- disable_api_termination = false -> null
- ebs_optimized = false -> null
- get_password_data = false -> null
- id = "i-03a8285fc49f48a69" -> null
- instance_state = "running" -> null
- instance_type = "t3.micro" -> null
- ipv6_address_count = 0 -> null
- ipv6_addresses = [] -> null
- monitoring = false -> null
- primary_network_interface_id = "eni-05f51ded8af0b5033" -> null
- private_dns = "ip-172-31-36-204.ec2.internal" -> null
- private_ip = "172.31.36.204" -> null
- security_groups = [
- "default",
] -> null
- source_dest_check = true -> null
- subnet_id = "subnet-01c13b00a5531828e" -> null
- tags = {
- "Name" = "two"
} -> null
- tenancy = "default" -> null
- volume_tags = {} -> null
- vpc_security_group_ids = [
- "sg-06aabe12f0a1b34fd",
] -> null
- credit_specification {
- cpu_credits = "unlimited" -> null
}
- root_block_device {
- delete_on_termination = true -> null
- encrypted = false -> null
- iops = 100 -> null
- volume_id = "vol-06ffd6ab6d4e8f671" -> null
- volume_size = 8 -> null
- volume_type = "gp2" -> null
}
}
Plan: 0 to add, 0 to change, 1 to destroy.
Ссылка: Ресурс Terraform For_Each