elb.tf 4.6 KB

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