Skip to content

Commit c6a69b8

Browse files
committed
refactor(sub-modules): only define block variables if the input variable is strictly not null
1 parent bfeea32 commit c6a69b8

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

modules/acm/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ resource "aws_acm_certificate" "amazon_issued" {
1111
key_algorithm = each.value.key_algorithm
1212

1313
dynamic "options" {
14-
for_each = length(each.value.options != null ? each.value.options : {}) > 0 ? [1] : []
14+
for_each = try(each.value.options, null) != null ? [1] : []
1515

1616
content {
1717
certificate_transparency_logging_preference = each.value.options.certificate_transparency_logging_preference
1818
}
1919
}
2020

2121
dynamic "validation_option" {
22-
for_each = length(each.value.validation_option != null ? each.value.validation_option : {}) > 0 ? [1] : []
22+
for_each = try(each.value.validation_option, null) != null ? [1] : []
2323

2424
content {
2525
domain_name = each.value.validation_option.domain_name

modules/alb/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ No modules.
3131
|------|-------------|------|---------|:--------:|
3232
| <a name="input_enable_deletion_protection"></a> [enable\_deletion\_protection](#input\_enable\_deletion\_protection) | If true, deletion of the load balancer will be disabled via the AWS API. This will prevent Terraform from deleting the load balancer | `bool` | `false` | no |
3333
| <a name="input_internal"></a> [internal](#input\_internal) | Either the ALB is internal or internet-facing | `bool` | `false` | no |
34-
| <a name="input_listeners"></a> [listeners](#input\_listeners) | Listeners to forward ALB ingress to desired Target Groups | <pre>map(object({<br> default_action = list(object({<br> type = string<br> target_group = string<br> authenticate_cognito = optional(any, {})<br> authenticate_oidc = optional(any, {})<br> fixed_response = optional(any, {})<br> forward = optional(any, {})<br> order = optional(number)<br> redirect = optional(any, {})<br> }))<br> alpn_policy = optional(string)<br> certificate_arn = optional(string)<br> mutual_authentication = optional(any, {})<br> port = optional(number)<br> protocol = optional(string)<br> ssl_policy = optional(string)<br> tags = optional(map(any), {})<br> }))</pre> | n/a | yes |
34+
| <a name="input_listeners"></a> [listeners](#input\_listeners) | Listeners to forward ALB ingress to desired Target Groups | <pre>map(object({<br> default_action = list(object({<br> type = string<br> target_group = string<br> authenticate_cognito = optional(any, null)<br> authenticate_oidc = optional(any, null)<br> fixed_response = optional(any, null)<br> forward = optional(any, null)<br> order = optional(number)<br> redirect = optional(any, null)<br> }))<br> alpn_policy = optional(string)<br> certificate_arn = optional(string)<br> mutual_authentication = optional(any, null)<br> port = optional(number)<br> protocol = optional(string)<br> ssl_policy = optional(string)<br> tags = optional(map(any), {})<br> }))</pre> | n/a | yes |
3535
| <a name="input_name"></a> [name](#input\_name) | Name of the ALB | `string` | `""` | no |
3636
| <a name="input_preserve_host_header"></a> [preserve\_host\_header](#input\_preserve\_host\_header) | Whether the ALB should preserve the Host Header in HTTP requests and send it to the target without any changes | `bool` | `false` | no |
3737
| <a name="input_security_groups_ids"></a> [security\_groups\_ids](#input\_security\_groups\_ids) | Identifiers of Security Groups for the ALB | `list(string)` | `[]` | no |
3838
| <a name="input_subnets_ids"></a> [subnets\_ids](#input\_subnets\_ids) | Identifiers of the VPC Subnets where the ALB will be active | `list(string)` | n/a | yes |
3939
| <a name="input_tags"></a> [tags](#input\_tags) | Resource Tags for the ALB | `map(any)` | `{}` | no |
40-
| <a name="input_target_groups"></a> [target\_groups](#input\_target\_groups) | Target Groups to create and forward ALB ingress to | <pre>map(object({<br> name = optional(string)<br> vpc_id = optional(string)<br> port = optional(number)<br> protocol = optional(string)<br> target_type = optional(string)<br> health_check = optional(any, {})<br> tags = optional(map(any), {})<br> }))</pre> | `{}` | no |
40+
| <a name="input_target_groups"></a> [target\_groups](#input\_target\_groups) | Target Groups to create and forward ALB ingress to | <pre>map(object({<br> name = optional(string)<br> vpc_id = optional(string)<br> port = optional(number)<br> protocol = optional(string)<br> target_type = optional(string)<br> health_check = optional(any, null)<br> tags = optional(map(any), {})<br> }))</pre> | `{}` | no |
4141

4242
## Outputs
4343

modules/alb/main.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "aws_lb_target_group" "this" {
3131
target_type = each.value.target_type
3232

3333
dynamic "health_check" {
34-
for_each = length(each.value.health_check != null ? each.value.health_check : {}) > 0 ? [1] : []
34+
for_each = try(each.value.health_check, null) != null ? [1] : []
3535

3636
content {
3737
enabled = try(each.value.health_check.enabled, null)
@@ -65,7 +65,7 @@ resource "aws_lb_listener" "this" {
6565
ssl_policy = each.value.ssl_policy
6666

6767
dynamic "mutual_authentication" {
68-
for_each = length(each.value.mutual_authentication != null ? each.value.mutual_authentication : {}) > 0 ? [1] : []
68+
for_each = try(each.value.mutual_authentication, null) != null ? [1] : []
6969

7070
content {
7171
mode = each.value.mutual_authentication.mode
@@ -84,7 +84,7 @@ resource "aws_lb_listener" "this" {
8484
order = default_action.value.order
8585

8686
dynamic "authenticate_cognito" {
87-
for_each = length(default_action.value.authenticate_cognito != null ? default_action.value.authenticate_cognito : {}) > 0 ? [1] : []
87+
for_each = try(default_action.value.authenticate_cognito, null) != null ? [1] : []
8888

8989
content {
9090
user_pool_arn = default_action.value.authenticate_cognito.user_pool_arn
@@ -99,7 +99,7 @@ resource "aws_lb_listener" "this" {
9999
}
100100

101101
dynamic "authenticate_oidc" {
102-
for_each = length(default_action.value.authenticate_oidc != null ? default_action.value.authenticate_oidc : {}) > 0 ? [1] : []
102+
for_each = try(default_action.value.authenticate_oidc, null) != null ? [1] : []
103103

104104
content {
105105
authorization_endpoint = default_action.value.authenticate_oidc.authorization_endpoint
@@ -117,7 +117,7 @@ resource "aws_lb_listener" "this" {
117117
}
118118

119119
dynamic "fixed_response" {
120-
for_each = length(default_action.value.fixed_response != null ? default_action.value.fixed_response : {}) > 0 ? [1] : []
120+
for_each = try(default_action.value.fixed_response, null) != null ? [1] : []
121121

122122
content {
123123
content_type = default_action.value.fixed_response.content_type
@@ -127,7 +127,7 @@ resource "aws_lb_listener" "this" {
127127
}
128128

129129
dynamic "forward" {
130-
for_each = length(default_action.value.forward != null ? default_action.value.forward : {}) > 0 ? [1] : []
130+
for_each = try(default_action.value.forward, null) != null ? [1] : []
131131

132132
content {
133133
dynamic "target_group" {
@@ -141,7 +141,7 @@ resource "aws_lb_listener" "this" {
141141
}
142142

143143
dynamic "stickiness" {
144-
for_each = length(default_action.value.forward.stickiness != null ? default_action.value.forward.stickiness : {}) > 0 ? [1] : []
144+
for_each = try(default_action.value.forward.stickiness, null) != null ? [1] : []
145145

146146
content {
147147
duration = default_action.value.forward.stickiness.duration
@@ -152,7 +152,7 @@ resource "aws_lb_listener" "this" {
152152
}
153153

154154
dynamic "redirect" {
155-
for_each = length(default_action.value.redirect != null ? default_action.value.redirect : {}) > 0 ? [1] : []
155+
for_each = try(default_action.value.redirect, null) != null ? [1] : []
156156

157157
content {
158158
status_code = default_action.value.redirect.status_code

modules/alb/variables.tf

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ variable "target_groups" {
5555
port = optional(number)
5656
protocol = optional(string)
5757
target_type = optional(string)
58-
health_check = optional(any, {})
58+
health_check = optional(any, null)
5959
tags = optional(map(any), {})
6060
}))
6161
default = {}
@@ -71,16 +71,16 @@ variable "listeners" {
7171
default_action = list(object({
7272
type = string
7373
target_group = string
74-
authenticate_cognito = optional(any, {})
75-
authenticate_oidc = optional(any, {})
76-
fixed_response = optional(any, {})
77-
forward = optional(any, {})
74+
authenticate_cognito = optional(any, null)
75+
authenticate_oidc = optional(any, null)
76+
fixed_response = optional(any, null)
77+
forward = optional(any, null)
7878
order = optional(number)
79-
redirect = optional(any, {})
79+
redirect = optional(any, null)
8080
}))
8181
alpn_policy = optional(string)
8282
certificate_arn = optional(string)
83-
mutual_authentication = optional(any, {})
83+
mutual_authentication = optional(any, null)
8484
port = optional(number)
8585
protocol = optional(string)
8686
ssl_policy = optional(string)

modules/capacity-provider/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ resource "aws_ecs_capacity_provider" "this" {
1111
auto_scaling_group_arn = coalesce(each.value.auto_scaling_group_arn, var.default_auto_scaling_group_arn)
1212

1313
dynamic "managed_scaling" {
14-
for_each = length(each.value.managed_scaling != null ? each.value.managed_scaling : {}) > 0 ? [1] : []
14+
for_each = try(each.value.managed_scaling, null) != null ? [1] : []
1515

1616
content {
1717
instance_warmup_period = each.value.managed_scaling.instance_warmup_period

0 commit comments

Comments
 (0)