@@ -264,7 +264,6 @@ resource "aws_ecs_task_definition" "app" {
264264
265265# API ECS Service
266266resource "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+
288288resource "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
296296resource "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+
315319resource "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+
343423resource "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
351431resource "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}
0 commit comments