elb.tf 6.1 KB

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