I make my own terraform module that creates an EC-2 instance and a role for it:
resource "aws_instance" "instance" {
ami=var.ami
instance_type="t3a.micro"
key_name = var.ssh_key
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
root_block_device {
volume_size = 30
volume_type = "gp3"
}
vpc_security_group_ids=var.ec2_security_groups
# Ommitted for verbosity reduction
}
resource "aws_route53_record" "domain" {
zone_id = var.domain_zone
name = local.domain
type = "CNAME"
ttl = 300
records = [var.lb.dns_name]
}
# Load Balance
resource "aws_lb_target_group" "tg" {
name = "${local.name_with_env}-Tg"
port = 80
protocol = "HTTP"
target_type="instance"
vpc_id=var.lb_tg_vpc
}
This is located into a module named ec2_lb
in my terraform project having the following file structure:
- modules
-- ec2_lb
--- main.tf <<< File above
- client1
-- main.tf
- client2
-- main.tf
Upon each client (clientX/main.tf
) I want to create 2 ec2 instances one for production and one for staging:
module "test_staging_ec2_lb" {
source = "../modules/ec2_lb"
# Stuff ommitted for simplicity
}
module "test_production_ec2_lb" {
source = "../modules/ec2_lb"
# Stuff ommitted for simplicity
}
But I feel like the module import test_production_ec2_lb
will replace the created resources created from test_staging_ec2_lb
one. Is there a way to namespace it without resorting into creating duplicate folders per customer and per environment:
- modules
-- ec2_lb
--- main.tf <<< File above
- client1_production
-- main.tf
- client1_staging
-- main.tf
- client2_production
-- main.tf
- client2_staging
-- main.tf