alb.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. name = "${var.instance_name}-alb-external"
  19. port = 443
  20. protocol = "HTTPS"
  21. #deregistration_delay = "${local.lb_deregistration_delay}"
  22. vpc_id = var.vpc_id
  23. health_check {
  24. protocol = "HTTPS"
  25. port = "443"
  26. path = "/"
  27. matcher = "200-400"
  28. timeout = "4"
  29. interval = "5"
  30. }
  31. stickiness {
  32. type = "lb_cookie"
  33. enabled = true
  34. }
  35. tags = merge(var.standard_tags, var.tags)
  36. }
  37. resource "aws_lb_target_group_attachment" "external" {
  38. target_group_arn = aws_alb_target_group.external.arn
  39. target_id = aws_instance.instance.id
  40. port = 443 # maybe 3080?
  41. }
  42. # Create a new alb listener
  43. resource "aws_alb_listener" "https_external" {
  44. load_balancer_arn = aws_alb.external.arn
  45. port = "443"
  46. protocol = "HTTPS"
  47. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  48. certificate_arn = aws_acm_certificate.cert.arn
  49. default_action {
  50. target_group_arn = aws_alb_target_group.external.arn
  51. type = "forward"
  52. }
  53. }
  54. resource "aws_lb_listener" "http_external" {
  55. load_balancer_arn = aws_alb.external.arn
  56. port = "80"
  57. protocol = "HTTP"
  58. default_action {
  59. type = "redirect"
  60. redirect {
  61. port = "443"
  62. protocol = "HTTPS"
  63. status_code = "HTTP_301"
  64. }
  65. }
  66. }
  67. # #########################
  68. # # DNS Entry
  69. module "public_dns_record_for_alb" {
  70. source = "../../submodules/dns/public_ALIAS_record"
  71. name = var.instance_name
  72. target_dns_name = aws_alb.external.dns_name
  73. target_zone_id = aws_alb.external.zone_id
  74. dns_info = var.dns_info
  75. providers = {
  76. aws.mdr-common-services-commercial = aws.mdr-common-services-commercial
  77. }
  78. }
  79. #----------------------------------------------------------------------------
  80. # ALB Security Group
  81. #----------------------------------------------------------------------------
  82. resource "aws_security_group" "alb_server_external" {
  83. vpc_id = var.vpc_id
  84. name = "${var.instance_name}-alb-sg-external"
  85. description = "Teleport LB SG"
  86. tags = merge(var.standard_tags, var.tags)
  87. }
  88. #----------------------------------------------------------------------------
  89. # INGRESS
  90. #----------------------------------------------------------------------------
  91. resource "aws_security_group_rule" "alb-http-in" {
  92. description = "HTTPS In"
  93. type = "ingress"
  94. from_port = "80"
  95. to_port = "80"
  96. protocol = "tcp"
  97. cidr_blocks = [ "0.0.0.0/0" ]
  98. security_group_id = aws_security_group.alb_server_external.id
  99. }
  100. resource "aws_security_group_rule" "alb-https-in" {
  101. description = "HTTPS In"
  102. type = "ingress"
  103. from_port = "443"
  104. to_port = "443"
  105. protocol = "tcp"
  106. cidr_blocks = [ "0.0.0.0/0" ]
  107. security_group_id = aws_security_group.alb_server_external.id
  108. }
  109. #----------------------------------------------------------------------------
  110. # EGRESS
  111. #----------------------------------------------------------------------------
  112. resource "aws_security_group_rule" "alb_to_server" {
  113. type = "egress"
  114. from_port = 443
  115. to_port = 443
  116. protocol = "tcp"
  117. source_security_group_id = aws_security_group.instance.id
  118. description = "Allows the ALB to talk to the Sensu servers"
  119. security_group_id = aws_security_group.alb_server_external.id
  120. }