elb.tf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Architecture:
  2. # 1. DNS points to an NLB
  3. # 2. NLB:22 forwards to instance:22
  4. # 3. NLB:443 forward to an ALB, which forwards to the instance
  5. # 4. NLB:80 forwards to the same ALB, which forwards to the instance.
  6. #
  7. # The module "static_nlb_to_alb" takes care of #3, but the rest
  8. # we have to handle here.
  9. #
  10. # tfsec:ignore:aws-elb-alb-not-public Purposefully public
  11. module "elb" {
  12. source = "../../submodules/load_balancer/static_nlb_to_alb"
  13. name = "github"
  14. subject_alternative_names = ["*.github.${var.dns_info["public"]["zone"]}"]
  15. target_ids = aws_instance.ghe[*].id
  16. listener_port = 443
  17. target_port = 443
  18. target_protocol = "HTTPS"
  19. target_security_group = aws_security_group.ghe_server.id
  20. allow_from_any = true
  21. redirect_80 = false # GitHub handles port 80, and needs it for LetsEncrypt
  22. # WAF variables
  23. waf_enabled = false # TODO: Turn this on
  24. #excluded_rules_AWSManagedRulesCommonRuleSet = [ "SizeRestrictions_BODY" ]
  25. #excluded_rules_AWSManagedRulesAmazonIpReputationList = []
  26. #excluded_rules_AWSManagedRulesKnownBadInputsRuleSet = []
  27. #excluded_rules_AWSManagedRulesSQLiRuleSet = []
  28. #excluded_rules_AWSManagedRulesLinuxRuleSet = []
  29. #excluded_rules_AWSManagedRulesUnixRuleSet = []
  30. #additional_blocked_ips = []
  31. #allowed_ips = []
  32. #admin_ips = []
  33. # Optional Variables
  34. healthcheck_port = 443
  35. healthcheck_protocol = "HTTPS"
  36. healthcheck_path = "/status"
  37. healthcheck_matcher = "200"
  38. stickiness = false
  39. # Inherited Variables
  40. tags = merge(var.standard_tags, var.tags)
  41. dns_info = var.dns_info
  42. public_subnets = var.public_subnets
  43. environment = var.environment
  44. aws_partition = var.aws_partition
  45. aws_region = var.aws_region
  46. aws_account_id = var.aws_account_id
  47. vpc_id = var.vpc_id
  48. providers = {
  49. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  50. aws.c2 = aws.c2
  51. }
  52. }
  53. # Github Needs a Wildcard Record
  54. module "public_dns_record_wildcard" {
  55. source = "../../submodules/dns/public_ALIAS_record"
  56. name = "*.github.${var.dns_info["public"]["zone"]}"
  57. target_dns_name = module.elb.nlb.dns_name
  58. target_zone_id = module.elb.nlb.zone_id
  59. dns_info = var.dns_info
  60. providers = {
  61. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  62. }
  63. }
  64. #################################
  65. # Add port 80 to the ALB and NLB
  66. #
  67. # GHE uses LetsEncrypt, which needs access on port 80.
  68. # ALB side
  69. resource "aws_lb_target_group" "github_alb_80" {
  70. name_prefix = "gita80"
  71. port = 80
  72. protocol = "HTTP"
  73. vpc_id = var.vpc_id
  74. health_check {
  75. protocol = "HTTPS"
  76. port = 443
  77. path = "/status"
  78. matcher = "200"
  79. timeout = "4"
  80. interval = "5"
  81. }
  82. lifecycle {
  83. create_before_destroy = true
  84. }
  85. tags = merge(var.standard_tags, var.tags)
  86. }
  87. resource "aws_lb_target_group_attachment" "github_alb_80" {
  88. for_each = toset(aws_instance.ghe[*].id)
  89. target_group_arn = aws_lb_target_group.github_alb_80.arn
  90. target_id = each.value
  91. port = 80
  92. }
  93. resource "aws_lb_listener" "github_alb_80" {
  94. load_balancer_arn = module.elb.alb_id
  95. port = "80" # tfsec:ignore:aws-elb-http-not-used HTTP only used for letsencrypt and redirect
  96. protocol = "HTTP"
  97. default_action {
  98. type = "forward"
  99. target_group_arn = aws_lb_target_group.github_alb_80.arn
  100. }
  101. lifecycle {
  102. create_before_destroy = true
  103. }
  104. tags = merge(var.standard_tags, var.tags)
  105. }
  106. resource "aws_security_group_rule" "github_alb_80" {
  107. description = "Github - Allow 80 from any"
  108. type = "ingress"
  109. from_port = 80
  110. to_port = 80
  111. protocol = "tcp"
  112. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally Open
  113. security_group_id = module.elb.security_group_id
  114. }
  115. resource "aws_security_group_rule" "github_alb_80_out" {
  116. description = "Github - Allow 80 to the instances"
  117. type = "egress"
  118. from_port = 80
  119. to_port = 80
  120. protocol = "tcp"
  121. source_security_group_id = aws_security_group.ghe_server.id
  122. security_group_id = module.elb.security_group_id
  123. }
  124. # NLB Side
  125. resource "aws_lb_target_group" "github_nlb_80" {
  126. name_prefix = "gitn80"
  127. target_type = "alb"
  128. port = 80
  129. protocol = "TCP"
  130. vpc_id = var.vpc_id
  131. lifecycle {
  132. create_before_destroy = true
  133. }
  134. tags = merge(var.standard_tags, var.tags)
  135. }
  136. resource "aws_lb_target_group_attachment" "github_nlb_80" {
  137. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  138. target_id = module.elb.alb_id
  139. port = 80
  140. }
  141. resource "aws_lb_listener" "github_nlb_80" {
  142. load_balancer_arn = module.elb.nlb_id
  143. port = "80"
  144. protocol = "TCP" # tfsec:ignore:aws-elb-http-not-used HTTP only for letsencrypt and redirects
  145. default_action {
  146. type = "forward"
  147. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  148. }
  149. lifecycle {
  150. create_before_destroy = true
  151. }
  152. tags = merge(var.standard_tags, var.tags)
  153. }
  154. ##########################
  155. # Add port 22 to the NLB
  156. resource "aws_lb_target_group" "github_ssh" {
  157. name_prefix = "gitssh"
  158. port = 22
  159. protocol = "TCP"
  160. vpc_id = var.vpc_id
  161. lifecycle {
  162. create_before_destroy = true
  163. }
  164. tags = merge(var.standard_tags, var.tags)
  165. }
  166. resource "aws_lb_target_group_attachment" "github_ssh" {
  167. for_each = toset(aws_instance.ghe[*].id)
  168. target_group_arn = aws_lb_target_group.github_ssh.arn
  169. target_id = each.value
  170. port = 22
  171. }
  172. resource "aws_lb_listener" "github_ssh" {
  173. load_balancer_arn = module.elb.nlb_id
  174. port = "22"
  175. protocol = "TCP"
  176. default_action {
  177. type = "forward"
  178. target_group_arn = aws_lb_target_group.github_ssh.arn
  179. }
  180. lifecycle {
  181. create_before_destroy = true
  182. }
  183. tags = merge(var.standard_tags, var.tags)
  184. }