Skip to content

Commit 8746b10

Browse files
chore: Update examples to use latest ALB module (#130)
* chore: Update examples to use latest ALB module * fix: Convert key hypens to underscores * Update examples/fargate/main.tf Co-authored-by: Anton Babenko <anton@antonbabenko.com> --------- Co-authored-by: Anton Babenko <anton@antonbabenko.com>
1 parent 38dab0a commit 8746b10

File tree

8 files changed

+195
-133
lines changed

8 files changed

+195
-133
lines changed

examples/complete/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ Note that this example may create resources which will incur monetary charges on
3939

4040
| Name | Source | Version |
4141
|------|--------|---------|
42-
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 8.0 |
43-
| <a name="module_alb_sg"></a> [alb\_sg](#module\_alb\_sg) | terraform-aws-modules/security-group/aws | ~> 5.0 |
42+
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 9.0 |
4443
| <a name="module_ecs"></a> [ecs](#module\_ecs) | ../../ | n/a |
4544
| <a name="module_ecs_cluster_disabled"></a> [ecs\_cluster\_disabled](#module\_ecs\_cluster\_disabled) | ../../modules/cluster | n/a |
4645
| <a name="module_ecs_disabled"></a> [ecs\_disabled](#module\_ecs\_disabled) | ../../ | n/a |

examples/complete/main.tf

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module "ecs" {
114114

115115
load_balancer = {
116116
service = {
117-
target_group_arn = element(module.alb.target_group_arns, 0)
117+
target_group_arn = module.alb.target_groups["ex_ecs"].arn
118118
container_name = local.container_name
119119
container_port = local.container_port
120120
}
@@ -140,7 +140,7 @@ module "ecs" {
140140
to_port = local.container_port
141141
protocol = "tcp"
142142
description = "Service port"
143-
source_security_group_id = module.alb_sg.security_group_id
143+
source_security_group_id = module.alb.security_group_id
144144
}
145145
egress_all = {
146146
type = "egress"
@@ -188,51 +188,72 @@ resource "aws_service_discovery_http_namespace" "this" {
188188
tags = local.tags
189189
}
190190

191-
module "alb_sg" {
192-
source = "terraform-aws-modules/security-group/aws"
193-
version = "~> 5.0"
191+
module "alb" {
192+
source = "terraform-aws-modules/alb/aws"
193+
version = "~> 9.0"
194194

195-
name = "${local.name}-service"
196-
description = "Service security group"
197-
vpc_id = module.vpc.vpc_id
195+
name = local.name
198196

199-
ingress_rules = ["http-80-tcp"]
200-
ingress_cidr_blocks = ["0.0.0.0/0"]
197+
load_balancer_type = "application"
201198

202-
egress_rules = ["all-all"]
203-
egress_cidr_blocks = module.vpc.private_subnets_cidr_blocks
199+
vpc_id = module.vpc.vpc_id
200+
subnets = module.vpc.public_subnets
204201

205-
tags = local.tags
206-
}
202+
# For example only
203+
enable_deletion_protection = false
207204

208-
module "alb" {
209-
source = "terraform-aws-modules/alb/aws"
210-
version = "~> 8.0"
205+
# Security Group
206+
security_group_ingress_rules = {
207+
all_http = {
208+
from_port = 80
209+
to_port = 80
210+
ip_protocol = "tcp"
211+
cidr_ipv4 = "0.0.0.0/0"
212+
}
213+
}
214+
security_group_egress_rules = {
215+
all = {
216+
ip_protocol = "-1"
217+
cidr_ipv4 = module.vpc.vpc_cidr_block
218+
}
219+
}
211220

212-
name = local.name
221+
listeners = {
222+
ex_http = {
223+
port = 80
224+
protocol = "HTTP"
213225

214-
load_balancer_type = "application"
226+
forward = {
227+
target_group_key = "ex_ecs"
228+
}
229+
}
230+
}
231+
232+
target_groups = {
233+
ex_ecs = {
234+
backend_protocol = "HTTP"
235+
backend_port = local.container_port
236+
target_type = "ip"
237+
deregistration_delay = 5
238+
load_balancing_cross_zone_enabled = true
239+
240+
health_check = {
241+
enabled = true
242+
healthy_threshold = 5
243+
interval = 30
244+
matcher = "200"
245+
path = "/"
246+
port = "traffic-port"
247+
protocol = "HTTP"
248+
timeout = 5
249+
unhealthy_threshold = 2
250+
}
215251

216-
vpc_id = module.vpc.vpc_id
217-
subnets = module.vpc.public_subnets
218-
security_groups = [module.alb_sg.security_group_id]
219-
220-
http_tcp_listeners = [
221-
{
222-
port = 80
223-
protocol = "HTTP"
224-
target_group_index = 0
225-
},
226-
]
227-
228-
target_groups = [
229-
{
230-
name = "${local.name}-${local.container_name}"
231-
backend_protocol = "HTTP"
232-
backend_port = local.container_port
233-
target_type = "ip"
234-
},
235-
]
252+
# Theres nothing to attach here in this definition. Instead,
253+
# ECS will attach the IPs of the tasks to this target group
254+
create_attachment = false
255+
}
256+
}
236257

237258
tags = local.tags
238259
}

examples/ec2-autoscaling/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ Note that this example may create resources which will incur monetary charges on
3939

4040
| Name | Source | Version |
4141
|------|--------|---------|
42-
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 8.0 |
43-
| <a name="module_alb_sg"></a> [alb\_sg](#module\_alb\_sg) | terraform-aws-modules/security-group/aws | ~> 5.0 |
42+
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 9.0 |
4443
| <a name="module_autoscaling"></a> [autoscaling](#module\_autoscaling) | terraform-aws-modules/autoscaling/aws | ~> 6.5 |
4544
| <a name="module_autoscaling_sg"></a> [autoscaling\_sg](#module\_autoscaling\_sg) | terraform-aws-modules/security-group/aws | ~> 5.0 |
4645
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | ../../modules/cluster | n/a |

examples/ec2-autoscaling/main.tf

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
175158
module "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

examples/fargate/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ Note that this example may create resources which will incur monetary charges on
3939

4040
| Name | Source | Version |
4141
|------|--------|---------|
42-
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 8.0 |
43-
| <a name="module_alb_sg"></a> [alb\_sg](#module\_alb\_sg) | terraform-aws-modules/security-group/aws | ~> 5.0 |
42+
| <a name="module_alb"></a> [alb](#module\_alb) | terraform-aws-modules/alb/aws | ~> 9.0 |
4443
| <a name="module_ecs_cluster"></a> [ecs\_cluster](#module\_ecs\_cluster) | ../../modules/cluster | n/a |
4544
| <a name="module_ecs_service"></a> [ecs\_service](#module\_ecs\_service) | ../../modules/service | n/a |
4645
| <a name="module_vpc"></a> [vpc](#module\_vpc) | terraform-aws-modules/vpc/aws | ~> 5.0 |

0 commit comments

Comments
 (0)