elb.tf 3.6 KB

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