Skip to content

Commit 80eaa39

Browse files
authored
[ISSUE #13] Replacing count with for_each (#18)
* Replacing count with for_each * Format change * Fixing validation issues * Fixing error with empty list * Fixing error with empty list * Fixing error with empty list * Removing unneeded role * Replacing count with for_each * Replacing count with for_each * Replacing count with for_each
1 parent 1655307 commit 80eaa39

File tree

6 files changed

+27
-68
lines changed

6 files changed

+27
-68
lines changed

autoscaling.tf

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#------------------------------------------------------------------------------
2-
# AWS ECS Auto Scale Role
3-
#------------------------------------------------------------------------------
4-
resource "aws_iam_role" "ecs_autoscale_role" {
5-
name = "${var.name_prefix}-ecs-autoscale-role"
6-
assume_role_policy = file("${path.module}/files/iam/ecs_autoscale_iam_role.json")
7-
}
8-
9-
resource "aws_iam_role_policy" "ecs_autoscale_role_policy" {
10-
name = "${var.name_prefix}-ecs-autoscale-role-policy"
11-
role = aws_iam_role.ecs_autoscale_role.id
12-
policy = file(
13-
"${path.module}/files/iam/ecs_autoscale_iam_role_policy.json",
14-
)
15-
}
16-
171
#------------------------------------------------------------------------------
182
# AWS Auto Scaling - CloudWatch Alarm CPU High
193
#------------------------------------------------------------------------------
@@ -99,7 +83,6 @@ resource "aws_appautoscaling_target" "scale_target" {
9983
service_namespace = "ecs"
10084
resource_id = "service/${var.ecs_cluster_name}/${aws_ecs_service.service.name}"
10185
scalable_dimension = "ecs:service:DesiredCount"
102-
role_arn = aws_iam_role.ecs_autoscale_role.arn
10386
min_capacity = var.scale_target_min_capacity
10487
max_capacity = var.scale_target_max_capacity
10588
}

examples/test/main.tf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module "base-network" {
2020

2121
module "load_balancer" {
2222
source = "cn-terraform/ecs-alb/aws"
23-
version = "1.0.5"
23+
version = "1.0.6"
2424
name_prefix = "test-alb"
2525
vpc_id = module.base-network.vpc_id
2626
private_subnets = module.base-network.private_subnets_ids
@@ -48,6 +48,8 @@ module "service" {
4848
lb_arn = module.load_balancer.aws_lb_lb_arn
4949
lb_http_tgs_arns = module.load_balancer.lb_http_tgs_arns
5050
lb_https_tgs_arns = module.load_balancer.lb_https_tgs_arns
51+
lb_http_tgs_ports = module.load_balancer.lb_http_tgs_ports
52+
lb_https_tgs_ports = module.load_balancer.lb_https_tgs_ports
5153
lb_http_listeners_arns = module.load_balancer.lb_http_listeners_arns
5254
lb_https_listeners_arns = module.load_balancer.lb_https_listeners_arns
5355
load_balancer_sg_id = module.load_balancer.aws_security_group_lb_access_sg_id

files/iam/ecs_autoscale_iam_role.json

Lines changed: 0 additions & 12 deletions
This file was deleted.

files/iam/ecs_autoscale_iam_role_policy.json

Lines changed: 0 additions & 24 deletions
This file was deleted.

main.tf

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@ data "aws_lb" "lb" {
77
}
88

99
data "aws_lb_target_group" "lb_http_target_groups" {
10-
count = length(var.lb_http_tgs_arns)
11-
arn = element(var.lb_http_tgs_arns, count.index)
10+
for_each = var.lb_http_tgs_arns != null ? toset(var.lb_http_tgs_arns) : toset([])
11+
arn = each.key
1212
}
1313

1414
data "aws_lb_target_group" "lb_https_target_groups" {
15-
count = length(var.lb_https_tgs_arns)
16-
arn = element(var.lb_https_tgs_arns, count.index)
15+
for_each = toset(var.lb_https_tgs_arns)
16+
arn = each.key
1717
}
1818

1919
data "aws_lb_listener" "lb_http_listeners" {
20-
count = length(var.lb_http_listeners_arns)
21-
arn = element(var.lb_http_listeners_arns, count.index)
20+
for_each = var.lb_http_listeners_arns != null ? toset(var.lb_http_listeners_arns) : toset([])
21+
arn = each.key
2222
}
2323

2424
data "aws_lb_listener" "lb_https_listeners" {
25-
count = length(var.lb_https_listeners_arns)
26-
arn = element(var.lb_https_listeners_arns, count.index)
25+
for_each = var.lb_https_listeners_arns != null ? toset(var.lb_https_listeners_arns) : toset([])
26+
arn = each.key
2727
}
2828

2929
#------------------------------------------------------------------------------
@@ -111,21 +111,21 @@ resource "aws_security_group" "ecs_tasks_sg" {
111111
}
112112

113113
resource "aws_security_group_rule" "ingress_through_http" {
114-
count = length(data.aws_lb_target_group.lb_http_target_groups)
114+
for_each = var.lb_http_tgs_ports != null ? toset(var.lb_http_tgs_ports) : toset([])
115115
security_group_id = aws_security_group.ecs_tasks_sg.id
116116
type = "ingress"
117-
from_port = element(data.aws_lb_target_group.lb_http_target_groups.*.port, count.index)
118-
to_port = element(data.aws_lb_target_group.lb_http_target_groups.*.port, count.index)
117+
from_port = each.key
118+
to_port = each.key
119119
protocol = "tcp"
120120
source_security_group_id = var.load_balancer_sg_id
121121
}
122122

123123
resource "aws_security_group_rule" "ingress_through_https" {
124-
count = length(data.aws_lb_target_group.lb_https_target_groups)
124+
for_each = var.lb_https_tgs_ports != null ? toset(var.lb_https_tgs_ports) : toset([])
125125
security_group_id = aws_security_group.ecs_tasks_sg.id
126126
type = "ingress"
127-
from_port = element(data.aws_lb_target_group.lb_https_target_groups.*.port, count.index)
128-
to_port = element(data.aws_lb_target_group.lb_https_target_groups.*.port, count.index)
127+
from_port = each.key
128+
to_port = each.key
129129
protocol = "tcp"
130130
source_security_group_id = var.load_balancer_sg_id
131131
}

variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,21 @@ variable "lb_http_tgs_arns" {
184184
type = list(string)
185185
}
186186

187+
variable "lb_http_tgs_ports" {
188+
description = "List of HTTP LB Target Group Ports"
189+
type = list(string)
190+
}
191+
187192
variable "lb_https_tgs_arns" {
188193
description = "List of HTTPS LB Target Group ARNs"
189194
type = list(string)
190195
}
191196

197+
variable "lb_https_tgs_ports" {
198+
description = "List of HTTPS LB Target Group Ports"
199+
type = list(string)
200+
}
201+
192202
variable "lb_http_listeners_arns" {
193203
description = "List of HTTP LB Listeners ARNs"
194204
type = list(string)

0 commit comments

Comments
 (0)