elb.tf 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # ---------------------------------------------------------------------------------------------------------------------
  2. # LOAD BALANCER FOR PORTAL
  3. # ---------------------------------------------------------------------------------------------------------------------
  4. resource "aws_alb" "portal" {
  5. name = "portal-alb-${var.environment}"
  6. security_groups = [aws_security_group.customer_portal_alb.id, ]
  7. internal = false #tfsec:ignore:aws-elb-alb-not-public The ALB requires Internet exposure
  8. subnets = var.public_subnets
  9. tags = merge(local.standard_tags, var.tags, { Name = "portal-alb-${var.environment}" })
  10. access_logs {
  11. bucket = "xdr-elb-${var.environment}"
  12. prefix = ""
  13. enabled = true
  14. }
  15. }
  16. # Create a new target group
  17. resource "aws_alb_target_group" "portal" {
  18. name = "portal-alb-targets-${var.environment}"
  19. port = 443
  20. protocol = "HTTPS"
  21. vpc_id = var.vpc_id
  22. health_check {
  23. protocol = "HTTPS"
  24. path = "/api/health/"
  25. matcher = "200-400"
  26. timeout = "4"
  27. interval = "15"
  28. unhealthy_threshold = 2
  29. healthy_threshold = 2
  30. }
  31. stickiness {
  32. type = "lb_cookie"
  33. enabled = false
  34. }
  35. tags = merge(local.standard_tags, var.tags, )
  36. }
  37. # Create a new alb listener ( certificate_arn wait for DNS cut over )
  38. resource "aws_alb_listener" "portal_https" {
  39. load_balancer_arn = aws_alb.portal.arn
  40. port = "443"
  41. protocol = "HTTPS"
  42. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  43. certificate_arn = aws_acm_certificate.cert.arn
  44. default_action {
  45. target_group_arn = aws_alb_target_group.portal.arn
  46. type = "forward"
  47. }
  48. }
  49. # HTTPs Redirect
  50. resource "aws_lb_listener" "portal_https_redirect" {
  51. load_balancer_arn = aws_alb.portal.arn
  52. port = "80"
  53. protocol = "HTTP"
  54. default_action {
  55. type = "redirect"
  56. redirect {
  57. port = "443"
  58. protocol = "HTTPS"
  59. status_code = "HTTP_301"
  60. }
  61. }
  62. }
  63. # Attach the instances to the ELB
  64. resource "aws_autoscaling_attachment" "customer_portal_asg_attachment" {
  65. lb_target_group_arn = aws_alb_target_group.portal.arn
  66. autoscaling_group_name = aws_autoscaling_group.customer_portal.name
  67. }
  68. #----------
  69. # DNS Entry
  70. #----------
  71. module "public_dns_record" {
  72. source = "../../submodules/dns/public_ALIAS_record"
  73. name = "portal"
  74. target_dns_name = aws_alb.portal.dns_name
  75. target_zone_id = aws_alb.portal.zone_id
  76. dns_info = var.dns_info
  77. providers = {
  78. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  79. }
  80. }
  81. #------------------------------------
  82. # Security Group
  83. #------------------------------------
  84. resource "aws_security_group" "customer_portal_alb" {
  85. name = "customer_portal_alb_inbound_sg"
  86. description = "Allow Customer Portal ALB HTTP Traffic Inbound"
  87. vpc_id = var.vpc_id
  88. }
  89. resource "aws_security_group_rule" "customer_portal_alb_https" {
  90. protocol = "tcp"
  91. type = "ingress"
  92. from_port = 443
  93. to_port = 443
  94. security_group_id = aws_security_group.customer_portal_alb.id
  95. cidr_blocks = [var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  96. }
  97. #Allow viewing of test portal from home. We don't want world to view test portal.
  98. resource "aws_security_group_rule" "customer_portal_alb_https_test" {
  99. protocol = "tcp"
  100. type = "ingress"
  101. from_port = 443
  102. to_port = 443
  103. security_group_id = aws_security_group.customer_portal_alb.id
  104. cidr_blocks = flatten(distinct(concat(local.portal_test_whitelist, formatlist("%s/32", var.nat_public_ips), local.trusted_ips)))
  105. }
  106. ## Needed for HTTPs redirect
  107. resource "aws_security_group_rule" "customer_portal_alb_http" {
  108. protocol = "tcp"
  109. type = "ingress"
  110. from_port = 80
  111. to_port = 80
  112. security_group_id = aws_security_group.customer_portal_alb.id
  113. cidr_blocks = [var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  114. }
  115. # Needed for Sensu Check from the proxy in test
  116. resource "aws_security_group_rule" "customer_portal_sensu_check" {
  117. count = var.environment == "test" ? 1 : 0
  118. protocol = "tcp"
  119. type = "ingress"
  120. from_port = 443
  121. to_port = 443
  122. security_group_id = aws_security_group.customer_portal_alb.id
  123. cidr_blocks = ["${var.proxy_public_ip}/32", ]
  124. }
  125. resource "aws_security_group_rule" "customer_portal_alb" {
  126. protocol = "tcp"
  127. type = "egress"
  128. from_port = 443
  129. to_port = 443
  130. security_group_id = aws_security_group.customer_portal_alb.id
  131. source_security_group_id = aws_security_group.customer_portal.id
  132. }