elb.tf 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 = true # TODO: Turn this on
  24. fqdns = local.hostnames
  25. # Set WAF to 'count' for now
  26. block_settings = {
  27. "default" = true # Default action. False = count
  28. "custom" = true # XDR Custom Rules. False = count
  29. "admin" = true # Block admin pages.
  30. "AWSManagedRulesCommonRuleSet" = false
  31. "AWSManagedRulesAmazonIpReputationList" = false
  32. "AWSManagedRulesKnownBadInputsRuleSet" = false
  33. "AWSManagedRulesSQLiRuleSet" = false # Irrelevant, module is disabled
  34. "AWSManagedRulesLinuxRuleSet" = false # Irrelevant, module is disabled
  35. "AWSManagedRulesUnixRuleSet" = false # Irrelevant, module is disabled
  36. }
  37. excluded_rules_AWSManagedRulesCommonRuleSet = ["SizeRestrictions_BODY"]
  38. #excluded_rules_AWSManagedRulesAmazonIpReputationList = []
  39. #excluded_rules_AWSManagedRulesKnownBadInputsRuleSet = []
  40. #excluded_rules_AWSManagedRulesSQLiRuleSet = [] # Module disabled
  41. #excluded_rules_AWSManagedRulesLinuxRuleSet = [] # Module disabled
  42. #excluded_rules_AWSManagedRulesUnixRuleSet = [] # Module disabled
  43. # Excluded Rulesets
  44. # There are too many hostnames, so we have to disable some
  45. excluded_set_AWSManagedRulesCommonRuleSet = false
  46. excluded_set_AWSManagedRulesAmazonIpReputationList = false
  47. excluded_set_AWSManagedRulesKnownBadInputsRuleSet = false
  48. excluded_set_AWSManagedRulesSQLiRuleSet = true
  49. excluded_set_AWSManagedRulesLinuxRuleSet = true
  50. excluded_set_AWSManagedRulesUnixRuleSet = true
  51. #additional_blocked_ips = []
  52. #allowed_ips = []
  53. #admin_ips = []
  54. # Optional Variables
  55. healthcheck_port = 443
  56. healthcheck_protocol = "HTTPS"
  57. healthcheck_path = "/status"
  58. healthcheck_matcher = "200"
  59. stickiness = false
  60. # Inherited Variables
  61. tags = merge(var.standard_tags, var.tags)
  62. dns_info = var.dns_info
  63. public_subnets = var.public_subnets
  64. environment = var.environment
  65. aws_partition = var.aws_partition
  66. aws_region = var.aws_region
  67. aws_account_id = var.aws_account_id
  68. vpc_id = var.vpc_id
  69. providers = {
  70. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  71. aws.c2 = aws.c2
  72. }
  73. }
  74. # Github Needs a Wildcard Record
  75. module "public_dns_record_wildcard" {
  76. source = "../../submodules/dns/public_ALIAS_record"
  77. name = "*.github.${var.dns_info["public"]["zone"]}"
  78. target_dns_name = module.elb.nlb.dns_name
  79. target_zone_id = module.elb.nlb.zone_id
  80. dns_info = var.dns_info
  81. providers = {
  82. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  83. }
  84. }
  85. #################################
  86. # Add port 80 to the ALB and NLB
  87. #
  88. # GHE uses LetsEncrypt, which needs access on port 80.
  89. # ALB side
  90. resource "aws_lb_target_group" "github_alb_80" {
  91. name_prefix = "gita80"
  92. port = 80
  93. protocol = "HTTP"
  94. vpc_id = var.vpc_id
  95. health_check {
  96. protocol = "HTTPS"
  97. port = 443
  98. path = "/status"
  99. matcher = "200"
  100. timeout = "4"
  101. interval = "5"
  102. }
  103. lifecycle {
  104. create_before_destroy = true
  105. }
  106. tags = merge(var.standard_tags, var.tags)
  107. }
  108. resource "aws_lb_target_group_attachment" "github_alb_80" {
  109. for_each = toset(aws_instance.ghe[*].id)
  110. target_group_arn = aws_lb_target_group.github_alb_80.arn
  111. target_id = each.value
  112. port = 80
  113. }
  114. resource "aws_lb_listener" "github_alb_80" {
  115. load_balancer_arn = module.elb.alb_id
  116. port = "80" # tfsec:ignore:aws-elb-http-not-used HTTP only used for letsencrypt and redirect
  117. protocol = "HTTP"
  118. default_action {
  119. type = "forward"
  120. target_group_arn = aws_lb_target_group.github_alb_80.arn
  121. }
  122. lifecycle {
  123. create_before_destroy = true
  124. }
  125. tags = merge(var.standard_tags, var.tags)
  126. }
  127. resource "aws_security_group_rule" "github_alb_80" {
  128. description = "Github - Allow 80 from any"
  129. type = "ingress"
  130. from_port = 80
  131. to_port = 80
  132. protocol = "tcp"
  133. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally Open
  134. security_group_id = module.elb.security_group_id
  135. }
  136. resource "aws_security_group_rule" "github_alb_80_out" {
  137. description = "Github - Allow 80 to the instances"
  138. type = "egress"
  139. from_port = 80
  140. to_port = 80
  141. protocol = "tcp"
  142. source_security_group_id = aws_security_group.ghe_server.id
  143. security_group_id = module.elb.security_group_id
  144. }
  145. # NLB Side
  146. resource "aws_lb_target_group" "github_nlb_80" {
  147. name_prefix = "gitn80"
  148. target_type = "alb"
  149. port = 80
  150. protocol = "TCP"
  151. vpc_id = var.vpc_id
  152. lifecycle {
  153. create_before_destroy = true
  154. }
  155. tags = merge(var.standard_tags, var.tags)
  156. }
  157. resource "aws_lb_target_group_attachment" "github_nlb_80" {
  158. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  159. target_id = module.elb.alb_id
  160. port = 80
  161. }
  162. resource "aws_lb_listener" "github_nlb_80" {
  163. load_balancer_arn = module.elb.nlb_id
  164. port = "80"
  165. protocol = "TCP" # tfsec:ignore:aws-elb-http-not-used HTTP only for letsencrypt and redirects
  166. default_action {
  167. type = "forward"
  168. target_group_arn = aws_lb_target_group.github_nlb_80.arn
  169. }
  170. lifecycle {
  171. create_before_destroy = true
  172. }
  173. tags = merge(var.standard_tags, var.tags)
  174. }
  175. ##########################
  176. # Add port 22 to the NLB
  177. resource "aws_lb_target_group" "github_ssh" {
  178. name_prefix = "gitssh"
  179. port = 22
  180. protocol = "TCP"
  181. vpc_id = var.vpc_id
  182. lifecycle {
  183. create_before_destroy = true
  184. }
  185. tags = merge(var.standard_tags, var.tags)
  186. }
  187. resource "aws_lb_target_group_attachment" "github_ssh" {
  188. for_each = toset(aws_instance.ghe[*].id)
  189. target_group_arn = aws_lb_target_group.github_ssh.arn
  190. target_id = each.value
  191. port = 22
  192. }
  193. resource "aws_lb_listener" "github_ssh" {
  194. load_balancer_arn = module.elb.nlb_id
  195. port = "22"
  196. protocol = "TCP"
  197. default_action {
  198. type = "forward"
  199. target_group_arn = aws_lb_target_group.github_ssh.arn
  200. }
  201. lifecycle {
  202. create_before_destroy = true
  203. }
  204. tags = merge(var.standard_tags, var.tags)
  205. }