I have a use case where I wanted to create 2 application load balancer, one will be public another will be private using terraform modules.
I know that we can create 2 directories with the same sort of code for private and public the parameters accordingly but I was trying to create both load balancer by using interpolation of terraform as mentioned below.
ALB Module:
resource "aws_alb" "default" {
name = "${var.name}-${var.env_name}-${var.internal == "false" ? "public" : "private" }"
internal = "${var.internal == "false" ? "false" : "true" }"
security_groups = ["${var.internal == "false" ? var.sg80 : var.instance_in_all }"]
subnets = ["${var.internal == "false" ? var.public_subnets : var.private_subnets }"]
}
main.tf from where I am calling alb module.
module "public-alb" {
source = "../../modules/alb"
name = "example"
internal = "false" #internal: Give it false for public load balancer.
env_name = "production"
vpc_id = "${module.vpc.vpc_id}"
public_subnets = "${module.vpc.public_subnets}"
private_subnets = "${module.vpc.public_subnets}" #This does not matter here because check condition in internal file.
sg80 = "${module.security-group.sg80}"
instance_in_all = "${module.security-group.instance_in_all}" #This does not matter here because check condition in internal file.
}
module "private-alb" {
source = "../../modules/alb"
name = "example"
internal = "true" #internal: Give it false for public load balancer.
env_name = "production"
vpc_id = "${module.vpc.vpc_id}"
private_subnets = "${module.vpc.public_subnets}"
public_subnets = "${module.vpc.public_subnets}" #This does not matter here because check condition in internal file.
sg80 = "${module.security-group.sg80}" #This does not matter here because check condition in internal file.
instance_in_all = "${module.security-group.instance_in_all}"
}
So for the public load balancer, I have to pass private subnets and internal security group same with private load balancer I have to pass public subnets and external security group because I am passing these variables from variables.tf(mentioned below) which is not necessary.
variable "vpc_id" {}
#variable "private_subnets" { type = "list"}
variable "sg80" {}
variable "public_subnets" {
type = "list"
}
variable "name" {}
variable "internal" {}
variable "env_name" {}
variable "private_subnets" {
type = "list"
}
variable "instance_in_all" {}
I wanted to know is it the right way to do it or the separate directory is the only workaround as of now.