Skip to content

Commit dd72035

Browse files
committed
refactor: accept user specified elb service account arn otherwise fallback to data source
Reformat and cleanup code.
1 parent 875fe16 commit dd72035

File tree

2 files changed

+76
-38
lines changed

2 files changed

+76
-38
lines changed

main.tf

+59-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
locals {
2+
# ECS Service
3+
ecs_service_launch_type = "EC2"
4+
5+
# ECS Task Definition
6+
ecs_task_definition_requires_compatibilities = ["EC2"]
7+
28
# ALB
39
alb_access_logs_default_s3_configuration = var.create_s3_bucket_for_alb_logging ? {
410
bucket = module.s3_bucket[0].bucket_id
@@ -10,6 +16,7 @@ locals {
1016
enabled = true
1117
prefix = var.s3_bucket_connection_logs_prefix
1218
} : null
19+
1320
alb_target_groups = {
1421
for k, v in try(var.load_balancer.target_groups, {}) :
1522
k => merge(
@@ -32,6 +39,10 @@ locals {
3239
v
3340
)
3441
}
42+
43+
# S3
44+
create_elb_service_account_data_source = var.s3_elb_service_account_arn == null
45+
elb_service_account_arn = local.create_elb_service_account_data_source ? data.aws_elb_service_account.this[0].arn : var.s3_elb_service_account_arn
3546
}
3647

3748
################################################################################
@@ -50,7 +61,7 @@ resource "aws_ecs_service" "this" {
5061
force_new_deployment = try(var.service.force_new_deployment, null)
5162
health_check_grace_period_seconds = try(var.service.health_check_grace_period_seconds, null)
5263
iam_role = try(var.service.iam_role, null)
53-
launch_type = "EC2"
64+
launch_type = local.ecs_service_launch_type
5465
propagate_tags = try(var.service.propagate_tags, null)
5566
scheduling_strategy = try(var.service.scheduling_strategy, null)
5667
task_definition = aws_ecs_task_definition.this.id
@@ -187,7 +198,7 @@ resource "aws_ecs_task_definition" "this" {
187198
memory = try(var.task_definition.memory, null)
188199
network_mode = try(var.task_definition.network_mode, null)
189200
pid_mode = try(var.task_definition.pid_mode, null)
190-
requires_compatibilities = ["EC2"]
201+
requires_compatibilities = local.ecs_task_definition_requires_compatibilities
191202
skip_destroy = try(var.task_definition.skip_destroy, null)
192203
task_role_arn = try(var.task_definition.task_role_arn, null)
193204
track_latest = try(var.task_definition.track_latest, null)
@@ -228,19 +239,26 @@ resource "aws_ecs_task_definition" "this" {
228239
}
229240

230241
################################################################################
231-
# Capacity Provider Sub-module
242+
# Amazon Certificates Manager Sub-module
232243
################################################################################
233244

234-
module "capacity_provider" {
235-
source = "./modules/capacity-provider"
245+
module "acm" {
246+
source = "./modules/acm"
236247

237-
count = var.create_capacity_provider ? 1 : 0
248+
for_each = var.create_acm ? var.acm_certificates : {}
238249

239-
ecs_cluster_name = var.cluster_name
240-
default_auto_scaling_group_arn = var.capacity_provider_default_auto_scaling_group_arn
250+
# ACM Certificate
251+
certificate_domain_name = each.value.domain_name
252+
certificate_subject_alternative_names = try(each.value.subject_alternative_names, null)
253+
certificate_validation_method = try(each.value.validation_method, null)
254+
certificate_key_algorithm = try(each.value.key_algorithm, null)
255+
certificate_validation_option = try(each.value.validation_option, null)
241256

242-
capacity_providers = var.capacity_providers
243-
default_capacity_provider_strategies = var.default_capacity_providers_strategies
257+
# Route53 Record
258+
record_zone_id = try(each.value.record_zone_id, null)
259+
record_allow_overwrite = try(each.value.record_allow_overwrite, null)
260+
261+
tags = try(each.value.tags, {})
244262
}
245263

246264
################################################################################
@@ -252,41 +270,67 @@ module "alb" {
252270

253271
count = var.create_alb ? 1 : 0
254272

273+
# Load Balancer
255274
name = try(var.load_balancer.name, null)
256275
internal = try(var.load_balancer.internal, null)
257276
subnets_ids = try(var.load_balancer.subnets_ids, [])
258277
security_groups_ids = try(var.load_balancer.security_groups_ids, [])
259278
preserve_host_header = try(var.load_balancer.preserve_host_header, null)
260279
enable_deletion_protection = try(var.load_balancer.enable_deletion_protection, null)
280+
access_logs = var.load_balancer.access_logs != null ? var.load_balancer.access_logs : local.alb_access_logs_default_s3_configuration
281+
connection_logs = var.load_balancer.connection_logs != null ? var.load_balancer.connection_logs : local.alb_connection_logs_default_s3_configuration
261282

262-
access_logs = var.load_balancer.access_logs != null ? var.load_balancer.access_logs : local.alb_access_logs_default_s3_configuration
263-
connection_logs = var.load_balancer.connection_logs != null ? var.load_balancer.connection_logs : local.alb_connection_logs_default_s3_configuration
264-
283+
# LB Target Group
265284
target_groups = local.alb_target_groups
266285

286+
# LB Listener
267287
listeners = local.alb_listeners
268288

289+
# LB Listener Rule
269290
listener_rules = try(var.load_balancer.listener_rules, {})
270291

271292
tags = try(var.load_balancer.tags, {})
272293

273294
depends_on = [module.acm]
274295
}
275296

297+
################################################################################
298+
# Capacity Provider Sub-module
299+
################################################################################
300+
301+
module "capacity_provider" {
302+
source = "./modules/capacity-provider"
303+
304+
count = var.create_capacity_provider ? 1 : 0
305+
306+
ecs_cluster_name = var.cluster_name
307+
308+
# ECS Capacity Provider
309+
capacity_providers = var.capacity_providers
310+
default_auto_scaling_group_arn = var.capacity_provider_default_auto_scaling_group_arn
311+
312+
# ECS Cluster Capacity Providers
313+
default_capacity_provider_strategies = var.default_capacity_providers_strategies
314+
}
315+
276316
################################################################################
277317
# S3 Bucket Sub-module
278318
################################################################################
279319

280-
data "aws_elb_service_account" "this" {}
320+
data "aws_elb_service_account" "this" {
321+
count = local.create_elb_service_account_data_source ? 1 : 0
322+
}
281323

282324
module "s3_bucket" {
283325
source = "./modules/s3-bucket"
284326

285327
count = var.create_s3_bucket_for_alb_logging ? 1 : 0
286328

329+
# S3 Bucket
287330
bucket = var.s3_bucket_name
288331
bucket_force_destroy = var.s3_bucket_force_destroy
289332

333+
# S3 Bucket Policy
290334
bucket_policies = {
291335
alb-logs = {
292336
id = "${var.s3_bucket_policy_id_prefix}-logs"
@@ -306,7 +350,7 @@ module "s3_bucket" {
306350
principals = [
307351
{
308352
identifiers = [
309-
data.aws_elb_service_account.this.arn
353+
local.elb_service_account_arn
310354
]
311355
type = "AWS"
312356
}
@@ -354,24 +398,3 @@ module "s3_bucket" {
354398

355399
tags = var.s3_bucket_tags
356400
}
357-
358-
################################################################################
359-
# Amazon Certificates Manager Sub-module
360-
################################################################################
361-
362-
module "acm" {
363-
source = "./modules/acm"
364-
365-
for_each = var.create_acm ? var.acm_certificates : {}
366-
367-
certificate_domain_name = each.value.domain_name
368-
certificate_subject_alternative_names = try(each.value.subject_alternative_names, null)
369-
certificate_validation_method = try(each.value.validation_method, null)
370-
certificate_key_algorithm = try(each.value.key_algorithm, null)
371-
certificate_validation_option = try(each.value.validation_option, null)
372-
373-
record_zone_id = try(each.value.record_zone_id, null)
374-
record_allow_overwrite = try(each.value.record_allow_overwrite, null)
375-
376-
tags = try(each.value.tags, {})
377-
}

variables.tf

+17-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ variable "service" {
3636
service_registries = optional(any, null)
3737
tags = optional(map(string), {})
3838
})
39+
nullable = false
3940
}
4041

4142
################################################################################
@@ -60,6 +61,7 @@ variable "task_definition" {
6061
volume = optional(any, null)
6162
tags = optional(map(string), {})
6263
})
64+
nullable = false
6365
}
6466

6567
################################################################################
@@ -69,6 +71,7 @@ variable "task_definition" {
6971
variable "create_capacity_provider" {
7072
description = "Creates a new Capacity Provider to use with the Autoscaling Group."
7173
type = bool
74+
nullable = false
7275
default = true
7376
}
7477

@@ -81,12 +84,14 @@ variable "capacity_provider_default_auto_scaling_group_arn" {
8184
variable "capacity_providers" {
8285
description = "Capacity Providers to associate with the ECS Cluster."
8386
type = any
87+
nullable = false
8488
default = {}
8589
}
8690

8791
variable "default_capacity_providers_strategies" {
8892
description = "(Optional) Set of capacity provider strategies to use by default for the cluster."
8993
type = any
94+
nullable = false
9095
default = []
9196
}
9297

@@ -97,6 +102,7 @@ variable "default_capacity_providers_strategies" {
97102
variable "create_alb" {
98103
description = "Creates a new Application Load Balancer to use with the ECS Service."
99104
type = bool
105+
nullable = false
100106
default = true
101107
}
102108

@@ -116,7 +122,8 @@ variable "load_balancer" {
116122
listener_rules = optional(any, {})
117123
tags = optional(map(string), {})
118124
})
119-
default = {}
125+
nullable = false
126+
default = {}
120127
}
121128

122129
################################################################################
@@ -150,6 +157,12 @@ variable "s3_bucket_policy_id_prefix" {
150157
default = "ecs-deployment-alb-"
151158
}
152159

160+
variable "s3_elb_service_account_arn" {
161+
description = "(Optional, Default:null) ARN of the ELB Service Account."
162+
type = string
163+
default = null
164+
}
165+
153166
variable "s3_bucket_access_logs_prefix" {
154167
description = "(Optional) - Prefix for storing ALB access logs in the S3 bucket."
155168
type = string
@@ -178,6 +191,7 @@ variable "s3_bucket_tags" {
178191
variable "create_acm" {
179192
description = "Creates the ACM certificates to use with the Load Balancer."
180193
type = bool
194+
nullable = false
181195
default = false
182196
}
183197

@@ -196,5 +210,6 @@ variable "acm_certificates" {
196210
record_zone_id = string
197211
record_allow_overwrite = optional(bool, true)
198212
}))
199-
default = {}
213+
nullable = false
214+
default = {}
200215
}

0 commit comments

Comments
 (0)