elb.tf 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. ))
  8. }
  9. resource "aws_lb" "searchhead-alb" {
  10. name = var.alb_name != "" ? "${local.alb_name}-alb" : "${var.prefix}-searchhead-alb"
  11. internal = true
  12. load_balancer_type = "application"
  13. # Not supported for NLB
  14. security_groups = [aws_security_group.searchhead-alb-sg.id]
  15. # Note, changing subnets results in recreation of the resource
  16. subnets = var.subnets
  17. enable_cross_zone_load_balancing = true
  18. access_logs {
  19. bucket = "xdr-elb-${ var.environment }"
  20. enabled = true
  21. }
  22. tags = merge(var.standard_tags, var.tags)
  23. }
  24. #########################
  25. # Listeners
  26. resource "aws_lb_listener" "searchhead-alb-listener-https" {
  27. load_balancer_arn = aws_lb.searchhead-alb.arn
  28. port = "443"
  29. protocol = "HTTPS"
  30. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  31. certificate_arn = aws_acm_certificate.cert.arn
  32. default_action {
  33. type = "forward"
  34. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  35. }
  36. }
  37. resource "aws_lb_listener" "searchhead-alb-listener-8000" {
  38. load_balancer_arn = aws_lb.searchhead-alb.arn
  39. port = "8000"
  40. protocol = "HTTPS"
  41. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  42. certificate_arn = aws_acm_certificate.cert.arn
  43. default_action {
  44. type = "forward"
  45. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  46. }
  47. }
  48. # Only alb's can redirect
  49. resource "aws_lb_listener" "searchhead-alb-listener-http" {
  50. load_balancer_arn = aws_lb.searchhead-alb.arn
  51. port = "80"
  52. protocol = "HTTP"
  53. default_action {
  54. type = "redirect"
  55. redirect {
  56. port = "443"
  57. protocol = "HTTPS"
  58. status_code = "HTTP_301"
  59. }
  60. }
  61. }
  62. resource "aws_lb_listener" "searchhead-alb-listener-api" {
  63. load_balancer_arn = aws_lb.searchhead-alb.arn
  64. port = "8089"
  65. protocol = "HTTPS"
  66. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  67. certificate_arn = aws_acm_certificate.cert.arn
  68. default_action {
  69. type = "forward"
  70. target_group_arn = aws_lb_target_group.searchhead-alb-target-api.arn
  71. }
  72. }
  73. #########################
  74. # Targets
  75. resource "aws_lb_target_group" "searchhead-alb-target-8000" {
  76. name = var.alb_name != "" ? "${local.alb_name}-alb-target-8000" : "${var.prefix}-sh-alb-target-8000"
  77. port = 8000
  78. protocol = "HTTPS"
  79. target_type = "instance"
  80. vpc_id = var.vpc_id
  81. tags = merge(var.standard_tags, var.tags)
  82. health_check {
  83. enabled = true
  84. path = "/en-US/account/login?return_to=%2Fen-US%2F"
  85. port = 8000
  86. protocol = "HTTPS"
  87. }
  88. # Stickiness is not needed here, but we'll need it if we add SHs
  89. stickiness {
  90. type = "lb_cookie"
  91. cookie_duration = 86400 # 1 day
  92. enabled = true
  93. }
  94. }
  95. resource "aws_lb_target_group_attachment" "searchhead-alb-target-8000-instance" {
  96. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  97. target_id = aws_instance.instance.id
  98. port = 8000
  99. }
  100. resource "aws_lb_target_group" "searchhead-alb-target-api" {
  101. name = var.alb_name != "" ? "${local.alb_name}-target-api" : "${var.prefix}-sh-alb-target-api"
  102. port = 8089
  103. protocol = "HTTPS"
  104. target_type = "instance"
  105. vpc_id = var.vpc_id
  106. tags = merge(var.standard_tags, var.tags)
  107. health_check {
  108. enabled = true
  109. #path = "/services/server/health/splunkd" # reportedly works, but doesn't
  110. path = "/"
  111. port = 8089
  112. protocol = "HTTPS"
  113. }
  114. }
  115. resource "aws_lb_target_group_attachment" "searchhead-alb-target-api-instance" {
  116. target_group_arn = aws_lb_target_group.searchhead-alb-target-api.arn
  117. target_id = aws_instance.instance.id
  118. port = 8089
  119. }
  120. #########################
  121. # Security Group for ALB
  122. resource "aws_security_group" "searchhead-alb-sg" {
  123. name = var.alb_name != "" ? "${local.alb_name}-alb-sh" : "${var.prefix}-sh-alb-sg"
  124. description = "Security Group for the Searchhead ALB"
  125. vpc_id = var.vpc_id
  126. tags = merge(var.standard_tags, var.tags)
  127. }
  128. resource "aws_security_group_rule" "searchhead-alb-api-in" {
  129. type = "ingress"
  130. from_port = 8089
  131. to_port = 8089
  132. protocol = "tcp"
  133. cidr_blocks = local.alb_clients
  134. security_group_id = aws_security_group.searchhead-alb-sg.id
  135. }
  136. resource "aws_security_group_rule" "searchhead-alb-https-in" {
  137. type = "ingress"
  138. from_port = 443
  139. to_port = 443
  140. protocol = "tcp"
  141. cidr_blocks = local.alb_clients
  142. security_group_id = aws_security_group.searchhead-alb-sg.id
  143. }
  144. resource "aws_security_group_rule" "searchhead-alb-8000-in" {
  145. type = "ingress"
  146. from_port = 8000
  147. to_port = 8000
  148. protocol = "tcp"
  149. cidr_blocks = local.alb_clients
  150. security_group_id = aws_security_group.searchhead-alb-sg.id
  151. }
  152. resource "aws_security_group_rule" "searchhead-http-in" {
  153. # Port 80 is open as a redirect to 443
  154. type = "ingress"
  155. from_port = 80
  156. to_port = 80
  157. protocol = "tcp"
  158. cidr_blocks = local.alb_clients
  159. security_group_id = aws_security_group.searchhead-alb-sg.id
  160. }
  161. resource "aws_security_group_rule" "searchhead-alb-8000-out" {
  162. type = "egress"
  163. from_port = 8000
  164. to_port = 8000
  165. protocol = "tcp"
  166. # Maybe should limit to the local vpc, but I don't readily have that cidr available
  167. cidr_blocks = [ "10.0.0.0/8" ]
  168. security_group_id = aws_security_group.searchhead-alb-sg.id
  169. }
  170. resource "aws_security_group_rule" "searchhead-alb-api-out" {
  171. type = "egress"
  172. from_port = 8089
  173. to_port = 8089
  174. protocol = "tcp"
  175. # Maybe should limit to the local vpc, but I don't readily have that cidr available
  176. cidr_blocks = [ "10.0.0.0/8" ]
  177. security_group_id = aws_security_group.searchhead-alb-sg.id
  178. }
  179. #########################
  180. # DNS Entry
  181. resource "aws_route53_record" "searchhead_internal" {
  182. zone_id = var.dns_info["private"]["zone_id"]
  183. name = local.alb_name
  184. type = "CNAME"
  185. records = [aws_lb.searchhead-alb.dns_name]
  186. ttl = "60"
  187. provider = aws.c2
  188. }