alb.tf 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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
  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-external-${var.environment}" })
  15. }
  16. # Create a new target group
  17. resource "aws_alb_target_group" "external" {
  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" "external" {
  43. target_group_arn = aws_alb_target_group.external.arn
  44. target_id = aws_instance.instance.id
  45. port = 3080
  46. }
  47. # Create a new alb listener
  48. resource "aws_alb_listener" "https_external" {
  49. load_balancer_arn = aws_alb.external.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.arn
  54. default_action {
  55. target_group_arn = aws_alb_target_group.external.arn
  56. type = "forward"
  57. }
  58. }
  59. resource "aws_alb_listener" "alb_3080_external" {
  60. load_balancer_arn = aws_alb.external.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.arn
  65. default_action {
  66. target_group_arn = aws_alb_target_group.external.arn
  67. type = "forward"
  68. }
  69. }
  70. resource "aws_lb_listener" "http_external" {
  71. load_balancer_arn = aws_alb.external.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 "public_dns_record_for_alb" {
  86. source = "../../submodules/dns/public_ALIAS_record"
  87. name = var.instance_name
  88. target_dns_name = aws_alb.external.dns_name
  89. target_zone_id = aws_alb.external.zone_id
  90. dns_info = var.dns_info
  91. providers = {
  92. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  93. }
  94. }
  95. #----------------------------------------------------------------------------
  96. # ALB Security Group
  97. #----------------------------------------------------------------------------
  98. resource "aws_security_group" "alb_server_external" {
  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-external"
  103. description = "Teleport LB SG"
  104. tags = merge(var.standard_tags, var.tags)
  105. lifecycle {
  106. create_before_destroy = true
  107. }
  108. }
  109. #----------------------------------------------------------------------------
  110. # INGRESS
  111. #----------------------------------------------------------------------------
  112. resource "aws_security_group_rule" "alb-http-in" {
  113. description = "HTTPS In"
  114. type = "ingress"
  115. from_port = "80"
  116. to_port = "80"
  117. protocol = "tcp"
  118. cidr_blocks = [ "0.0.0.0/0" ]
  119. security_group_id = aws_security_group.alb_server_external.id
  120. }
  121. resource "aws_security_group_rule" "alb-https-in" {
  122. description = "HTTPS In"
  123. type = "ingress"
  124. from_port = "443"
  125. to_port = "443"
  126. protocol = "tcp"
  127. cidr_blocks = [ "0.0.0.0/0" ]
  128. security_group_id = aws_security_group.alb_server_external.id
  129. }
  130. resource "aws_security_group_rule" "alb-3080-in" {
  131. description = "3080 In"
  132. type = "ingress"
  133. from_port = "3080"
  134. to_port = "3080"
  135. protocol = "tcp"
  136. cidr_blocks = [ "0.0.0.0/0" ]
  137. security_group_id = aws_security_group.alb_server_external.id
  138. }
  139. #----------------------------------------------------------------------------
  140. # EGRESS
  141. #----------------------------------------------------------------------------
  142. resource "aws_security_group_rule" "alb_to_server" {
  143. type = "egress"
  144. from_port = 3080
  145. to_port = 3080
  146. protocol = "tcp"
  147. source_security_group_id = aws_security_group.instance.id
  148. description = "Allows the ALB to talk to the Sensu servers"
  149. security_group_id = aws_security_group.alb_server_external.id
  150. }