@@ -7,30 +7,30 @@ data "aws_region" "current" {}
77# Cloudwatch
88# ------------------------------------------------------------------------------
99resource "aws_cloudwatch_log_group" "main" {
10- name = " ${ var . name_prefix } "
11- retention_in_days = " ${ var . log_retention_in_days } "
12- tags = " ${ var . tags } "
10+ name = var. name_prefix
11+ retention_in_days = var. log_retention_in_days
12+ tags = var. tags
1313}
1414
1515# ------------------------------------------------------------------------------
1616# IAM - Task execution role, needed to pull ECR images etc.
1717# ------------------------------------------------------------------------------
1818resource "aws_iam_role" "execution" {
1919 name = " ${ var . name_prefix } -task-execution-role"
20- assume_role_policy = " ${ data . aws_iam_policy_document . task_assume . json } "
20+ assume_role_policy = data. aws_iam_policy_document . task_assume . json
2121}
2222
2323resource "aws_iam_role_policy" "task_execution" {
2424 name = " ${ var . name_prefix } -task-execution"
25- role = " ${ aws_iam_role . execution . id } "
26- policy = " ${ data . aws_iam_policy_document . task_execution_permissions . json } "
25+ role = aws_iam_role. execution . id
26+ policy = data. aws_iam_policy_document . task_execution_permissions . json
2727}
2828
2929resource "aws_iam_role_policy" "read_repository_credentials" {
30- count = " ${ length (var. repository_credentials ) != 0 ? 1 : 0 } "
30+ count = length (var. repository_credentials ) != 0 ? 1 : 0
3131 name = " ${ var . name_prefix } -read-repository-credentials"
32- role = " ${ aws_iam_role . execution . id } "
33- policy = " ${ data . aws_iam_policy_document . read_repository_credentials . json } "
32+ role = aws_iam_role. execution . id
33+ policy = data. aws_iam_policy_document . read_repository_credentials . json
3434}
3535
3636# ------------------------------------------------------------------------------
@@ -39,27 +39,32 @@ resource "aws_iam_role_policy" "read_repository_credentials" {
3939# ------------------------------------------------------------------------------
4040resource "aws_iam_role" "task" {
4141 name = " ${ var . name_prefix } -task-role"
42- assume_role_policy = " ${ data . aws_iam_policy_document . task_assume . json } "
42+ assume_role_policy = data. aws_iam_policy_document . task_assume . json
4343}
4444
4545resource "aws_iam_role_policy" "log_agent" {
4646 name = " ${ var . name_prefix } -log-permissions"
47- role = " ${ aws_iam_role . task . id } "
48- policy = " ${ data . aws_iam_policy_document . task_permissions . json } "
47+ role = aws_iam_role. task . id
48+ policy = data. aws_iam_policy_document . task_permissions . json
4949}
5050
5151# ------------------------------------------------------------------------------
5252# Security groups
5353# ------------------------------------------------------------------------------
5454resource "aws_security_group" "ecs_service" {
55- vpc_id = " ${ var . vpc_id } "
55+ vpc_id = var. vpc_id
5656 name = " ${ var . name_prefix } -ecs-service-sg"
5757 description = " Fargate service security group"
58- tags = " ${ merge (var. tags , map (" Name" , " ${ var . name_prefix } -sg" ))} "
58+ tags = merge (
59+ var. tags ,
60+ {
61+ Name = " ${ var . name_prefix } -sg"
62+ },
63+ )
5964}
6065
6166resource "aws_security_group_rule" "egress_service" {
62- security_group_id = " ${ aws_security_group . ecs_service . id } "
67+ security_group_id = aws_security_group. ecs_service . id
6368 type = " egress"
6469 protocol = " -1"
6570 from_port = 0
@@ -72,11 +77,24 @@ resource "aws_security_group_rule" "egress_service" {
7277# LB Target group
7378# ------------------------------------------------------------------------------
7479resource "aws_lb_target_group" "task" {
75- vpc_id = " ${ var . vpc_id } "
76- protocol = " ${ var . task_container_protocol } "
77- port = " ${ var . task_container_port } "
78- target_type = " ip"
79- health_check = [" ${ var . health_check } " ]
80+ vpc_id = var. vpc_id
81+ protocol = var. task_container_protocol
82+ port = var. task_container_port
83+ target_type = " ip"
84+ dynamic "health_check" {
85+ for_each = [var . health_check ]
86+ content {
87+ enabled = lookup (health_check. value , " enabled" , null )
88+ healthy_threshold = lookup (health_check. value , " healthy_threshold" , null )
89+ interval = lookup (health_check. value , " interval" , null )
90+ matcher = lookup (health_check. value , " matcher" , null )
91+ path = lookup (health_check. value , " path" , null )
92+ port = lookup (health_check. value , " port" , null )
93+ protocol = lookup (health_check. value , " protocol" , null )
94+ timeout = lookup (health_check. value , " timeout" , null )
95+ unhealthy_threshold = lookup (health_check. value , " unhealthy_threshold" , null )
96+ }
97+ }
8098
8199 # NOTE: TF is unable to destroy a target group while a listener is attached,
82100 # therefor we have to create a new one before destroying the old. This also means
@@ -85,29 +103,34 @@ resource "aws_lb_target_group" "task" {
85103 create_before_destroy = true
86104 }
87105
88- tags = " ${ merge (var. tags , map (" Name" , " ${ var . name_prefix } -target-${ var . task_container_port } " ))} "
106+ tags = merge (
107+ var. tags ,
108+ {
109+ Name = " ${ var . name_prefix } -target-${ var . task_container_port } "
110+ },
111+ )
89112}
90113
91114# ------------------------------------------------------------------------------
92115# ECS Task/Service
93116# ------------------------------------------------------------------------------
94117data "null_data_source" "task_environment" {
95- count = " ${ var . task_container_environment_count } "
118+ count = var. task_container_environment_count
96119
97120 inputs = {
98- name = " ${ element (keys (var. task_container_environment ), count. index )} "
99- value = " ${ element (values (var. task_container_environment ), count. index )} "
121+ name = element (keys (var. task_container_environment ), count. index )
122+ value = element (values (var. task_container_environment ), count. index )
100123 }
101124}
102125
103126resource "aws_ecs_task_definition" "task" {
104- family = " ${ var . name_prefix } "
105- execution_role_arn = " ${ aws_iam_role . execution . arn } "
127+ family = var. name_prefix
128+ execution_role_arn = aws_iam_role. execution . arn
106129 network_mode = " awsvpc"
107130 requires_compatibilities = [" FARGATE" ]
108- cpu = " ${ var . task_definition_cpu } "
109- memory = " ${ var . task_definition_memory } "
110- task_role_arn = " ${ aws_iam_role . task . arn } "
131+ cpu = var. task_definition_cpu
132+ memory = var. task_definition_memory
133+ task_role_arn = aws_iam_role. task . arn
111134
112135 container_definitions = << EOF
113136[{
@@ -134,34 +157,35 @@ resource "aws_ecs_task_definition" "task" {
134157 "environment": ${ jsonencode (data. null_data_source . task_environment . * . outputs )}
135158}]
136159EOF
160+
137161}
138162
139163resource "aws_ecs_service" "service" {
140- depends_on = [" null_resource.lb_exists" ]
141- name = " ${ var . name_prefix } "
142- cluster = " ${ var . cluster_id } "
143- task_definition = " ${ aws_ecs_task_definition . task . arn } "
144- desired_count = " ${ var . desired_count } "
164+ depends_on = [null_resource. lb_exists ]
165+ name = var. name_prefix
166+ cluster = var. cluster_id
167+ task_definition = aws_ecs_task_definition. task . arn
168+ desired_count = var. desired_count
145169 launch_type = " FARGATE"
146- deployment_minimum_healthy_percent = " ${ var . deployment_minimum_healthy_percent } "
147- deployment_maximum_percent = " ${ var . deployment_maximum_percent } "
148- health_check_grace_period_seconds = " ${ var . health_check_grace_period_seconds } "
170+ deployment_minimum_healthy_percent = var. deployment_minimum_healthy_percent
171+ deployment_maximum_percent = var. deployment_maximum_percent
172+ health_check_grace_period_seconds = var. health_check_grace_period_seconds
149173
150174 network_configuration {
151- subnets = [ " ${ var . private_subnet_ids } " ]
152- security_groups = [" ${ aws_security_group . ecs_service . id } " ]
153- assign_public_ip = " ${ var . task_container_assign_public_ip } "
175+ subnets = var. private_subnet_ids
176+ security_groups = [aws_security_group . ecs_service . id ]
177+ assign_public_ip = var. task_container_assign_public_ip
154178 }
155179
156180 load_balancer {
157- container_name = " ${ var . container_name != " " ? var . container_name : var . name_prefix } "
158- container_port = " ${ var . task_container_port } "
159- target_group_arn = " ${ aws_lb_target_group . task . arn } "
181+ container_name = var. container_name != " " ? var. container_name : var. name_prefix
182+ container_port = var. task_container_port
183+ target_group_arn = aws_lb_target_group. task . arn
160184 }
161185
162186 deployment_controller {
163187 # The deployment controller type to use. Valid values: CODE_DEPLOY, ECS.
164- type = " ${ var . deployment_controller_type } "
188+ type = var. deployment_controller_type
165189 }
166190}
167191
@@ -170,7 +194,8 @@ resource "aws_ecs_service" "service" {
170194# see https://github.com/hashicorp/terraform/issues/12634.
171195# Service depends on this resources which prevents it from being created until the LB is ready
172196resource "null_resource" "lb_exists" {
173- triggers {
174- alb_name = " ${ var . lb_arn } "
197+ triggers = {
198+ alb_name = var.lb_arn
175199 }
176200}
201+
0 commit comments