elb.tf 2.9 KB

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