elb-hec.tf 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. resource "aws_lb" "alsi-alb-hec" {
  2. # checkov:skip=CKV2_AWS_28: TO DO - WAF
  3. # checkov:skip=CKV_AWS_150: Skip deletion protection - Test env
  4. count = local.alsi_hec_alb ? 1 : 0
  5. name = "${var.prefix}-alsi-alb-hec"
  6. internal = false # tfsec:ignore:aws-elb-alb-not-public The ALB requires Internet exposure
  7. load_balancer_type = "application"
  8. drop_invalid_header_fields = true
  9. # Not supported for NLB
  10. security_groups = [aws_security_group.alsi-alb-hec-sg.id]
  11. # Note, changing subnets results in recreation of the resource
  12. subnets = var.subnets
  13. enable_cross_zone_load_balancing = true
  14. access_logs {
  15. bucket = "xdr-elb-${var.environment}"
  16. enabled = true
  17. }
  18. tags = merge(local.standard_tags, var.tags)
  19. }
  20. #########################
  21. # Listeners
  22. resource "aws_lb_listener" "alsi-alb-hec-listener-https" {
  23. count = local.alsi_hec_alb ? 1 : 0
  24. load_balancer_arn = aws_lb.alsi-alb-hec[count.index].arn
  25. port = "443"
  26. protocol = "HTTPS"
  27. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  28. certificate_arn = aws_acm_certificate.cert_hec[count.index].arn
  29. default_action {
  30. type = "forward"
  31. target_group_arn = aws_lb_target_group.alsi-alb-hec-target-8088[count.index].arn
  32. }
  33. }
  34. # Only alb's can redirect
  35. resource "aws_lb_listener" "alsi-alb-hec-listener-http" {
  36. count = local.alsi_hec_alb ? 1 : 0
  37. load_balancer_arn = aws_lb.alsi-alb-hec[count.index].arn
  38. port = "80"
  39. protocol = "HTTP"
  40. default_action {
  41. type = "redirect"
  42. redirect {
  43. port = "443"
  44. protocol = "HTTPS"
  45. status_code = "HTTP_301"
  46. }
  47. }
  48. }
  49. #########################
  50. # Targets
  51. resource "aws_lb_target_group" "alsi-alb-hec-target-8088" {
  52. count = local.alsi_hec_alb ? 1 : 0
  53. name = "${var.prefix}-alsi-hec-8088"
  54. port = 8088
  55. protocol = "HTTPS"
  56. target_type = "instance"
  57. vpc_id = var.vpc_id
  58. tags = merge(local.standard_tags, var.tags)
  59. health_check {
  60. enabled = true
  61. path = "/api/v1/health"
  62. port = 8088
  63. protocol = "HTTPS"
  64. matcher = "200,405"
  65. }
  66. # sure would be nice to check the actual port
  67. #health_check {
  68. # enabled = true
  69. # path = "/"
  70. # port = 9000
  71. # protocol = "HTTPS"
  72. #}
  73. }
  74. resource "aws_lb_target_group_attachment" "alsi-alb-hec-target-8088-instance" {
  75. count = local.alsi_workers * (local.alsi_hec_alb ? 1 : 0)
  76. target_group_arn = aws_lb_target_group.alsi-alb-hec-target-8088[0].arn
  77. target_id = aws_instance.worker[count.index].id
  78. port = 8088
  79. }
  80. #----------------------------------------------------------------------------
  81. # Security Group for ALB
  82. #----------------------------------------------------------------------------
  83. resource "aws_security_group" "alsi-alb-hec-sg" {
  84. name_prefix = "${var.prefix}-alsi-alb-hec-sg"
  85. lifecycle { create_before_destroy = true } # handle updates gracefully
  86. description = "Security Group for the Cribl ALB for hec"
  87. vpc_id = var.vpc_id
  88. tags = merge(local.standard_tags, var.tags)
  89. }
  90. #----------------------------------------------------------------------------
  91. # INGRESS
  92. #----------------------------------------------------------------------------
  93. resource "aws_security_group_rule" "alsi-alb-hec-https-in" {
  94. type = "ingress"
  95. description = "HTTPS - Inbound"
  96. from_port = 443
  97. to_port = 443
  98. protocol = "tcp"
  99. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  100. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  101. }
  102. resource "aws_security_group_rule" "alsi-elastic-http-in" {
  103. # Port 80 is open as a redirect to 443
  104. type = "ingress"
  105. description = "HTTP redirect HTTPS - Inbound"
  106. from_port = 80
  107. to_port = 80
  108. protocol = "tcp"
  109. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  110. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  111. }
  112. #----------------------------------------------------------------------------
  113. # EGRESS
  114. #----------------------------------------------------------------------------
  115. resource "aws_security_group_rule" "alsi-alb-hec-8088-out" {
  116. type = "egress"
  117. description = "8088 - Outbound"
  118. from_port = 8088
  119. to_port = 8088
  120. protocol = "tcp"
  121. source_security_group_id = aws_security_group.alsi_worker_security_group.id
  122. security_group_id = aws_security_group.alsi-alb-hec-sg.id
  123. }
  124. #----------------------------------------------------------------------------
  125. # DNS Entry
  126. #----------------------------------------------------------------------------
  127. resource "aws_route53_record" "alsi-alb-hec" {
  128. count = local.alsi_hec_alb ? 1 : 0
  129. zone_id = var.dns_info["public"]["zone_id"]
  130. name = "${var.prefix}-alsi-hec"
  131. type = "CNAME"
  132. records = [aws_lb.alsi-alb-hec[count.index].dns_name]
  133. ttl = "60"
  134. provider = aws.mdr-common-services-commercial
  135. }