elb-elastic.tf 4.2 KB

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