elb.tf 7.5 KB

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