elb.tf 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #----------------------------------------------------------------------------
  2. # EXTERNAL LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_lb" "server_external" {
  5. name_prefix = local.prefix
  6. security_groups = [aws_security_group.alb.id]
  7. internal = false # tfsec:ignore:aws-elb-alb-not-public The ALB requires Internet exposure
  8. subnets = var.subnets
  9. load_balancer_type = "application"
  10. drop_invalid_header_fields = true
  11. access_logs {
  12. bucket = "xdr-elb-${var.environment}"
  13. enabled = true
  14. }
  15. idle_timeout = 1200
  16. tags = merge(local.tags, { "Name" : local.name })
  17. }
  18. # Create a new target group
  19. resource "aws_lb_target_group" "server_external" {
  20. name_prefix = local.prefix
  21. port = var.server_port
  22. protocol = var.server_protocol
  23. vpc_id = var.vpc_id
  24. health_check {
  25. protocol = var.server_protocol
  26. port = var.server_port
  27. path = var.health_check_path
  28. matcher = "200,302"
  29. timeout = "4"
  30. interval = "5"
  31. unhealthy_threshold = 2
  32. healthy_threshold = 2
  33. }
  34. dynamic "stickiness" {
  35. for_each = var.sticky_sessions == true ? toset([1]) : toset([])
  36. content {
  37. type = "lb_cookie"
  38. enabled = true
  39. }
  40. }
  41. tags = merge(local.tags, { "Name" : local.name })
  42. }
  43. resource "aws_lb_target_group_attachment" "server_external" {
  44. # This needs explanation.
  45. # If I were to for_each over var.target_servers, then we get the annoying warning:
  46. #
  47. # │ The "for_each" value depends on resource attributes that cannot be
  48. # │ determined until apply, so Terraform cannot predict how many instances will
  49. # │ be created. To work around this, use the -target argument to first apply
  50. # │ only the resources that the for_each depends on.
  51. #
  52. # If instead we pass in a list and a count, we avoid this, and we can do it all in one
  53. # atomic apply, making us happier engineers.
  54. count = var.target_count
  55. target_group_arn = aws_lb_target_group.server_external.arn
  56. target_id = var.target_servers[count.index]
  57. port = var.server_port
  58. }
  59. # Create a new alb listener
  60. resource "aws_lb_listener" "server_https_external" {
  61. load_balancer_arn = aws_lb.server_external.arn
  62. port = "443"
  63. protocol = "HTTPS"
  64. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  65. certificate_arn = aws_acm_certificate.cert_public.arn
  66. default_action {
  67. target_group_arn = aws_lb_target_group.server_external.arn
  68. type = "forward"
  69. }
  70. tags = merge(local.tags, { "Name" : local.name })
  71. }
  72. resource "aws_lb_listener" "jira_server_listener_http" {
  73. load_balancer_arn = aws_lb.server_external.arn
  74. port = "80"
  75. protocol = "HTTP"
  76. default_action {
  77. type = "redirect"
  78. redirect {
  79. port = "443"
  80. protocol = "HTTPS"
  81. status_code = "HTTP_301"
  82. }
  83. }
  84. tags = merge(local.tags, { "Name" : local.name })
  85. }