elb.tf 4.0 KB

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