alb-internal.tf 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. access_logs {
  11. bucket = "xdr-elb-${ var.environment }"
  12. enabled = true
  13. }
  14. tags = merge(var.standard_tags, var.tags, { Name = "${var.instance_name}-alb-internal-${var.environment}" })
  15. }
  16. # Create a new target group
  17. resource "aws_alb_target_group" "internal" {
  18. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  19. # otherwise, you get stuck in `destroying` during routine changes.
  20. name_prefix = substr(var.instance_name, 0, 6)
  21. port = 3080
  22. protocol = "HTTPS"
  23. #deregistration_delay = "${local.lb_deregistration_delay}"
  24. vpc_id = var.vpc_id
  25. health_check {
  26. protocol = "HTTPS"
  27. port = "3080"
  28. path = "/web/login"
  29. matcher = "200-400"
  30. timeout = "4"
  31. interval = "5"
  32. }
  33. stickiness {
  34. type = "lb_cookie"
  35. enabled = true
  36. }
  37. tags = merge(var.standard_tags, var.tags)
  38. lifecycle {
  39. create_before_destroy = true
  40. }
  41. }
  42. resource "aws_lb_target_group_attachment" "internal" {
  43. target_group_arn = aws_alb_target_group.internal.arn
  44. target_id = aws_instance.instance.id
  45. port = 3080
  46. }
  47. # Create a new alb listener
  48. resource "aws_alb_listener" "https_internal" {
  49. load_balancer_arn = aws_alb.internal.arn
  50. port = "443"
  51. protocol = "HTTPS"
  52. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  53. certificate_arn = aws_acm_certificate.cert_internal.arn
  54. default_action {
  55. target_group_arn = aws_alb_target_group.internal.arn
  56. type = "forward"
  57. }
  58. }
  59. resource "aws_alb_listener" "alb_3080_internal" {
  60. load_balancer_arn = aws_alb.internal.arn
  61. port = "3080"
  62. protocol = "HTTPS"
  63. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  64. certificate_arn = aws_acm_certificate.cert_internal.arn
  65. default_action {
  66. target_group_arn = aws_alb_target_group.internal.arn
  67. type = "forward"
  68. }
  69. }
  70. resource "aws_lb_listener" "http_internal" {
  71. load_balancer_arn = aws_alb.internal.arn
  72. port = "80"
  73. protocol = "HTTP"
  74. default_action {
  75. type = "redirect"
  76. redirect {
  77. port = "443"
  78. protocol = "HTTPS"
  79. status_code = "HTTP_301"
  80. }
  81. }
  82. }
  83. # #########################
  84. # # DNS Entry
  85. module "private_alb_dns_record" {
  86. source = "../../submodules/dns/private_CNAME_record"
  87. name = "${var.instance_name}-alb.${var.dns_info["private"]["zone"]}"
  88. target_dns_names = [ aws_alb.internal.dns_name ]
  89. dns_info = var.dns_info
  90. providers = {
  91. aws.c2 = aws.c2
  92. }
  93. }
  94. #----------------------------------------------------------------------------
  95. # ALB Security Group
  96. #----------------------------------------------------------------------------
  97. resource "aws_security_group" "alb_server_internal" {
  98. vpc_id = var.vpc_id
  99. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  100. # otherwise, you get stuck in `destroying` during routine changes.
  101. name_prefix = "${var.instance_name}-alb-sg-internal"
  102. description = "Teleport LB SG"
  103. tags = merge(var.standard_tags, var.tags)
  104. lifecycle {
  105. create_before_destroy = true
  106. }
  107. }
  108. #----------------------------------------------------------------------------
  109. # INGRESS
  110. #----------------------------------------------------------------------------
  111. resource "aws_security_group_rule" "alb-internal-http-in" {
  112. description = "HTTPS In"
  113. type = "ingress"
  114. from_port = "80"
  115. to_port = "80"
  116. protocol = "tcp"
  117. cidr_blocks = [ "10.0.0.0/8" ]
  118. security_group_id = aws_security_group.alb_server_internal.id
  119. }
  120. resource "aws_security_group_rule" "alb-internal-https-in" {
  121. description = "HTTPS In"
  122. type = "ingress"
  123. from_port = "443"
  124. to_port = "443"
  125. protocol = "tcp"
  126. cidr_blocks = [ "10.0.0.0/8" ]
  127. security_group_id = aws_security_group.alb_server_internal.id
  128. }
  129. resource "aws_security_group_rule" "alb-internal-3080-in" {
  130. description = "3080 In"
  131. type = "ingress"
  132. from_port = "3080"
  133. to_port = "3080"
  134. protocol = "tcp"
  135. cidr_blocks = [ "10.0.0.0/8" ]
  136. security_group_id = aws_security_group.alb_server_internal.id
  137. }
  138. #----------------------------------------------------------------------------
  139. # EGRESS
  140. #----------------------------------------------------------------------------
  141. resource "aws_security_group_rule" "alb_internal-to_server" {
  142. type = "egress"
  143. from_port = 3080
  144. to_port = 3080
  145. protocol = "tcp"
  146. source_security_group_id = aws_security_group.instance.id
  147. description = "Allows the ALB to talk to the Sensu servers"
  148. security_group_id = aws_security_group.alb_server_internal.id
  149. }