Skip to content

Commit

Permalink
Fixes #134 - Provides guard against referencing aws_lb_target_group.d…
Browse files Browse the repository at this point in the history
…efault when disabled

* disables aws_lb_listener.http_forward when default_target_group is not enabled
* disables aws_lb_listener.http_redirect when default_target_group is disabled unless default http response exists
* disables aws_lb_listener.https when default_target_group is disabled unless default secure https response exists

Fixes this Validation error when default_target_group_enabled == 0 :
```
module.alb.aws_lb_listener.http_forward[0]: Creating...
╷
│ Error: creating ELBv2 Listener
(arn:aws:elasticloadbalancing:...:...:loadbalancer/...):
ValidationError: A target group ARN must be specified
│       status code: 400, request id:
7cf9d727-fc77-4d32-a160-cbd175e16e20
│
│   with module.alb.aws_lb_listener.http_forward[0],
│   on .terraform/modules/alb/main.tf line 150, in resource
"aws_lb_listener" "http_forward":
│  150: resource "aws_lb_listener" "http_forward" {

```
  • Loading branch information
spazm committed Oct 25, 2023
1 parent fb4ec8e commit 8a9eb28
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ resource "aws_lb_target_group" "default" {
resource "aws_lb_listener" "http_forward" {
#bridgecrew:skip=BC_AWS_GENERAL_43 - Skipping Ensure that load balancer is using TLS 1.2.
#bridgecrew:skip=BC_AWS_NETWORKING_29 - Skipping Ensure ALB Protocol is HTTPS
count = module.this.enabled && var.http_enabled && var.http_redirect != true ? 1 : 0
count = (
module.this.enabled &&
var.http_enabled &&
var.http_redirect != true &&
(var.listener_http_fixed_response != null || var.default_target_group_enabled)
? 1 : 0
)
load_balancer_arn = one(aws_lb.default[*].arn)
port = var.http_port
protocol = "HTTP"
Expand All @@ -172,7 +178,13 @@ resource "aws_lb_listener" "http_forward" {
}

resource "aws_lb_listener" "http_redirect" {
count = module.this.enabled && var.http_enabled && var.http_redirect == true ? 1 : 0
count = (
module.this.enabled &&
var.http_enabled &&
var.http_redirect == true &&
var.default_target_group_enabled
? 1 : 0
)
load_balancer_arn = one(aws_lb.default[*].arn)
port = var.http_port
protocol = "HTTP"
Expand All @@ -192,7 +204,12 @@ resource "aws_lb_listener" "http_redirect" {

resource "aws_lb_listener" "https" {
#bridgecrew:skip=BC_AWS_GENERAL_43 - Skipping Ensure that load balancer is using TLS 1.2.
count = module.this.enabled && var.https_enabled ? 1 : 0
count = (
module.this.enabled &&
var.https_enabled &&
(var.listener_https_fixed_response != null || var.default_target_group_enabled)
? 1 : 0
)
load_balancer_arn = one(aws_lb.default[*].arn)

port = var.https_port
Expand Down

0 comments on commit 8a9eb28

Please sign in to comment.