@@ -3,6 +3,7 @@ data "aws_partition" "current" {}
33data "aws_caller_identity" "current" {}
44
55# ECR Repositories
6+
67resource "aws_ecr_repository" "fhir_api" {
78 name = " ${ var . account_name } -fhir-api"
89}
@@ -11,10 +12,22 @@ resource "aws_ecr_repository" "fhir_api_migrations" {
1112 name = " ${ var . account_name } -fhir-api-migrations"
1213}
1314
15+ # Log Groups
16+
17+ resource "aws_cloudwatch_log_group" "fhir_api_log_group" {
18+ name = " /ecs/${ var . account_name } -fhir-api-logs"
19+ retention_in_days = 30
20+ }
21+
22+ resource "aws_cloudwatch_log_group" "fhir_api_migrations_log_group" {
23+ name = " /ecs/${ var . account_name } -fhir-api-migrations-logs"
24+ retention_in_days = 30
25+ }
26+
1427# ECS Roles and Policies
15- resource "aws_iam_role" "fhir_api_role " {
16- name = " ${ var . account_name } -fhir-api-role"
17- description = " Defines what AWS actions the FHIR API task is allowed to make"
28+ resource "aws_iam_role" "fhir_api_execution_role " {
29+ name = " ${ var . account_name } -fhir-api-execution- role"
30+ description = " Defines what AWS actions the FHIR API task execution environment is allowed to make"
1831 assume_role_policy = jsonencode ({
1932 Version = " 2012-10-17"
2033 Statement = [{
@@ -25,6 +38,11 @@ resource "aws_iam_role" "fhir_api_role" {
2538 })
2639}
2740
41+ resource "aws_iam_role_policy_attachment" "ecs_task_execution" {
42+ role = aws_iam_role. fhir_api_execution_role . name
43+ policy_arn = " arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
44+ }
45+
2846resource "aws_iam_policy" "fhir_api_can_access_fhir_api_db_secret" {
2947 name = " ${ var . account_name } -fhir-api-can-access-fhir-database-secret"
3048 description = " Allows ECS to access the RDS secret"
@@ -36,22 +54,21 @@ resource "aws_iam_policy" "fhir_api_can_access_fhir_api_db_secret" {
3654 Effect = " Allow"
3755 Resource = [
3856 var.db.db_instance_master_user_secret_arn,
39- aws_secretsmanager_secret.django_secret .arn
57+ aws_secretsmanager_secret_version.django_secret_version .arn
4058 ]
4159 }
4260 ]
4361 })
4462}
4563
4664resource "aws_iam_role_policy_attachment" "fhir_api_can_access_database_secret_attachment" {
47- role = aws_iam_role. fhir_api_role . name
65+ role = aws_iam_role. fhir_api_execution_role . name
4866 policy_arn = aws_iam_policy. fhir_api_can_access_fhir_api_db_secret . arn
4967}
5068
5169resource "aws_iam_policy" "fhir_api_logs_policy" {
5270 name = " ${ var . account_name } -fhir-api-can-log-to-cloudwatch"
5371 description = " Allow ECS tasks to write logs to CloudWatch"
54- path = " /delegatedadmin/developer/"
5572
5673 policy = jsonencode ({
5774 Version = " 2012-10-17"
@@ -61,16 +78,18 @@ resource "aws_iam_policy" "fhir_api_logs_policy" {
6178 " logs:CreateLogStream" ,
6279 " logs:PutLogEvents"
6380 ]
64- Effect = " Allow"
65- Resource = " arn:${ data . aws_partition . current . partition } :logs:*:${ data . aws_caller_identity . current . account_id } :log-group:/ecs/${ var . account_name } *:*"
81+ Effect = " Allow"
82+ Resource = [
83+ " arn:${ data . aws_partition . current . partition } :logs:*:${ data . aws_caller_identity . current . account_id } :log-group:/ecs/${ var . account_name } *:*"
84+ ]
6685 },
6786 ]
6887 })
6988}
7089
7190resource "aws_iam_role_policy_attachment" "fhir_api_can_create_cloudwatch_logs" {
7291 policy_arn = aws_iam_policy. fhir_api_logs_policy . id
73- role = aws_iam_role. fhir_api_role . id
92+ role = aws_iam_role. fhir_api_execution_role . id
7493}
7594
7695# FHIR API Secrets
@@ -96,7 +115,7 @@ resource "aws_ecs_task_definition" "app" {
96115 network_mode = " awsvpc"
97116 cpu = " 512"
98117 memory = " 1024"
99- execution_role_arn = aws_iam_role. fhir_api_role . arn
118+ execution_role_arn = aws_iam_role. fhir_api_execution_role . arn
100119
101120 container_definitions = jsonencode ([
102121 # In the past, I've put the migration container in a separate task and invoked it manually to avoid the case
@@ -113,7 +132,7 @@ resource "aws_ecs_task_definition" "app" {
113132 environment = [
114133 {
115134 name = " FLYWAY_URL"
116- value = " jdbc:postgresql://${ var . db . db_instance_address } :${ var . db . db_instance_port } /${ var . app_db_name } "
135+ value = " jdbc:postgresql://${ var . db . db_instance_address } :${ var . db . db_instance_port } /${ var . db . db_instance_name } "
117136 }
118137 ],
119138 secrets = [
@@ -129,9 +148,9 @@ resource "aws_ecs_task_definition" "app" {
129148 logConfiguration = {
130149 logDriver = " awslogs"
131150 options = {
132- " awslogs-group" = " /ecs/ ${ var . account_name } -fhir-api-migration-logs "
133- " awslogs-region" = data.aws_region.current.name
134- " awslogs-stream-prefix" = " ${ var . account_name } -fhir-api-migration-logs "
151+ " awslogs-group" = aws_cloudwatch_log_group.fhir_api_migrations_log_group.name
152+ " awslogs-region" = " us-east-1 "
153+ " awslogs-stream-prefix" = var.account_name
135154 }
136155 }
137156 },
@@ -142,7 +161,7 @@ resource "aws_ecs_task_definition" "app" {
142161 environment = [
143162 {
144163 name = " NPD_DB_NAME"
145- value = var.app_db_name
164+ value = var.db.db_instance_name
146165 },
147166 {
148167 name = " NPD_DB_HOST"
@@ -170,7 +189,7 @@ resource "aws_ecs_task_definition" "app" {
170189 },
171190 {
172191 name = " NPD_PROJECT_NAME"
173- value = " ndh "
192+ value = " npd "
174193 },
175194 {
176195 name = " CACHE_LOCATION" ,
@@ -191,13 +210,15 @@ resource "aws_ecs_task_definition" "app" {
191210 valueFrom = " ${ var . db . db_instance_master_user_secret_arn } :password::"
192211 },
193212 ]
194- portMappings = [{ containerPort = var.fhir_api_port }]
213+ portMappings = [{
214+ containerPort = var.fhir_api_port
215+ }]
195216 logConfiguration = {
196217 logDriver = " awslogs"
197218 options = {
198- " awslogs-group" = " /ecs/ ${ var . account_name } -fhir-api-logs "
199- " awslogs-region" = data.aws_region.current.name
200- " awslogs-stream-prefix" = " ${ var . account_name } -fhir-api-logs "
219+ " awslogs-group" = aws_cloudwatch_log_group.fhir_api_log_group.name
220+ " awslogs-region" = " us-east-1 "
221+ " awslogs-stream-prefix" = var.account_name
201222 }
202223 }
203224 # TODO: Implement for your app
@@ -227,7 +248,7 @@ resource "aws_ecs_service" "app" {
227248 }
228249
229250 load_balancer {
230- target_group_arn = aws_lb_target_group. fhir_api . arn
251+ target_group_arn = aws_lb_target_group. fhir_api_tg . arn
231252 container_name = " ${ var . account_name } -fhir-api"
232253 container_port = var. fhir_api_port
233254 }
@@ -242,7 +263,7 @@ resource "aws_lb" "fhir_api_alb" {
242263 subnets = var. networking . public_subnet_ids
243264}
244265
245- resource "aws_lb_target_group" "fhir_api " {
266+ resource "aws_lb_target_group" "fhir_api_tg " {
246267 name = " ${ var . account_name } -fhir-api-tg"
247268 port = var. fhir_api_port
248269 protocol = " HTTP"
@@ -256,7 +277,7 @@ resource "aws_lb_target_group" "fhir_api" {
256277 timeout = 5
257278 healthy_threshold = 2
258279 unhealthy_threshold = 10
259- matcher = " 200"
280+ matcher = " 200"
260281 }
261282}
262283
@@ -267,6 +288,6 @@ resource "aws_lb_listener" "http" {
267288
268289 default_action {
269290 type = " forward"
270- target_group_arn = aws_lb_target_group. fhir_api . arn
291+ target_group_arn = aws_lb_target_group. fhir_api_tg . arn
271292 }
272293}
0 commit comments