我有一个用例,我想创建 2 个应用程序负载均衡器,一个是公共的,另一个是私有的,使用 terraform 模块。
我知道我们可以使用相同类型的代码为私有和公共参数创建 2 个目录,但我试图通过使用 terraform 的插值来创建两个负载均衡器,如下所述。
ALB 模块:
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从我调用 alb 模块的地方。
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}"
}
因此,对于公共负载均衡器,我必须传递与私有负载均衡器相同的私有子网和内部安全组.
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" {}
我想知道这是正确的方法还是单独的目录是目前唯一的解决方法。