elb.tf 5.6 KB

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