elb.tf 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. locals {
  2. # alb_clients access the SH
  3. alb_clients = (
  4. var.environment == "test" ?
  5. toset(concat(
  6. [ "10.0.0.0/8" ],
  7. var.portal_test_whitelist
  8. ))
  9. :
  10. [ "0.0.0.0/0" ]
  11. )
  12. }
  13. resource "aws_lb" "searchhead-alb" {
  14. name = local.alb_name
  15. internal = false
  16. load_balancer_type = "application"
  17. # Not supported for NLB
  18. security_groups = [aws_security_group.searchhead-alb-sg.id]
  19. # Note, changing subnets results in recreation of the resource
  20. subnets = var.public_subnets
  21. enable_cross_zone_load_balancing = true
  22. access_logs {
  23. bucket = "xdr-elb-${ var.environment }"
  24. enabled = true
  25. }
  26. tags = merge(var.standard_tags, var.tags)
  27. }
  28. #########################
  29. # Listeners
  30. resource "aws_lb_listener" "searchhead-alb-listener-https" {
  31. load_balancer_arn = aws_lb.searchhead-alb.arn
  32. port = "443"
  33. protocol = "HTTPS"
  34. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  35. certificate_arn = aws_acm_certificate.cert.arn
  36. default_action {
  37. type = "forward"
  38. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  39. }
  40. }
  41. # Redirect HTTP to HTTPS
  42. resource "aws_lb_listener" "searchhead-alb-listener-http" {
  43. load_balancer_arn = aws_lb.searchhead-alb.arn
  44. port = "80"
  45. protocol = "HTTP"
  46. default_action {
  47. type = "redirect"
  48. redirect {
  49. port = "443"
  50. protocol = "HTTPS"
  51. status_code = "HTTP_301"
  52. }
  53. }
  54. }
  55. #########################
  56. # Targets
  57. resource "aws_lb_target_group" "searchhead-alb-target-8000" {
  58. name = "${local.alb_name}-8000"
  59. port = 8000
  60. protocol = "HTTPS"
  61. target_type = "instance"
  62. vpc_id = var.vpc_id
  63. tags = merge(var.standard_tags, var.tags)
  64. health_check {
  65. enabled = true
  66. path = "/en-US/account/login?return_to=%2Fen-US%2F"
  67. port = 8000
  68. protocol = "HTTPS"
  69. }
  70. # Stickiness is not needed here, but we'll need it if we add SHs
  71. stickiness {
  72. type = "lb_cookie"
  73. cookie_duration = 86400 # 1 day
  74. enabled = true
  75. }
  76. }
  77. resource "aws_lb_target_group_attachment" "searchhead-alb-target-8000-instance" {
  78. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  79. target_id = aws_instance.instance.id
  80. port = 8000
  81. }
  82. #########################
  83. # Security Group for ALB
  84. resource "aws_security_group" "searchhead-alb-sg" {
  85. name = "${local.alb_name}-customer-alb-sh"
  86. description = "Security Group for the Customer Searchhead ALB"
  87. vpc_id = var.vpc_id
  88. tags = merge(var.standard_tags, var.tags)
  89. }
  90. resource "aws_security_group_rule" "searchhead-alb-https-in" {
  91. type = "ingress"
  92. from_port = 443
  93. to_port = 443
  94. protocol = "tcp"
  95. cidr_blocks = local.alb_clients
  96. security_group_id = aws_security_group.searchhead-alb-sg.id
  97. }
  98. resource "aws_security_group_rule" "searchhead-http-in" {
  99. # Port 80 is open as a redirect to 443
  100. type = "ingress"
  101. from_port = 80
  102. to_port = 80
  103. protocol = "tcp"
  104. cidr_blocks = local.alb_clients
  105. security_group_id = aws_security_group.searchhead-alb-sg.id
  106. }
  107. resource "aws_security_group_rule" "searchhead-alb-8000-out" {
  108. type = "egress"
  109. from_port = 8000
  110. to_port = 8000
  111. protocol = "tcp"
  112. # Maybe should limit to the local vpc, but I don't readily have that cidr available
  113. cidr_blocks = [ var.vpc_cidr ]
  114. security_group_id = aws_security_group.searchhead-alb-sg.id
  115. }
  116. #########################
  117. # DNS Entry
  118. module "public_dns_record_cust-elb" {
  119. source = "../../../submodules/dns/public_ALIAS_record"
  120. name = local.dns_short_name
  121. target_dns_name = aws_lb.searchhead-alb.dns_name
  122. target_zone_id = aws_lb.searchhead-alb.zone_id
  123. dns_info = var.dns_info
  124. providers = {
  125. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  126. }
  127. }