Skip to content

Commit 2f4c890

Browse files
Wbprice/troubleshoot api service not starting (#121)
* Troubleshoot API Startup Failures - Troubleshoot networking configuration between load balancer and API security group - Troubleshoot API IAM permissions - Troubleshoot vpn security group permission - add env.* to gitignore --------- Co-authored-by: Blaine Price <william.price@cms.hhs.gov>
1 parent ea53e1a commit 2f4c890

File tree

7 files changed

+126
-58
lines changed

7 files changed

+126
-58
lines changed

infrastructure/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.terraform
22
.terraform.lock.hcl
33
terraform.tfstate
4-
terraform.tfstate.backup
4+
terraform.tfstate.backup
5+
.env.*

infrastructure/envs/dev/main.tf

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = "~> 5.0"
7+
version = "~> 6.0"
88
}
99
}
1010

@@ -51,6 +51,7 @@ module "api-db" {
5151
allocated_storage = 20
5252
publicly_accessible = false
5353
username = "npd"
54+
db_name = "npd"
5455
vpc_security_group_ids = [module.networking.db_security_group_id]
5556
db_subnet_group_name = module.networking.db_subnet_group_name
5657
backup_retention_period = 7 # Remove automated snapshots after 7 days
@@ -79,21 +80,16 @@ module "etl-db" {
7980
# ECS Cluster
8081
module "ecs" {
8182
source = "terraform-aws-modules/ecs/aws"
82-
version = "5.12.1"
83+
version = "6.6.2"
8384

8485
cluster_name = "${local.account_name}-ecs-cluster"
85-
86-
fargate_capacity_providers = {
86+
default_capacity_provider_strategy = {
8787
FARGATE = {
88-
default_capacity_provider_strategy = {
89-
weight = 50
90-
base = 20
91-
}
88+
weight = 50
89+
base = 20
9290
}
9391
FARGATE_SPOT = {
94-
default_capacity_provider_strategy = {
95-
weight = 50
96-
}
92+
weight = 50
9793
}
9894
}
9995
}
@@ -103,14 +99,14 @@ module "fhir-api" {
10399
source = "../../modules/fhir-api"
104100

105101
account_name = local.account_name
106-
app_db_name = "npd"
107102
fhir_api_migration_image = var.migration_image
108103
fhir_api_image = var.fhir_api_image
109104
ecs_cluster_id = module.ecs.cluster_id
110105
db = {
111106
db_instance_master_user_secret_arn = module.api-db.db_instance_master_user_secret_arn
112107
db_instance_address = module.api-db.db_instance_address
113108
db_instance_port = module.api-db.db_instance_port
109+
db_instance_name = module.api-db.db_instance_name
114110
}
115111
networking = {
116112
db_subnet_ids = module.networking.db_subnet_ids

infrastructure/envs/dev/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ variable "tier" {
66
default = "dev"
77
}
88

9-
variable "migration_image" { default = "public.ecr.aws/docker/library/hello-world:nanoserver-ltsc2022" }
10-
variable "fhir_api_image" { default = "public.ecr.aws/docker/library/hello-world:nanoserver-ltsc2022" }
9+
variable "migration_image" { default = "575012135727.dkr.ecr.us-east-1.amazonaws.com/npd-east-dev-fhir-api-migrations:latest" }
10+
variable "fhir_api_image" { default = "575012135727.dkr.ecr.us-east-1.amazonaws.com/npd-east-dev-fhir-api:latest" }

infrastructure/modules/fhir-api/main.tf

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ data "aws_partition" "current" {}
33
data "aws_caller_identity" "current" {}
44

55
# ECR Repositories
6+
67
resource "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+
2846
resource "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

4664
resource "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

5169
resource "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

7190
resource "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
}

infrastructure/modules/fhir-api/variables.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
variable "account_name" {}
2-
variable "app_db_name" {}
32
variable "fhir_api_image" {}
3+
variable "fhir_api_migration_image" {}
44
variable "fhir_api_port" {
55
default = 8000
66
}
7-
variable "fhir_api_migration_image" {}
87
variable "ecs_cluster_id" {}
98
variable "db" {
109
type = object({
1110
db_instance_master_user_secret_arn = string
1211
db_instance_address = string
12+
db_instance_name = string
1313
db_instance_port = string
1414
})
1515
}

0 commit comments

Comments
 (0)