alb-internal.tf 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #----------------------------------------------------------------------------
  2. # EXTERNAL APPLICATION LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_alb" "internal" {
  5. name = "${var.instance_name}-alb-internal-${var.environment}"
  6. security_groups = [aws_security_group.alb_server_internal.id]
  7. internal = true
  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-internal-${var.environment}" })
  16. }
  17. # Create a new target group
  18. resource "aws_alb_target_group" "internal" {
  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" "internal" {
  44. target_group_arn = aws_alb_target_group.internal.arn
  45. target_id = aws_instance.instance.id
  46. port = 3080
  47. }
  48. # Create a new alb listener
  49. resource "aws_alb_listener" "https_internal" {
  50. load_balancer_arn = aws_alb.internal.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_internal.arn
  55. default_action {
  56. target_group_arn = aws_alb_target_group.internal.arn
  57. type = "forward"
  58. }
  59. }
  60. resource "aws_alb_listener" "alb_3080_internal" {
  61. load_balancer_arn = aws_alb.internal.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_internal.arn
  66. default_action {
  67. target_group_arn = aws_alb_target_group.internal.arn
  68. type = "forward"
  69. }
  70. }
  71. resource "aws_lb_listener" "http_internal" {
  72. load_balancer_arn = aws_alb.internal.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 "private_alb_dns_record" {
  87. source = "../../submodules/dns/private_CNAME_record"
  88. name = "${var.instance_name}-alb.${var.dns_info["private"]["zone"]}"
  89. target_dns_names = [aws_alb.internal.dns_name]
  90. dns_info = var.dns_info
  91. providers = {
  92. aws.c2 = aws.c2
  93. }
  94. }
  95. #----------------------------------------------------------------------------
  96. # ALB Security Group
  97. #----------------------------------------------------------------------------
  98. resource "aws_security_group" "alb_server_internal" {
  99. vpc_id = var.vpc_id
  100. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  101. # otherwise, you get stuck in `destroying` during routine changes.
  102. name_prefix = "${var.instance_name}-alb-sg-internal"
  103. description = "Teleport LB SG"
  104. tags = merge(local.standard_tags, var.tags)
  105. lifecycle {
  106. create_before_destroy = true
  107. }
  108. }
  109. #----------------------------------------------------------------------------
  110. # INGRESS
  111. #----------------------------------------------------------------------------
  112. resource "aws_security_group_rule" "alb-internal-http-in" {
  113. type = "ingress"
  114. description = "HTTP - Inbound"
  115. from_port = "80"
  116. to_port = "80"
  117. protocol = "tcp"
  118. cidr_blocks = ["10.0.0.0/8"]
  119. security_group_id = aws_security_group.alb_server_internal.id
  120. }
  121. resource "aws_security_group_rule" "alb-internal-https-in" {
  122. type = "ingress"
  123. description = "HTTPS - Inbound"
  124. from_port = "443"
  125. to_port = "443"
  126. protocol = "tcp"
  127. cidr_blocks = ["10.0.0.0/8"]
  128. security_group_id = aws_security_group.alb_server_internal.id
  129. }
  130. resource "aws_security_group_rule" "alb-internal-3080-in" {
  131. type = "ingress"
  132. description = "3080 TCP - Inbound"
  133. from_port = "3080"
  134. to_port = "3080"
  135. protocol = "tcp"
  136. cidr_blocks = ["10.0.0.0/8"]
  137. security_group_id = aws_security_group.alb_server_internal.id
  138. }
  139. #----------------------------------------------------------------------------
  140. # EGRESS
  141. #----------------------------------------------------------------------------
  142. resource "aws_security_group_rule" "alb_internal-to_server" {
  143. type = "egress"
  144. description = "Allows the ALB to talk to the Sensu servers"
  145. from_port = 3080
  146. to_port = 3080
  147. protocol = "tcp"
  148. source_security_group_id = aws_security_group.instance.id
  149. security_group_id = aws_security_group.alb_server_internal.id
  150. }