elb.tf 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #----------
  60. # DNS Entry
  61. #----------
  62. module "public_dns_record" {
  63. source = "../../submodules/dns/public_ALIAS_record"
  64. name = "portal"
  65. target_dns_name = aws_alb.portal.dns_name
  66. target_zone_id = aws_alb.portal.zone_id
  67. dns_info = var.dns_info
  68. providers = {
  69. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  70. }
  71. }
  72. #------------------------------------
  73. # Security Group
  74. #------------------------------------
  75. resource "aws_security_group" "customer_portal_alb" {
  76. name = "customer_portal_alb_inbound_sg"
  77. description = "Allow Customer Portal ALB HTTP Traffic Inbound"
  78. vpc_id = var.vpc_id
  79. }
  80. resource "aws_security_group_rule" "customer_portal_alb_https" {
  81. protocol = "tcp"
  82. type = "ingress"
  83. from_port = 443
  84. to_port = 443
  85. security_group_id = aws_security_group.customer_portal_alb.id
  86. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  87. }
  88. #Allow viewing of test portal from home. We don't want world to view test portal.
  89. resource "aws_security_group_rule" "customer_portal_alb_https_test" {
  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 = flatten(var.portal_test_whitelist)
  96. }
  97. ## Needed for HTTPs redirect
  98. resource "aws_security_group_rule" "customer_portal_alb_http" {
  99. protocol = "tcp"
  100. type = "ingress"
  101. from_port = 80
  102. to_port = 80
  103. security_group_id = aws_security_group.customer_portal_alb.id
  104. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  105. }
  106. resource "aws_security_group_rule" "customer_portal_alb" {
  107. protocol = "tcp"
  108. type = "egress"
  109. from_port = 443
  110. to_port = 443
  111. security_group_id = aws_security_group.customer_portal_alb.id
  112. source_security_group_id = aws_security_group.customer_portal.id
  113. }