alb.tf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 = "/"
  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_lb_listener" "http_external" {
  60. load_balancer_arn = aws_alb.external.arn
  61. port = "80"
  62. protocol = "HTTP"
  63. default_action {
  64. type = "redirect"
  65. redirect {
  66. port = "443"
  67. protocol = "HTTPS"
  68. status_code = "HTTP_301"
  69. }
  70. }
  71. }
  72. # #########################
  73. # # DNS Entry
  74. module "public_dns_record_for_alb" {
  75. source = "../../submodules/dns/public_ALIAS_record"
  76. name = var.instance_name
  77. target_dns_name = aws_alb.external.dns_name
  78. target_zone_id = aws_alb.external.zone_id
  79. dns_info = var.dns_info
  80. providers = {
  81. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  82. }
  83. }
  84. #----------------------------------------------------------------------------
  85. # ALB Security Group
  86. #----------------------------------------------------------------------------
  87. resource "aws_security_group" "alb_server_external" {
  88. vpc_id = var.vpc_id
  89. # use name_prefix instead of name and create-before-destroy on security groups and alb target groups to make future changes easier,
  90. # otherwise, you get stuck in `destroying` during routine changes.
  91. name_prefix = "${var.instance_name}-alb-sg-external"
  92. description = "Teleport LB SG"
  93. tags = merge(var.standard_tags, var.tags)
  94. lifecycle {
  95. create_before_destroy = true
  96. }
  97. }
  98. #----------------------------------------------------------------------------
  99. # INGRESS
  100. #----------------------------------------------------------------------------
  101. resource "aws_security_group_rule" "alb-http-in" {
  102. description = "HTTPS In"
  103. type = "ingress"
  104. from_port = "80"
  105. to_port = "80"
  106. protocol = "tcp"
  107. cidr_blocks = [ "0.0.0.0/0" ]
  108. security_group_id = aws_security_group.alb_server_external.id
  109. }
  110. resource "aws_security_group_rule" "alb-https-in" {
  111. description = "HTTPS In"
  112. type = "ingress"
  113. from_port = "443"
  114. to_port = "443"
  115. protocol = "tcp"
  116. cidr_blocks = [ "0.0.0.0/0" ]
  117. security_group_id = aws_security_group.alb_server_external.id
  118. }
  119. #----------------------------------------------------------------------------
  120. # EGRESS
  121. #----------------------------------------------------------------------------
  122. resource "aws_security_group_rule" "alb_to_server" {
  123. type = "egress"
  124. from_port = 443
  125. to_port = 443
  126. protocol = "tcp"
  127. source_security_group_id = aws_security_group.instance.id
  128. description = "Allows the ALB to talk to the Sensu servers"
  129. security_group_id = aws_security_group.alb_server_external.id
  130. }