@@ -34,8 +34,8 @@ module "ecs_cluster" {
3434 default_capacity_provider_use_fargate = false
3535 autoscaling_capacity_providers = {
3636 # On-demand instances
37- ex-1 = {
38- auto_scaling_group_arn = module.autoscaling[" ex-1 " ].autoscaling_group_arn
37+ ex_1 = {
38+ auto_scaling_group_arn = module.autoscaling[" ex_1 " ].autoscaling_group_arn
3939 managed_termination_protection = " ENABLED"
4040
4141 managed_scaling = {
@@ -51,8 +51,8 @@ module "ecs_cluster" {
5151 }
5252 }
5353 # Spot instances
54- ex-2 = {
55- auto_scaling_group_arn = module.autoscaling[" ex-2 " ].autoscaling_group_arn
54+ ex_2 = {
55+ auto_scaling_group_arn = module.autoscaling[" ex_2 " ].autoscaling_group_arn
5656 managed_termination_protection = " ENABLED"
5757
5858 managed_scaling = {
@@ -86,8 +86,8 @@ module "ecs_service" {
8686 requires_compatibilities = [" EC2" ]
8787 capacity_provider_strategy = {
8888 # On-demand instances
89- ex-1 = {
90- capacity_provider = module.ecs_cluster.autoscaling_capacity_providers[" ex-1 " ].name
89+ ex_1 = {
90+ capacity_provider = module.ecs_cluster.autoscaling_capacity_providers[" ex_1 " ].name
9191 weight = 1
9292 base = 1
9393 }
@@ -125,7 +125,7 @@ module "ecs_service" {
125125
126126 load_balancer = {
127127 service = {
128- target_group_arn = element ( module. alb . target_group_arns , 0 )
128+ target_group_arn = module.alb.target_groups[ " ex_ecs " ].arn
129129 container_name = local.container_name
130130 container_port = local.container_port
131131 }
@@ -139,7 +139,7 @@ module "ecs_service" {
139139 to_port = local.container_port
140140 protocol = " tcp"
141141 description = " Service port"
142- source_security_group_id = module.alb_sg .security_group_id
142+ source_security_group_id = module.alb .security_group_id
143143 }
144144 }
145145
@@ -155,51 +155,72 @@ data "aws_ssm_parameter" "ecs_optimized_ami" {
155155 name = " /aws/service/ecs/optimized-ami/amazon-linux-2/recommended"
156156}
157157
158- module "alb_sg" {
159- source = " terraform-aws-modules/security-group/aws"
160- version = " ~> 5.0"
161-
162- name = " ${ local . name } -service"
163- description = " Service security group"
164- vpc_id = module. vpc . vpc_id
165-
166- ingress_rules = [" http-80-tcp" ]
167- ingress_cidr_blocks = [" 0.0.0.0/0" ]
168-
169- egress_rules = [" all-all" ]
170- egress_cidr_blocks = module. vpc . private_subnets_cidr_blocks
171-
172- tags = local. tags
173- }
174-
175158module "alb" {
176159 source = " terraform-aws-modules/alb/aws"
177- version = " ~> 8 .0"
160+ version = " ~> 9 .0"
178161
179162 name = local. name
180163
181164 load_balancer_type = " application"
182165
183- vpc_id = module. vpc . vpc_id
184- subnets = module. vpc . public_subnets
185- security_groups = [module . alb_sg . security_group_id ]
166+ vpc_id = module. vpc . vpc_id
167+ subnets = module. vpc . public_subnets
186168
187- http_tcp_listeners = [
188- {
189- port = local.container_port
190- protocol = " HTTP"
191- target_group_index = 0
192- },
193- ]
169+ # For example only
170+ enable_deletion_protection = false
194171
195- target_groups = [
196- {
197- name = " ${ local . name } -${ local . container_name } "
198- backend_protocol = " HTTP"
199- backend_port = local.container_port
200- target_type = " ip"
201- },
202- ]
172+ # Security Group
173+ security_group_ingress_rules = {
174+ all_http = {
175+ from_port = 80
176+ to_port = 80
177+ ip_protocol = " tcp"
178+ cidr_ipv4 = " 0.0.0.0/0"
179+ }
180+ }
181+ security_group_egress_rules = {
182+ all = {
183+ ip_protocol = " -1"
184+ cidr_ipv4 = module.vpc.vpc_cidr_block
185+ }
186+ }
187+
188+ listeners = {
189+ ex_http = {
190+ port = 80
191+ protocol = " HTTP"
192+
193+ forward = {
194+ target_group_key = " ex_ecs"
195+ }
196+ }
197+ }
198+
199+ target_groups = {
200+ ex_ecs = {
201+ backend_protocol = " HTTP"
202+ backend_port = local.container_port
203+ target_type = " ip"
204+ deregistration_delay = 5
205+ load_balancing_cross_zone_enabled = true
206+
207+ health_check = {
208+ enabled = true
209+ healthy_threshold = 5
210+ interval = 30
211+ matcher = " 200"
212+ path = " /"
213+ port = " traffic-port"
214+ protocol = " HTTP"
215+ timeout = 5
216+ unhealthy_threshold = 2
217+ }
218+
219+ # Theres nothing to attach here in this definition. Instead,
220+ # ECS will attach the IPs of the tasks to this target group
221+ create_attachment = false
222+ }
223+ }
203224
204225 tags = local. tags
205226}
@@ -210,12 +231,13 @@ module "autoscaling" {
210231
211232 for_each = {
212233 # On-demand instances
213- ex-1 = {
234+ ex_1 = {
214235 instance_type = " t3.large"
215236 use_mixed_instances_policy = false
216237 mixed_instances_policy = {}
217238 user_data = <<- EOT
218239 #!/bin/bash
240+
219241 cat <<'EOF' >> /etc/ecs/ecs.config
220242 ECS_CLUSTER=${ local . name }
221243 ECS_LOGLEVEL=debug
@@ -225,7 +247,7 @@ module "autoscaling" {
225247 EOT
226248 }
227249 # Spot instances
228- ex-2 = {
250+ ex_2 = {
229251 instance_type = " t3.medium"
230252 use_mixed_instances_policy = true
231253 mixed_instances_policy = {
@@ -248,6 +270,7 @@ module "autoscaling" {
248270 }
249271 user_data = <<- EOT
250272 #!/bin/bash
273+
251274 cat <<'EOF' >> /etc/ecs/ecs.config
252275 ECS_CLUSTER=${ local . name }
253276 ECS_LOGLEVEL=debug
@@ -308,7 +331,7 @@ module "autoscaling_sg" {
308331 computed_ingress_with_source_security_group_id = [
309332 {
310333 rule = " http-80-tcp"
311- source_security_group_id = module.alb_sg .security_group_id
334+ source_security_group_id = module.alb .security_group_id
312335 }
313336 ]
314337 number_of_computed_ingress_with_source_security_group_id = 1
0 commit comments