nlb.tf 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # KeyCloak Needs an NLB:
  2. # * ALB/ELB can't terminate SSL, because Keycloak needs the certificate
  3. # * Because they don't terminate SSL, they can't provide X-forwarded-for, and keycloak needs the source IP
  4. # * Therefore, we use an NLB and preserve the source IP.
  5. module "public_dns_record" {
  6. source = "../../submodules/dns/public_ALIAS_record"
  7. name = "keycloak.${var.dns_info["public"]["zone"]}"
  8. target_dns_name = aws_lb.external.dns_name
  9. target_zone_id = aws_lb.external.zone_id
  10. dns_info = var.dns_info
  11. providers = {
  12. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  13. }
  14. }
  15. #module "public_dns_record_2" {
  16. # # A second dns record that can be used after configuring the XDR realm
  17. # source = "../../submodules/dns/public_ALIAS_record"
  18. #
  19. # name = "auth.${var.dns_info["public"]["zone"]}"
  20. # target_dns_name = aws_lb.external.dns_name
  21. # target_zone_id = aws_lb.external.zone_id
  22. # dns_info = var.dns_info
  23. #
  24. # providers = {
  25. # aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  26. # }
  27. #}
  28. resource "aws_lb" "external" {
  29. name = "keycloak-external-nlb"
  30. load_balancer_type = "network"
  31. internal = false
  32. subnets = var.public_subnets
  33. access_logs {
  34. bucket = "xdr-elb-${ var.environment }"
  35. enabled = true
  36. }
  37. enable_cross_zone_load_balancing = true
  38. idle_timeout = 300
  39. tags = merge(var.standard_tags, var.tags)
  40. }
  41. resource "aws_lb_listener" "nlb_443" {
  42. load_balancer_arn = aws_lb.external.arn
  43. port = "443"
  44. protocol = "TCP"
  45. default_action {
  46. type = "forward"
  47. target_group_arn = aws_lb_target_group.external.arn
  48. }
  49. }
  50. resource "aws_lb_target_group" "external" {
  51. name = "keycloak-external-nlb"
  52. port = 8443
  53. protocol = "TCP"
  54. vpc_id = var.vpc_id
  55. target_type = "instance"
  56. health_check {
  57. enabled = true
  58. #healthy_threshold = 3
  59. #unhealthy_threshold = 2
  60. timeout = 10
  61. interval = 10
  62. #matcher = "200,302"
  63. path = "/"
  64. protocol = "HTTPS"
  65. }
  66. stickiness {
  67. enabled = true
  68. type = "source_ip" # only option for NLBs
  69. }
  70. }
  71. # Create a new load balancer attachment
  72. resource "aws_lb_target_group_attachment" "external_attachment" {
  73. count = var.keycloak_instance_count
  74. target_group_arn = aws_lb_target_group.external.arn
  75. target_id = aws_instance.instance[count.index].id
  76. }