I am trying to attach subnet A to the first ec2 instance and subnet B to the second ec2 instance.
I am not sure how to make that loop. Please suggest.
Below is the main.tf file of my ec2 module.
resource "aws_instance" "web_pub_servers" {
for_each = var.web_vm_attribute
ami = var.ami
instance_type = var.instance_type
vpc_security_group_ids = [var.ssh_security_group_id]
key_name = each.value.key_name
**subnet_id =** ??
tags = {
Name = "web_${each.value.name}"
}
}
and this is the Variables.tf file of the ec2 module
variable "ami" {}
variable "instance_type" {}
variable "ssh_security_group_id" {}
variable "public_subnet_a_id" {}
variable "public_subnet_b_id" {}
variable "web_vm_attribute" {
type = map(object({
name = string
key_name = string
}))
default = {}
}
variable "pri_vm_attribute" {
type = map(object({
name = string
key_name = string
}))
default = {}
}
This is the output file of my VPC module.
output "region" {
value = var.region
}
output "project_name" {
value = var.project_name
}
output "vpc_id" {
value = aws_vpc.vpc.id
}
output "public_subnet_a_id" {
value = aws_subnet.public_subnet_a.id
}
output "public_subnet_b_id" {
value = aws_subnet.public_subnet_b.id
}
output "internet_gateway" {
value = aws_internet_gateway.internet_gateway
}
I want to achieve this using for_each only. I don't want to use count. None of my resources are present in the AWS. I want to create VPC, Subnets, Internet gateway, Nat gateway and ec2 instances in one go.
Here is a quick trick you can use to alternate between the subnets:
subnets = [var.public_subnet_a_id, var.public_subnet_b_id]local.subnets[index(keys(var.web_vm_attribute), each.key) % length(local.subnets)]Here is some sample code
the output of
terraform planon that code is:You can see in the output how the
subnetalternates between the values of twopublic_subnet_a_idandpublic_subnet_b_id...And of course these are just sample values, and I'm using a
null_resourceyou can do the same with yourresource "aws_instance"