elb-elastic.tf 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. resource "aws_lb" "alsi-alb-elastic" {
  2. # checkov:skip=CKV2_AWS_28: TO DO - WAF
  3. # checkov:skip=CKV_AWS_150: Skip deletion protection - Test env
  4. count = local.alsi_elastic_alb ? 1 : 0
  5. name = "${var.prefix}-alsi-alb-elastic"
  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-elastic-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-elastic-listener-https" {
  23. count = local.alsi_elastic_alb ? 1 : 0
  24. load_balancer_arn = aws_lb.alsi-alb-elastic[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_elastic[count.index].arn
  29. default_action {
  30. type = "forward"
  31. target_group_arn = aws_lb_target_group.alsi-alb-elastic-target-9200[count.index].arn
  32. }
  33. }
  34. # Only alb's can redirect
  35. resource "aws_lb_listener" "alsi-alb-elastic-listener-http" {
  36. count = local.alsi_elastic_alb ? 1 : 0
  37. load_balancer_arn = aws_lb.alsi-alb-elastic[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-elastic-target-9200" {
  52. count = local.alsi_elastic_alb ? 1 : 0
  53. name = "${var.prefix}-alsi-elastic-9200"
  54. port = 9200
  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 = 9000
  63. protocol = "HTTPS"
  64. }
  65. # sure would be nice to check the actual port
  66. #health_check {
  67. # enabled = true
  68. # path = "/"
  69. # port = 9000
  70. # protocol = "HTTPS"
  71. #}
  72. }
  73. resource "aws_lb_target_group_attachment" "alsi-alb-elastic-target-9200-instance" {
  74. count = local.alsi_workers * (local.alsi_elastic_alb ? 1 : 0)
  75. target_group_arn = aws_lb_target_group.alsi-alb-elastic-target-9200[0].arn
  76. target_id = aws_instance.worker[count.index].id
  77. port = 9200
  78. }
  79. #----------------------------------------------------------------------------
  80. # Security Group for ALB
  81. #----------------------------------------------------------------------------
  82. resource "aws_security_group" "alsi-alb-elastic-sg" {
  83. name_prefix = "${var.prefix}-alsi-alb-elastic-sg"
  84. lifecycle { create_before_destroy = true } # handle updates gracefully
  85. description = "Security Group for the Cribl ALB for elastic"
  86. vpc_id = var.vpc_id
  87. tags = merge(local.standard_tags, var.tags)
  88. }
  89. #----------------------------------------------------------------------------
  90. # INGRESS
  91. #----------------------------------------------------------------------------
  92. resource "aws_security_group_rule" "alsi-alb-elastic-https-in" {
  93. type = "ingress"
  94. description = "HTTPS - Inbound"
  95. from_port = 443
  96. to_port = 443
  97. protocol = "tcp"
  98. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  99. security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  100. }
  101. resource "aws_security_group_rule" "alsi-hec-http-in" {
  102. # Port 80 is open as a redirect to 443
  103. type = "ingress"
  104. description = "HTTP redirect HTTPS - Inbound"
  105. from_port = 80
  106. to_port = 80
  107. protocol = "tcp"
  108. cidr_blocks = toset(concat(local.cidr_map["vpc-access"], local.trusted_ips, local.splunk_data_sources))
  109. security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  110. }
  111. #----------------------------------------------------------------------------
  112. # EGRESS
  113. #----------------------------------------------------------------------------
  114. resource "aws_security_group_rule" "alsi-alb-elastic-9200-out" {
  115. type = "egress"
  116. description = "9200 - Outbound"
  117. from_port = 9200
  118. to_port = 9200
  119. protocol = "tcp"
  120. source_security_group_id = aws_security_group.alsi_worker_security_group.id
  121. security_group_id = aws_security_group.alsi-alb-elastic-sg.id
  122. }
  123. #----------------------------------------------------------------------------
  124. # DNS Entry
  125. #----------------------------------------------------------------------------
  126. resource "aws_route53_record" "alsi-alb-elastic" {
  127. count = local.alsi_elastic_alb ? 1 : 0
  128. zone_id = var.dns_info["public"]["zone_id"]
  129. name = "${var.prefix}-alsi-elastic"
  130. type = "CNAME"
  131. records = [aws_lb.alsi-alb-elastic[count.index].dns_name]
  132. ttl = "60"
  133. provider = aws.mdr-common-services-commercial
  134. }