elb.tf 5.8 KB

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