elb.tf 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. locals {
  2. #allow some users to view test portal
  3. portal_test_whitelist = [
  4. "12.245.107.250/32", # DPS Office Legato
  5. "12.204.167.162/32", # DPS Office San Antonio
  6. "54.86.98.62/32", # DPS AWS User VPN
  7. "75.138.227.80/32", # Duane Waddle
  8. "24.11.231.98/32", # George Starcher
  9. "99.151.37.185/32", # Wesley Leonard
  10. "70.106.200.157/32", # John Reuther
  11. "73.10.53.113/32", # Rick Page Home
  12. "50.21.207.50/32", # Brad Poulton
  13. "70.160.60.248/32", # Brandon Naughton
  14. "99.56.213.129/32", # Frederick Damstra
  15. ]
  16. }
  17. # ---------------------------------------------------------------------------------------------------------------------
  18. # LOAD BALANCER FOR PORTAL
  19. # ---------------------------------------------------------------------------------------------------------------------
  20. data "aws_caller_identity" "current" {}
  21. resource "aws_alb" "portal" {
  22. name = "portal-alb-${var.environment}"
  23. security_groups = [ aws_security_group.customer_portal_alb.id, ]
  24. internal = false
  25. subnets = var.subnets
  26. tags = merge( var.standard_tags, var.tags, { Name = "portal-alb-${var.environment}" })
  27. access_logs {
  28. bucket = "xdr-elb-${ var.environment }"
  29. prefix = ""
  30. enabled = true
  31. }
  32. }
  33. # Create a new target group
  34. resource "aws_alb_target_group" "portal" {
  35. name = "portal-alb-targets-${var.environment}"
  36. port = 443
  37. protocol = "HTTPS"
  38. vpc_id = var.vpc_id
  39. health_check {
  40. protocol = "HTTPS"
  41. path = "/"
  42. matcher = "200-400"
  43. }
  44. stickiness {
  45. type = "lb_cookie"
  46. enabled = false
  47. }
  48. tags = merge( var.standard_tags, var.tags, )
  49. }
  50. resource "aws_autoscaling_attachment" "portal" {
  51. autoscaling_group_name = module.customer_portal_asg.this_autoscaling_group_name
  52. alb_target_group_arn = aws_alb_target_group.portal.arn
  53. }
  54. # Create a new alb listener ( certificate_arn wait for DNS cut over )
  55. resource "aws_alb_listener" "portal_https" {
  56. load_balancer_arn = aws_alb.portal.arn
  57. port = "443"
  58. protocol = "HTTPS"
  59. ssl_policy = "ELBSecurityPolicy-TLS-1-2-2017-01"
  60. #certificate_arn = aws_acm_certificate_validation.portal_cert.certificate_arn
  61. default_action {
  62. target_group_arn = aws_alb_target_group.portal.arn
  63. type = "forward"
  64. }
  65. }
  66. # resource "aws_alb_listener_certificate" "portal_https_cert" {
  67. # certificate_arn = data.aws_acm_certificate.portal_cert_v2.arn
  68. # listener_arn = aws_alb_listener.portal_https.arn
  69. # }
  70. # HTTPs Redirect
  71. resource "aws_lb_listener" "portal_https_redirect" {
  72. load_balancer_arn = aws_alb.portal.arn
  73. port = "80"
  74. protocol = "HTTP"
  75. default_action {
  76. type = "redirect"
  77. redirect {
  78. port = "443"
  79. protocol = "HTTPS"
  80. status_code = "HTTP_301"
  81. }
  82. }
  83. }
  84. #resource "aws_route53_record" "portal_cert_validation" {
  85. # name = "${aws_acm_certificate.portal_cert.domain_validation_options.0.resource_record_name}"
  86. # type = "${aws_acm_certificate.portal_cert.domain_validation_options.0.resource_record_type}"
  87. # zone_id = "${data.terraform_remote_state.infra.public_zone}"
  88. # records = ["${aws_acm_certificate.portal_cert.domain_validation_options.0.resource_record_value}"]
  89. # ttl = 60
  90. #}
  91. #resource "aws_acm_certificate" "portal_cert" {
  92. # domain_name = "portal.${data.terraform_remote_state.infra.private_zone2_name}"
  93. # validation_method = "DNS"
  94. #
  95. # tags = {
  96. # Name = "portal-cert-${terraform.workspace}"
  97. # Environment = "${terraform.workspace}"
  98. # }
  99. #}
  100. #data "aws_acm_certificate" "portal_cert_v2" {
  101. # domain = "portal.${terraform.workspace == "test" ? "xdrtest" : "xdr" }.accenturefederalcyber.com"
  102. # most_recent = true
  103. #}
  104. #resource "aws_acm_certificate_validation" "portal_cert" {
  105. # certificate_arn = "${aws_acm_certificate.portal_cert.arn}"
  106. # validation_record_fqdns = ["${aws_route53_record.portal_cert_validation.fqdn}"]
  107. #}
  108. #Wait to transfer the DNS until you are 100% ready!
  109. # DNS Alias for the LB
  110. # resource "aws_route53_record" "portal" {
  111. # zone_id = var.dns_info["public"]["zone_id"]
  112. # name = "portal.${var.dns_info["public"]["zone_id"]}"
  113. # records = [ aws_alb.portal.dns_name, ]
  114. # type = "CNAME"
  115. # ttl = 60
  116. # }
  117. # resource "aws_route53_record" "portal_private" {
  118. # zone_id = var.dns_info["private"]["zone_id"]
  119. # name = "portal.${var.dns_info["private"]["zone_id"]}"
  120. # type = "CNAME"
  121. # records = [ aws_alb.portal.dns_name, ]
  122. # ttl = 60
  123. # }
  124. #------------------------------------
  125. # Security Group
  126. #------------------------------------
  127. resource "aws_security_group" "customer_portal_alb" {
  128. name = "customer_portal_alb_inbound_sg"
  129. description = "Allow Customer Portal ALB HTTP Traffic Inbound"
  130. vpc_id = var.vpc_id
  131. ingress {
  132. from_port = 443
  133. to_port = 443
  134. protocol = "tcp"
  135. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  136. }
  137. #Allow viewing of test portal from home. We don't want world to view test portal.
  138. ingress {
  139. from_port = 443
  140. to_port = 443
  141. protocol = "tcp"
  142. cidr_blocks = flatten(local.portal_test_whitelist)
  143. }
  144. ## Needed for HTTPs redirect
  145. ingress {
  146. from_port = 80
  147. to_port = 80
  148. protocol = "tcp"
  149. cidr_blocks = [ var.environment == "test" ? "10.0.0.0/8" : "0.0.0.0/0", ]
  150. }
  151. }
  152. resource "aws_security_group_rule" "customer_portal_alb" {
  153. protocol = "tcp"
  154. type = "egress"
  155. from_port = 443
  156. to_port = 443
  157. security_group_id = aws_security_group.customer_portal_alb.id
  158. source_security_group_id = aws_security_group.customer_portal.id
  159. }