alb.tf 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. module "public_dns_record_for_alb" {
  87. source = "../../submodules/dns/public_ALIAS_record"
  88. name = var.instance_name
  89. target_dns_name = aws_alb.external.dns_name
  90. target_zone_id = aws_alb.external.zone_id
  91. dns_info = var.dns_info
  92. providers = {
  93. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  94. }
  95. }
  96. #----------------------------------------------------------------------------
  97. # ALB Security Group
  98. #----------------------------------------------------------------------------
  99. resource "aws_security_group" "alb_server_external" {
  100. vpc_id = var.vpc_id
  101. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  102. # otherwise, you get stuck in `destroying` during routine changes.
  103. name_prefix = "${var.instance_name}-alb-sg-external"
  104. description = "Teleport LB SG"
  105. tags = merge(local.standard_tags, var.tags)
  106. lifecycle {
  107. create_before_destroy = true
  108. }
  109. }
  110. #----------------------------------------------------------------------------
  111. # INGRESS
  112. #----------------------------------------------------------------------------
  113. resource "aws_security_group_rule" "alb-http-in" {
  114. description = "HTTPS In"
  115. type = "ingress"
  116. from_port = "80"
  117. to_port = "80"
  118. protocol = "tcp"
  119. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-ingress-sgr
  120. security_group_id = aws_security_group.alb_server_external.id
  121. }
  122. resource "aws_security_group_rule" "alb-https-in" {
  123. description = "HTTPS In"
  124. type = "ingress"
  125. from_port = "443"
  126. to_port = "443"
  127. protocol = "tcp"
  128. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-ingress-sgr
  129. security_group_id = aws_security_group.alb_server_external.id
  130. }
  131. resource "aws_security_group_rule" "alb-3080-in" {
  132. description = "3080 In"
  133. type = "ingress"
  134. from_port = "3080"
  135. to_port = "3080"
  136. protocol = "tcp"
  137. cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:aws-vpc-no-public-ingress-sgr
  138. security_group_id = aws_security_group.alb_server_external.id
  139. }
  140. #----------------------------------------------------------------------------
  141. # EGRESS
  142. #----------------------------------------------------------------------------
  143. resource "aws_security_group_rule" "alb_to_server" {
  144. type = "egress"
  145. from_port = 3080
  146. to_port = 3080
  147. protocol = "tcp"
  148. source_security_group_id = aws_security_group.instance.id
  149. description = "Allows the ALB to talk to the Sensu servers"
  150. security_group_id = aws_security_group.alb_server_external.id
  151. }