Skip to content

Commit b16ea39

Browse files
Wbprice/https load balancer update redirect rules (#208)
* configure https listeners and cert configuration * create domain name module * remove counts, since we're just going to have this run all the time now * remove counts, since we're just going to have this run all the time now * that might work * ok * toggle on ssl in prod * https * format * add preview query string flag --------- Co-authored-by: Blaine Price <[email protected]>
1 parent f2b0667 commit b16ea39

File tree

6 files changed

+166
-9
lines changed

6 files changed

+166
-9
lines changed

infrastructure/envs/dev/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ data "aws_vpc" "default" {
3333
}
3434
}
3535

36+
module "domains" {
37+
source = "../../modules/domains"
38+
39+
tier = var.tier
40+
}
41+
3642
module "repositories" {
3743
source = "../../modules/repositories"
3844

@@ -137,6 +143,10 @@ module "fhir-api" {
137143
alb_security_group_id = module.networking.alb_security_group_id
138144
api_security_group_id = module.networking.api_security_group_id
139145
vpc_id = module.networking.vpc_id
146+
directory_domain = module.domains.directory_domain
147+
enable_ssl_directory = false
148+
api_domain = module.domains.api_domain
149+
enable_ssl_api = false
140150
}
141151
}
142152

infrastructure/envs/prod/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ data "aws_vpc" "default" {
3131
}
3232
}
3333

34+
module "domains" {
35+
source = "../../modules/domains"
36+
37+
tier = var.tier
38+
}
39+
3440
module "repositories" {
3541
source = "../../modules/repositories"
3642

@@ -138,6 +144,10 @@ module "fhir-api" {
138144
alb_security_group_id = module.networking.alb_security_group_id
139145
api_security_group_id = module.networking.api_security_group_id
140146
vpc_id = module.networking.vpc_id
147+
directory_domain = module.domains.directory_domain
148+
enable_ssl_directory = true
149+
api_domain = module.domains.api_domain
150+
enable_ssl_api = true
141151
}
142152
}
143153

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
locals {
2+
domains = {
3+
dev = {
4+
etl = "etl.dev.directory.internal.cms.gov"
5+
api = "api.dev.directory.internal.cms.gov"
6+
directory = "dev.directory.internal.cms.gov"
7+
}
8+
impl = {
9+
etl = "etl.impl.directory.internal.cms.gov"
10+
api = "api.impl.directory.internal.cms.gov"
11+
directory = "impl.directory.internal.cms.gov"
12+
}
13+
prod = {
14+
etl = "etl.directory.internal.cms.gov"
15+
api = "api.directory.cms.gov" # public route
16+
directory = "directory.cms.gov" # public route
17+
}
18+
}
19+
api_domain = local.domains[var.tier]["api"]
20+
directory_domain = local.domains[var.tier]["directory"]
21+
etl_domain = local.domains[var.tier]["etl"]
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "api_domain" {
2+
value = local.api_domain
3+
}
4+
output "directory_domain" {
5+
value = local.directory_domain
6+
}
7+
output "etl_domain" {
8+
value = local.etl_domain
9+
}

infrastructure/modules/fhir-api/main.tf

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ resource "aws_ecs_task_definition" "app" {
264264

265265
# API ECS Service
266266
resource "aws_ecs_service" "app" {
267-
count = var.redirect_to_strategy_page == true ? 0 : 1
268267
name = "${var.account_name}-fhir-api-service"
269268
cluster = var.ecs_cluster_id
270269
task_definition = aws_ecs_task_definition.app.arn
@@ -278,13 +277,14 @@ resource "aws_ecs_service" "app" {
278277
}
279278

280279
load_balancer {
281-
target_group_arn = aws_lb_target_group.fhir_api_tg[0].arn
280+
target_group_arn = aws_lb_target_group.fhir_api_tg.arn
282281
container_name = "${var.account_name}-fhir-api"
283282
container_port = var.fhir_api_port
284283
}
285284
}
286285

287-
# API Load Balancer Configuration
286+
# ALB directory.cms.gov traffic
287+
288288
resource "aws_lb" "fhir_api_alb" {
289289
name = "${var.account_name}-fhir-api-alb"
290290
internal = var.private_load_balancer
@@ -294,7 +294,6 @@ resource "aws_lb" "fhir_api_alb" {
294294
}
295295

296296
resource "aws_lb_target_group" "fhir_api_tg" {
297-
count = var.redirect_to_strategy_page ? 0 : 1
298297
name = "${var.account_name}-fhir-api-tg"
299298
port = var.fhir_api_port
300299
protocol = "HTTP"
@@ -312,6 +311,11 @@ resource "aws_lb_target_group" "fhir_api_tg" {
312311
}
313312
}
314313

314+
# Port 80 traffic
315+
# TODO: upgrade all incoming traffic to HTTPS after:
316+
# - internal domain names are registered
317+
# - ssl certs are requested and validated
318+
315319
resource "aws_lb_listener" "forward_to_task_group" {
316320
count = var.redirect_to_strategy_page ? 0 : 1
317321
load_balancer_arn = aws_lb.fhir_api_alb.arn
@@ -320,7 +324,7 @@ resource "aws_lb_listener" "forward_to_task_group" {
320324

321325
default_action {
322326
type = "forward"
323-
target_group_arn = aws_lb_target_group.fhir_api_tg[0].arn
327+
target_group_arn = aws_lb_target_group.fhir_api_tg.arn
324328
}
325329
}
326330

@@ -340,6 +344,82 @@ resource "aws_lb_listener" "forward_to_strategy_page" {
340344
}
341345
}
342346

347+
resource "aws_lb_listener_rule" "preview_flag" {
348+
count = var.redirect_to_strategy_page ? 1 : 0
349+
listener_arn = aws_lb_listener.forward_to_strategy_page[0].arn
350+
351+
condition {
352+
query_string {
353+
key = "preview"
354+
value = "true"
355+
}
356+
}
357+
358+
action {
359+
type = "forward"
360+
target_group_arn = aws_lb_target_group.fhir_api_tg.arn
361+
}
362+
}
363+
364+
# Port 443 Traffic
365+
# TODO: upgrade all incoming traffic to HTTPS after:
366+
# - internal domain names are registered
367+
# - ssl certs are requested and validated
368+
369+
data "aws_acm_certificate" "directory_ssl_cert" {
370+
count = var.networking.enable_ssl_directory ? 1 : 0
371+
domain = var.networking.directory_domain
372+
statuses = ["ISSUED"]
373+
}
374+
375+
resource "aws_lb_listener" "forward_to_task_group_https" {
376+
count = var.redirect_to_strategy_page && var.networking.enable_ssl_directory ? 0 : 1
377+
load_balancer_arn = aws_lb.fhir_api_alb.arn
378+
port = 443
379+
protocol = "HTTP"
380+
381+
default_action {
382+
type = "forward"
383+
target_group_arn = aws_lb_target_group.fhir_api_tg.arn
384+
}
385+
}
386+
387+
resource "aws_lb_listener" "forward_to_strategy_page_https" {
388+
count = var.redirect_to_strategy_page && var.networking.enable_ssl_directory ? 1 : 0
389+
load_balancer_arn = aws_lb.fhir_api_alb.arn
390+
port = 443
391+
protocol = "HTTPS"
392+
certificate_arn = data.aws_acm_certificate.directory_ssl_cert[0].arn
393+
394+
default_action {
395+
type = "redirect"
396+
redirect {
397+
status_code = "HTTP_302"
398+
host = "www.cms.gov"
399+
path = "/priorities/health-technology-ecosystem/overview"
400+
}
401+
}
402+
}
403+
404+
resource "aws_lb_listener_rule" "preview_flag_https" {
405+
count = var.redirect_to_strategy_page ? 1 : 0
406+
listener_arn = aws_lb_listener.forward_to_strategy_page_https[0].arn
407+
408+
condition {
409+
query_string {
410+
key = "preview"
411+
value = "true"
412+
}
413+
}
414+
415+
action {
416+
type = "forward"
417+
target_group_arn = aws_lb_target_group.fhir_api_tg.arn
418+
}
419+
}
420+
421+
# api.directory.cms.gov and friends
422+
343423
resource "aws_alb" "fhir_api_alb_redirect" {
344424
name = "${var.account_name}-fhir-redirect"
345425
internal = var.private_load_balancer
@@ -349,7 +429,6 @@ resource "aws_alb" "fhir_api_alb_redirect" {
349429
}
350430

351431
resource "aws_alb_listener" "forward_to_directory_slash_fhir" {
352-
count = var.redirect_to_strategy_page ? 0 : 1
353432
load_balancer_arn = aws_alb.fhir_api_alb_redirect.arn
354433
port = 80
355434
protocol = "HTTP"
@@ -359,9 +438,32 @@ resource "aws_alb_listener" "forward_to_directory_slash_fhir" {
359438
redirect {
360439
status_code = "HTTP_302"
361440
port = 80
362-
# TODO replace this with a domain name not dns name
363-
host = aws_lb.fhir_api_alb.dns_name
364-
path = "/fhir/#{path}"
441+
host = var.networking.directory_domain
442+
path = "/fhir/#{path}"
443+
}
444+
}
445+
}
446+
447+
data "aws_acm_certificate" "api_directory_ssl_cert" {
448+
count = var.networking.enable_ssl_api ? 1 : 0
449+
domain = var.networking.api_domain
450+
statuses = ["ISSUED"]
451+
}
452+
453+
resource "aws_alb_listener" "forward_to_directory_slash_fhir_https" {
454+
count = var.networking.enable_ssl_api ? 1 : 0
455+
load_balancer_arn = aws_alb.fhir_api_alb_redirect.arn
456+
port = 443
457+
protocol = "HTTPS"
458+
certificate_arn = data.aws_acm_certificate.api_directory_ssl_cert[0].arn
459+
460+
default_action {
461+
type = "redirect"
462+
redirect {
463+
status_code = "HTTP_302"
464+
port = 443
465+
host = var.networking.directory_domain
466+
path = "/fhir/#{path}"
365467
}
366468
}
367469
}

infrastructure/modules/fhir-api/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ variable "db" {
1818
}
1919
variable "networking" {
2020
type = object({
21+
api_domain = string
22+
enable_ssl_api = bool
23+
directory_domain = string
24+
enable_ssl_directory = bool
2125
private_subnet_ids = list(string)
2226
public_subnet_ids = list(string)
2327
alb_security_group_id = string

0 commit comments

Comments
 (0)