diff --git a/_example/alb/example.tf b/_example/alb/example.tf index 0b941a8..cd3e7f4 100644 --- a/_example/alb/example.tf +++ b/_example/alb/example.tf @@ -119,7 +119,7 @@ module "ec2" { instance_profile_enabled = true iam_instance_profile = module.iam-role.name - + ebs_optimized = false ebs_volume_enabled = true ebs_volume_type = "gp2" @@ -137,6 +137,7 @@ module "alb" { security_groups = [module.ssh.security_group_ids, module.http_https.security_group_ids] subnets = module.public_subnets.public_subnet_id enable_deletion_protection = false + with_target_group = true target_id = module.ec2.instance_id vpc_id = module.vpc.vpc_id diff --git a/_example/clb/example.tf b/_example/clb/example.tf index 29a52a5..528c9ba 100644 --- a/_example/clb/example.tf +++ b/_example/clb/example.tf @@ -117,7 +117,7 @@ module "ec2" { instance_profile_enabled = true iam_instance_profile = module.iam-role.name - + ebs_optimized = false ebs_volume_enabled = true ebs_volume_type = "gp2" @@ -134,6 +134,8 @@ module "clb" { target_id = module.ec2.instance_id security_groups = [module.ssh.security_group_ids, module.http_https.security_group_ids] subnets = module.public_subnets.public_subnet_id + with_target_group = true + listeners = [ { diff --git a/_example/nlb/example.tf b/_example/nlb/example.tf index f5846d8..71aed76 100644 --- a/_example/nlb/example.tf +++ b/_example/nlb/example.tf @@ -135,6 +135,7 @@ module "nlb" { instance_count = module.ec2.instance_count subnets = module.public_subnets.public_subnet_id enable_deletion_protection = false + with_target_group = true target_id = module.ec2.instance_id vpc_id = module.vpc.vpc_id diff --git a/main.tf b/main.tf index 6d5dccc..ddc7d11 100644 --- a/main.tf +++ b/main.tf @@ -60,7 +60,7 @@ resource "aws_lb" "main" { # Module : LOAD BALANCER LISTENER HTTPS # Description : Provides a Load Balancer Listener resource. resource "aws_lb_listener" "https" { - count = var.enable == true && var.https_enabled == true && var.load_balancer_type == "application" ? 1 : 0 + count = var.enable == true && var.with_target_group && var.https_enabled == true && var.load_balancer_type == "application" ? 1 : 0 load_balancer_arn = element(aws_lb.main.*.arn, count.index) port = var.https_port @@ -85,7 +85,7 @@ resource "aws_lb_listener" "https" { # Module : LOAD BALANCER LISTENER HTTP # Description : Provides a Load Balancer Listener resource. resource "aws_lb_listener" "http" { - count = var.enable == true && var.http_enabled == true && var.load_balancer_type == "application" ? 1 : 0 + count = var.enable == true && var.with_target_group && var.http_enabled == true && var.load_balancer_type == "application" ? 1 : 0 load_balancer_arn = element(aws_lb.main.*.arn, count.index) port = var.http_port @@ -104,7 +104,7 @@ resource "aws_lb_listener" "http" { # Module : LOAD BALANCER LISTENER HTTPS # Description : Provides a Load Balancer Listener resource. resource "aws_lb_listener" "nhttps" { - count = var.enable == true && var.https_enabled == true && var.load_balancer_type == "network" ? length(var.https_listeners) : 0 + count = var.enable == true && var.with_target_group && var.https_enabled == true && var.load_balancer_type == "network" ? length(var.https_listeners) : 0 load_balancer_arn = element(aws_lb.main.*.arn, count.index) port = var.https_listeners[count.index]["port"] @@ -120,7 +120,7 @@ resource "aws_lb_listener" "nhttps" { # Module : LOAD BALANCER LISTENER HTTP # Description : Provides a Load Balancer Listener resource. resource "aws_lb_listener" "nhttp" { - count = var.enable == true && var.load_balancer_type == "network" ? length(var.http_tcp_listeners) : 0 + count = var.enable == true && var.with_target_group && var.load_balancer_type == "network" ? length(var.http_tcp_listeners) : 0 load_balancer_arn = element(aws_lb.main.*.arn, 0) port = var.http_tcp_listeners[count.index]["port"] @@ -134,7 +134,7 @@ resource "aws_lb_listener" "nhttp" { # Module : LOAD BALANCER TARGET GROUP # Description : Provides a Target Group resource for use with Load Balancer resources. resource "aws_lb_target_group" "main" { - count = var.enable ? length(var.target_groups) : 0 + count = var.enable && var.with_target_group ? length(var.target_groups) : 0 name = format("%s-%s", module.labels.id, count.index) port = lookup(var.target_groups[count.index], "backend_port", null) protocol = lookup(var.target_groups[count.index], "backend_protocol", null) != null ? upper(lookup(var.target_groups[count.index], "backend_protocol")) : null @@ -177,7 +177,7 @@ resource "aws_lb_target_group" "main" { # Description : Provides the ability to register instances and containers with an # Application Load Balancer (ALB) or Network Load Balancer (NLB) target group. resource "aws_lb_target_group_attachment" "attachment" { - count = var.enable && var.load_balancer_type == "application" && var.target_type == "" ? var.instance_count : 0 + count = var.enable && var.with_target_group && var.load_balancer_type == "application" && var.target_type == "" ? var.instance_count : 0 target_group_arn = element(aws_lb_target_group.main.*.arn, count.index) target_id = element(var.target_id, count.index) @@ -185,7 +185,7 @@ resource "aws_lb_target_group_attachment" "attachment" { } resource "aws_lb_target_group_attachment" "nattachment" { - count = var.enable && var.load_balancer_type == "network" ? length(var.https_listeners) : 0 + count = var.enable && var.with_target_group && var.load_balancer_type == "network" ? length(var.https_listeners) : 0 target_group_arn = element(aws_lb_target_group.main.*.arn, count.index) target_id = element(var.target_id, 0) diff --git a/variables.tf b/variables.tf index 7920901..610d387 100644 --- a/variables.tf +++ b/variables.tf @@ -371,3 +371,8 @@ variable "https_listener_rules" { default = [] } +variable "with_target_group" { + type = bool + default = true + description = "Create LoadBlancer without target group" +}