elb.tf 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. resource "aws_lb_listener" "searchhead-alb-listener-https" {
  32. load_balancer_arn = aws_lb.searchhead-alb.arn
  33. port = "443"
  34. protocol = "HTTPS"
  35. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  36. certificate_arn = aws_acm_certificate.cert.arn
  37. default_action {
  38. type = "forward"
  39. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  40. }
  41. }
  42. # Redirect HTTP to HTTPS
  43. resource "aws_lb_listener" "searchhead-alb-listener-http" {
  44. load_balancer_arn = aws_lb.searchhead-alb.arn
  45. port = "80"
  46. protocol = "HTTP"
  47. default_action {
  48. type = "redirect"
  49. redirect {
  50. port = "443"
  51. protocol = "HTTPS"
  52. status_code = "HTTP_301"
  53. }
  54. }
  55. }
  56. #########################
  57. # Targets
  58. resource "aws_lb_target_group" "searchhead-alb-target-8000" {
  59. name = "${local.alb_name}-8000"
  60. port = 8000
  61. protocol = "HTTPS"
  62. target_type = "instance"
  63. vpc_id = var.vpc_id
  64. tags = merge(local.standard_tags, var.tags)
  65. health_check {
  66. enabled = true
  67. path = "/en-US/account/login?return_to=%2Fen-US%2F"
  68. port = 8000
  69. protocol = "HTTPS"
  70. }
  71. # Stickiness is not needed here, but we'll need it if we add SHs
  72. stickiness {
  73. type = "lb_cookie"
  74. cookie_duration = 86400 # 1 day
  75. enabled = true
  76. }
  77. }
  78. resource "aws_lb_target_group_attachment" "searchhead-alb-target-8000-instance" {
  79. target_group_arn = aws_lb_target_group.searchhead-alb-target-8000.arn
  80. target_id = aws_instance.instance.id
  81. port = 8000
  82. }
  83. #########################
  84. # Security Group for ALB
  85. resource "aws_security_group" "searchhead-alb-sg" {
  86. name = "${local.alb_name}-customer-alb-sh"
  87. description = "Security Group for the Customer Searchhead ALB"
  88. vpc_id = var.vpc_id
  89. tags = merge(local.standard_tags, var.tags)
  90. }
  91. resource "aws_security_group_rule" "searchhead-alb-https-in" {
  92. description = "HTTPS in"
  93. type = "ingress"
  94. from_port = 443
  95. to_port = 443
  96. protocol = "tcp"
  97. cidr_blocks = local.alb_clients
  98. security_group_id = aws_security_group.searchhead-alb-sg.id
  99. }
  100. resource "aws_security_group_rule" "searchhead-http-in" {
  101. # Port 80 is open as a redirect to 443
  102. description = "Allow redirect from 80 to 443"
  103. type = "ingress"
  104. from_port = 80
  105. to_port = 80
  106. protocol = "tcp"
  107. cidr_blocks = local.alb_clients
  108. security_group_id = aws_security_group.searchhead-alb-sg.id
  109. }
  110. resource "aws_security_group_rule" "searchhead-alb-8000-out" {
  111. description = "Allow outbound to default splunk web"
  112. type = "egress"
  113. from_port = 8000
  114. to_port = 8000
  115. protocol = "tcp"
  116. # Maybe should limit to the local vpc, but I don't readily have that cidr available
  117. cidr_blocks = [var.vpc_cidr]
  118. security_group_id = aws_security_group.searchhead-alb-sg.id
  119. }
  120. #########################
  121. # DNS Entry
  122. module "public_dns_record_cust-elb" {
  123. source = "../../../submodules/dns/public_ALIAS_record"
  124. name = local.dns_short_name
  125. target_dns_name = aws_lb.searchhead-alb.dns_name
  126. target_zone_id = aws_lb.searchhead-alb.zone_id
  127. dns_info = var.dns_info
  128. providers = {
  129. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  130. }
  131. }