alb.tf 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #----------------------------------------------------------------------------
  2. # INTERNAL LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_alb" "internal" {
  5. name = "${local.server_name_stem}-alb-internal-${var.environment}"
  6. security_groups = [aws_security_group.alb_internal.id]
  7. internal = true
  8. subnets = var.public_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. idle_timeout = 1200
  16. tags = merge(local.standard_tags, var.tags, { Name = "${local.server_name_stem}-alb-internal-${var.environment}" })
  17. }
  18. # Create a new target group
  19. resource "aws_alb_target_group" "internal" {
  20. name = "${local.server_name_stem}-alb-targets"
  21. port = 443
  22. protocol = "HTTPS"
  23. vpc_id = var.vpc_id
  24. health_check {
  25. protocol = "HTTPS"
  26. port = "443"
  27. path = "/"
  28. matcher = "200,302"
  29. timeout = "4"
  30. interval = "5"
  31. unhealthy_threshold = 2
  32. healthy_threshold = 2
  33. }
  34. #stickiness {
  35. # type = "lb_cookie"
  36. # enabled = false
  37. #}
  38. tags = merge(local.standard_tags, var.tags)
  39. }
  40. resource "aws_lb_target_group_attachment" "internal" {
  41. count = local.instance_count
  42. target_group_arn = aws_alb_target_group.internal.arn
  43. target_id = aws_instance.instance[count.index].id
  44. port = 443
  45. }
  46. # Create a new alb listener
  47. resource "aws_alb_listener" "https_internal" {
  48. load_balancer_arn = aws_alb.internal.arn
  49. port = "443"
  50. protocol = "HTTPS"
  51. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  52. certificate_arn = aws_acm_certificate.cert_private.arn
  53. default_action {
  54. target_group_arn = aws_alb_target_group.internal.arn
  55. type = "forward"
  56. }
  57. }
  58. resource "aws_lb_listener" "listener_http" {
  59. load_balancer_arn = aws_alb.internal.arn
  60. port = "80"
  61. protocol = "HTTP"
  62. default_action {
  63. type = "redirect"
  64. redirect {
  65. port = "443"
  66. protocol = "HTTPS"
  67. status_code = "HTTP_301"
  68. }
  69. }
  70. }
  71. #----------------------------------------------------------------------------
  72. # DNS Entry
  73. #----------------------------------------------------------------------------
  74. module "alb_private_dns_record" {
  75. source = "../../submodules/dns/private_CNAME_record"
  76. name = local.server_name_stem
  77. target_dns_names = [aws_alb.internal.dns_name]
  78. dns_info = var.dns_info
  79. providers = {
  80. aws.c2 = aws.c2
  81. }
  82. }
  83. #----------------------------------------------------------------------------
  84. # ALB Security Group
  85. #----------------------------------------------------------------------------
  86. resource "aws_security_group" "alb_internal" {
  87. vpc_id = var.vpc_id
  88. name = "${local.server_name_stem}-alb-sg-internal"
  89. description = "ALB for ${local.server_name_stem}"
  90. tags = merge(local.standard_tags, var.tags)
  91. }
  92. #----------------------------------------------------------------------------
  93. # INGRESS
  94. #----------------------------------------------------------------------------
  95. resource "aws_security_group_rule" "internal_http_from_local" {
  96. type = "ingress"
  97. description = "HTTP - Inbound from internal VPCs"
  98. from_port = "80"
  99. to_port = "80"
  100. protocol = "tcp"
  101. cidr_blocks = local.supernets
  102. security_group_id = aws_security_group.alb_internal.id
  103. }
  104. resource "aws_security_group_rule" "internal_https_from_local" {
  105. type = "ingress"
  106. description = "HTTPS - Inbound from internal_vpc"
  107. from_port = "443"
  108. to_port = "443"
  109. protocol = "tcp"
  110. cidr_blocks = local.supernets
  111. security_group_id = aws_security_group.alb_internal.id
  112. }
  113. #----------------------------------------------------------------------------
  114. # EGRESS
  115. #----------------------------------------------------------------------------
  116. resource "aws_security_group_rule" "internal_alb_to_server" {
  117. type = "egress"
  118. description = "HTTPS to the Server"
  119. from_port = "443"
  120. to_port = "443"
  121. protocol = "tcp"
  122. source_security_group_id = aws_security_group.instance.id
  123. security_group_id = aws_security_group.alb_internal.id
  124. }