elb.tf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. resource "aws_lb" "openvpn-nlb" {
  2. name = "${ var.instance_name }-nlb"
  3. internal = false
  4. load_balancer_type = "network"
  5. # Not supported for NLB
  6. #security_groups = [aws_security_group.openvpn-nlb-sg.id]
  7. # Note, changing subnets results in recreation of the resource
  8. subnets = var.public_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" "openvpn-nlb-listener-https" {
  19. load_balancer_arn = aws_lb.openvpn-nlb.arn
  20. port = "443"
  21. protocol = "TLS"
  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.openvpn-nlb-target-https.arn
  27. }
  28. }
  29. # Only alb's can redirect
  30. #resource "aws_lb_listener" "openvpn-nlb-listener-http" {
  31. # load_balancer_arn = aws_lb.openvpn-nlb.arn
  32. # port = "80"
  33. # protocol = "HTTP"
  34. #
  35. # default_action {
  36. # type = "redirect"
  37. #
  38. # redirect {
  39. # port = "443"
  40. # protocol = "HTTPS"
  41. # status_code = "HTTP_301"
  42. # }
  43. # }
  44. #}
  45. resource "aws_lb_listener" "openvpn-nlb-listener-openvpn" {
  46. load_balancer_arn = aws_lb.openvpn-nlb.arn
  47. port = "1194"
  48. protocol = "UDP"
  49. default_action {
  50. type = "forward"
  51. target_group_arn = aws_lb_target_group.openvpn-nlb-target-openvpn.arn
  52. }
  53. }
  54. #########################
  55. # Targets
  56. resource "aws_lb_target_group" "openvpn-nlb-target-https" {
  57. name = "${ var.instance_name }-nlb-target-https"
  58. port = 443
  59. protocol = "TLS"
  60. target_type = "instance"
  61. vpc_id = var.vpc_id
  62. tags = merge(var.standard_tags, var.tags)
  63. }
  64. resource "aws_lb_target_group_attachment" "openvpn-nlb-target-https-instance" {
  65. target_group_arn = aws_lb_target_group.openvpn-nlb-target-https.arn
  66. target_id = aws_instance.instance.id
  67. port = 443
  68. }
  69. resource "aws_lb_target_group" "openvpn-nlb-target-openvpn" {
  70. name = "${ var.instance_name }-nlb-target-openvpn"
  71. port = 1194
  72. protocol = "UDP"
  73. target_type = "instance"
  74. vpc_id = var.vpc_id
  75. tags = merge(var.standard_tags, var.tags)
  76. }
  77. resource "aws_lb_target_group_attachment" "openvpn-nlb-target-openvpn-instance" {
  78. target_group_arn = aws_lb_target_group.openvpn-nlb-target-openvpn.arn
  79. target_id = aws_instance.instance.id
  80. port = 1194
  81. }
  82. #########################
  83. # Security Group for NLB
  84. #
  85. # From tf:
  86. # Error: error creating network Load Balancer: InvalidConfigurationRequest: Security groups are not supported for load balancers with type 'network'
  87. #resource "aws_security_group" "openvpn-nlb-sg" {
  88. # name = "openvpn_nlb_sg"
  89. # description = "Security Group for the OpenVPN NLB"
  90. # vpc_id = var.vpc_id
  91. # tags = merge(var.standard_tags, var.tags)
  92. #}
  93. #
  94. #resource "aws_security_group_rule" "openvpn-nlb-in" {
  95. # type = "ingress"
  96. # from_port = 1194
  97. # to_port = 1194
  98. # protocol = "udp"
  99. # cidr_blocks = [ "0.0.0.0/0" ]
  100. # security_group_id = aws_security_group.openvpn-nlb-sg.id
  101. #}
  102. #
  103. #resource "aws_security_group_rule" "openvpn-nlb-https-in" {
  104. # type = "ingress"
  105. # from_port = 443
  106. # to_port = 443
  107. # protocol = "tcp"
  108. # cidr_blocks = [ "0.0.0.0/0" ]
  109. # security_group_id = aws_security_group.openvpn-nlb-sg.id
  110. #}
  111. #
  112. #resource "aws_security_group_rule" "openvpn-nlb-out" {
  113. # type = "egress"
  114. # from_port = 1194
  115. # to_port = 1194
  116. # protocol = "udp"
  117. # # Maybe should limit to the local vpc, but I don't readily have that cidr available
  118. # cidr_blocks = [ "10.0.0.0/8" ]
  119. # security_group_id = aws_security_group.openvpn-nlb-sg.id
  120. #}
  121. #
  122. #resource "aws_security_group_rule" "openvpn-nlb-https-out" {
  123. # type = "egress"
  124. # from_port = 443
  125. # to_port = 443
  126. # protocol = "tcp"
  127. # # Maybe should limit to the local vpc, but I don't readily have that cidr available
  128. # cidr_blocks = [ "10.0.0.0/8" ]
  129. # security_group_id = aws_security_group.openvpn-nlb-sg.id
  130. #}
  131. #########################
  132. # DNS Entry
  133. module "public_dns_record" {
  134. source = "../../submodules/dns/public_ALIAS_record"
  135. name = var.instance_name
  136. target_dns_name = aws_lb.openvpn-nlb.dns_name
  137. target_zone_id = aws_lb.openvpn-nlb.zone_id
  138. dns_info = var.dns_info
  139. providers = {
  140. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  141. }
  142. }