elb.tf 4.2 KB

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