elb.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  8. subnets = var.public_subnets
  9. tags = merge( var.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. }
  27. stickiness {
  28. type = "lb_cookie"
  29. enabled = false
  30. }
  31. tags = merge( var.standard_tags, var.tags, )
  32. }
  33. # Create a new alb listener ( certificate_arn wait for DNS cut over )
  34. resource "aws_alb_listener" "portal_https" {
  35. load_balancer_arn = aws_alb.portal.arn
  36. port = "443"
  37. protocol = "HTTPS"
  38. ssl_policy = "ELBSecurityPolicy-TLS-1-2-2017-01"
  39. certificate_arn = aws_acm_certificate.cert.arn
  40. default_action {
  41. target_group_arn = aws_alb_target_group.portal.arn
  42. type = "forward"
  43. }
  44. }
  45. # HTTPs Redirect
  46. resource "aws_lb_listener" "portal_https_redirect" {
  47. load_balancer_arn = aws_alb.portal.arn
  48. port = "80"
  49. protocol = "HTTP"
  50. default_action {
  51. type = "redirect"
  52. redirect {
  53. port = "443"
  54. protocol = "HTTPS"
  55. status_code = "HTTP_301"
  56. }
  57. }
  58. }
  59. # Attach the instances to the ELB
  60. resource "aws_autoscaling_attachment" "customer_portal_asg_attachment" {
  61. alb_target_group_arn = aws_alb_target_group.portal.arn
  62. autoscaling_group_name = aws_autoscaling_group.customer_portal.name
  63. }
  64. #----------
  65. # DNS Entry
  66. #----------
  67. module "public_dns_record" {
  68. source = "../../submodules/dns/public_ALIAS_record"
  69. name = "portal"
  70. target_dns_name = aws_alb.portal.dns_name
  71. target_zone_id = aws_alb.portal.zone_id
  72. dns_info = var.dns_info
  73. providers = {
  74. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  75. }
  76. }
  77. #------------------------------------
  78. # Security Group
  79. #------------------------------------
  80. resource "aws_security_group" "customer_portal_alb" {
  81. name = "customer_portal_alb_inbound_sg"
  82. description = "Allow Customer Portal ALB HTTP Traffic Inbound"
  83. vpc_id = var.vpc_id
  84. }
  85. resource "aws_security_group_rule" "customer_portal_alb_https" {
  86. protocol = "tcp"
  87. type = "ingress"
  88. from_port = 443
  89. to_port = 443
  90. security_group_id = aws_security_group.customer_portal_alb.id
  91. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  92. }
  93. #Allow viewing of test portal from home. We don't want world to view test portal.
  94. resource "aws_security_group_rule" "customer_portal_alb_https_test" {
  95. protocol = "tcp"
  96. type = "ingress"
  97. from_port = 443
  98. to_port = 443
  99. security_group_id = aws_security_group.customer_portal_alb.id
  100. cidr_blocks = flatten(concat(var.portal_test_whitelist, formatlist("%s/32",var.nat_public_ips)))
  101. }
  102. ## Needed for HTTPs redirect
  103. resource "aws_security_group_rule" "customer_portal_alb_http" {
  104. protocol = "tcp"
  105. type = "ingress"
  106. from_port = 80
  107. to_port = 80
  108. security_group_id = aws_security_group.customer_portal_alb.id
  109. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  110. }
  111. # Needed for Sensu Check from the proxy in test
  112. resource "aws_security_group_rule" "customer_portal_sensu_check" {
  113. count = var.environment == "test" ? 1 : 0
  114. protocol = "tcp"
  115. type = "ingress"
  116. from_port = 443
  117. to_port = 443
  118. security_group_id = aws_security_group.customer_portal_alb.id
  119. cidr_blocks = [ "${var.proxy_public_ip}/32", ]
  120. }
  121. resource "aws_security_group_rule" "customer_portal_alb" {
  122. protocol = "tcp"
  123. type = "egress"
  124. from_port = 443
  125. to_port = 443
  126. security_group_id = aws_security_group.customer_portal_alb.id
  127. source_security_group_id = aws_security_group.customer_portal.id
  128. }