alb.tf 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #----------------------------------------------------------------------------
  2. # EXTERNAL APPLICATION LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_alb" "external" {
  5. name = "${var.instance_name}-alb-external-${var.environment}"
  6. security_groups = [aws_security_group.alb_server_external.id]
  7. internal = false # tfsec:ignore:aws-elb-alb-not-public The ALB requires Internet exposure
  8. subnets = var.subnets
  9. load_balancer_type = "application"
  10. drop_invalid_header_fields = true
  11. access_logs {
  12. bucket = "xdr-elb-${var.environment}"
  13. enabled = true
  14. }
  15. tags = merge(local.standard_tags, var.tags, { Name = "${var.instance_name}-alb-external-${var.environment}" })
  16. }
  17. # Create a new target group
  18. resource "aws_alb_target_group" "external" {
  19. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  20. # otherwise, you get stuck in `destroying` during routine changes.
  21. name_prefix = substr(var.instance_name, 0, 6)
  22. port = 3080
  23. protocol = "HTTPS"
  24. #deregistration_delay = "${local.lb_deregistration_delay}"
  25. vpc_id = var.vpc_id
  26. health_check {
  27. protocol = "HTTPS"
  28. port = "3080"
  29. path = "/web/login"
  30. matcher = "200-400"
  31. timeout = "4"
  32. interval = "5"
  33. }
  34. stickiness {
  35. type = "lb_cookie"
  36. enabled = true
  37. }
  38. tags = merge(local.standard_tags, var.tags)
  39. lifecycle {
  40. create_before_destroy = true
  41. }
  42. }
  43. resource "aws_lb_target_group_attachment" "external" {
  44. target_group_arn = aws_alb_target_group.external.arn
  45. target_id = aws_instance.instance.id
  46. port = 3080
  47. }
  48. # Create a new alb listener
  49. resource "aws_alb_listener" "https_external" {
  50. load_balancer_arn = aws_alb.external.arn
  51. port = "443"
  52. protocol = "HTTPS"
  53. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  54. certificate_arn = aws_acm_certificate.cert.arn
  55. default_action {
  56. target_group_arn = aws_alb_target_group.external.arn
  57. type = "forward"
  58. }
  59. }
  60. resource "aws_alb_listener" "alb_3080_external" {
  61. load_balancer_arn = aws_alb.external.arn
  62. port = "3080"
  63. protocol = "HTTPS"
  64. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  65. certificate_arn = aws_acm_certificate.cert.arn
  66. default_action {
  67. target_group_arn = aws_alb_target_group.external.arn
  68. type = "forward"
  69. }
  70. }
  71. resource "aws_lb_listener" "http_external" {
  72. load_balancer_arn = aws_alb.external.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. #----------------------------------------------------------------------------
  85. # DNS Entry
  86. #----------------------------------------------------------------------------
  87. module "public_dns_record_for_alb" {
  88. source = "../../submodules/dns/public_ALIAS_record"
  89. name = var.instance_name
  90. target_dns_name = aws_alb.external.dns_name
  91. target_zone_id = aws_alb.external.zone_id
  92. dns_info = var.dns_info
  93. providers = {
  94. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  95. }
  96. }
  97. #----------------------------------------------------------------------------
  98. # ALB Security Group
  99. #----------------------------------------------------------------------------
  100. resource "aws_security_group" "alb_server_external" {
  101. vpc_id = var.vpc_id
  102. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  103. # otherwise, you get stuck in `destroying` during routine changes.
  104. name_prefix = "${var.instance_name}-alb-sg-external"
  105. description = "Teleport LB SG"
  106. tags = merge(local.standard_tags, var.tags)
  107. lifecycle {
  108. create_before_destroy = true
  109. }
  110. }
  111. #----------------------------------------------------------------------------
  112. # INGRESS
  113. #----------------------------------------------------------------------------
  114. resource "aws_security_group_rule" "alb-http-in" {
  115. type = "ingress"
  116. description = "HTTP - Inbound"
  117. from_port = "80"
  118. to_port = "80"
  119. protocol = "tcp"
  120. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr
  121. security_group_id = aws_security_group.alb_server_external.id
  122. }
  123. resource "aws_security_group_rule" "alb-https-in" {
  124. type = "ingress"
  125. description = "HTTPS - Inbound"
  126. from_port = "443"
  127. to_port = "443"
  128. protocol = "tcp"
  129. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr
  130. security_group_id = aws_security_group.alb_server_external.id
  131. }
  132. resource "aws_security_group_rule" "alb-3080-in" {
  133. type = "ingress"
  134. description = "3080 TCP - Inbound"
  135. from_port = "3080"
  136. to_port = "3080"
  137. protocol = "tcp"
  138. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr
  139. security_group_id = aws_security_group.alb_server_external.id
  140. }
  141. #----------------------------------------------------------------------------
  142. # EGRESS
  143. #----------------------------------------------------------------------------
  144. resource "aws_security_group_rule" "alb_to_server" {
  145. type = "egress"
  146. description = "Allows the ALB to talk to the Sensu servers"
  147. from_port = 3080
  148. to_port = 3080
  149. protocol = "tcp"
  150. source_security_group_id = aws_security_group.instance.id
  151. security_group_id = aws_security_group.alb_server_external.id
  152. }