11locals {
2- container_name = " ${ var . app_name } "
2+ container_name = var. app_name
33}
44
55# Try to fetch secrets manager secret, but don't fail if it doesn't exist
@@ -29,25 +29,25 @@ locals {
2929 password = " changeme"
3030 }
3131 )
32-
32+
3333 # Provide default database endpoints with try() for safe access
3434 db_endpoint = try (
3535 data. aws_rds_cluster . rds_cluster [0 ]. endpoint ,
3636 " localhost"
3737 )
38-
38+
3939 db_reader_endpoint = try (
4040 data. aws_rds_cluster . rds_cluster [0 ]. reader_endpoint ,
4141 " localhost"
4242 )
43-
43+
4444 # Flag to indicate if database resources are available
4545 db_resources_available = var. db_cluster_name != " " && length (data. aws_rds_cluster . rds_cluster ) > 0
4646}
4747
4848
4949resource "aws_ecs_cluster" "ecs_cluster" {
50- name = " ${ var . app_name } "
50+ name = var. app_name
5151 tags = module. common . common_tags
5252}
5353
@@ -65,7 +65,7 @@ resource "aws_ecs_cluster_capacity_providers" "ecs_cluster_capacity_providers" {
6565
6666resource "terraform_data" "trigger_flyway" {
6767 count = var. db_cluster_name != " " ? 1 : 0
68- input = " ${ timestamp ()} "
68+ input = timestamp ()
6969}
7070
7171resource "aws_ecs_task_definition" "flyway_task" {
@@ -78,7 +78,7 @@ resource "aws_ecs_task_definition" "flyway_task" {
7878 execution_role_arn = aws_iam_role. ecs_task_execution_role . arn
7979 task_role_arn = aws_iam_role. app_container_role . arn
8080 container_definitions = jsonencode ([
81- {
81+ {
8282 name = " ${ var . app_name } -flyway"
8383 image = " ${ var . flyway_image } "
8484 essential = true
@@ -108,7 +108,7 @@ resource "aws_ecs_task_definition" "flyway_task" {
108108 value = " true"
109109 }
110110 ]
111-
111+
112112 logConfiguration = {
113113 logDriver = " awslogs"
114114 options = {
@@ -120,15 +120,15 @@ resource "aws_ecs_task_definition" "flyway_task" {
120120 }
121121 mountPoints = []
122122 volumesFrom = []
123-
123+
124124 }
125125 ])
126126 lifecycle {
127127 replace_triggered_by = [terraform_data . trigger_flyway [0 ]]
128128 }
129129 provisioner "local-exec" {
130130 interpreter = [" bash" , " -c" ]
131- command = <<- EOF
131+ command = <<- EOF
132132 set -euo pipefail
133133
134134 max_attempts=5
@@ -194,15 +194,15 @@ resource "aws_ecs_task_definition" "flyway_task" {
194194}
195195
196196resource "aws_ecs_task_definition" "node_api_task" {
197- family = " ${ var . app_name } "
197+ family = var. app_name
198198 network_mode = " awsvpc"
199199 requires_compatibilities = [" FARGATE" ]
200200 cpu = var. api_cpu
201201 memory = var. api_memory
202202 execution_role_arn = aws_iam_role. ecs_task_execution_role . arn
203203 task_role_arn = aws_iam_role. app_container_role . arn
204204 container_definitions = jsonencode ([
205- {
205+ {
206206 name = " ${ local . container_name } "
207207 image = " ${ var . api_image } "
208208 essential = true
@@ -232,7 +232,7 @@ resource "aws_ecs_task_definition" "node_api_task" {
232232 value = " ${ var . db_schema } "
233233 },
234234 {
235- name = " POSTGRES_POOL_SIZE"
235+ name = " POSTGRES_POOL_SIZE"
236236 value = " ${ var . postgres_pool_size } "
237237 },
238238 {
@@ -268,10 +268,10 @@ resource "aws_ecs_task_definition" "node_api_task" {
268268
269269
270270resource "aws_ecs_service" "node_api_service" {
271- name = " ${ var . app_name } "
272- cluster = aws_ecs_cluster. ecs_cluster . id
273- task_definition = aws_ecs_task_definition. node_api_task . arn
274- desired_count = 1
271+ name = var. app_name
272+ cluster = aws_ecs_cluster. ecs_cluster . id
273+ task_definition = aws_ecs_task_definition. node_api_task . arn
274+ desired_count = 1
275275 health_check_grace_period_seconds = 60
276276
277277 capacity_provider_strategy {
@@ -292,10 +292,10 @@ resource "aws_ecs_service" "node_api_service" {
292292
293293 load_balancer {
294294 target_group_arn = aws_alb_target_group. app . id
295- container_name = " ${ local . container_name } "
295+ container_name = local. container_name
296296 container_port = var. app_port
297297 }
298298 wait_for_steady_state = true
299- depends_on = [aws_iam_role_policy_attachment . ecs_task_execution_role ]
300- tags = module. common . common_tags
299+ depends_on = [aws_iam_role_policy_attachment . ecs_task_execution_role ]
300+ tags = module. common . common_tags
301301}
0 commit comments