elb.tf 4.9 KB

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