alb.tf 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #----------------------------------------------------------------------------
  2. # INTERNAL LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_alb" "phantom_internal" {
  5. name = "phantom-alb-internal-${var.environment}"
  6. security_groups = [aws_security_group.phantom_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 = "phantom-alb-internal-${var.environment}" })
  17. }
  18. # Create a new target group
  19. resource "aws_alb_target_group" "phantom_internal" {
  20. name = "phantom-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" "phantom_internal" {
  41. count = local.instance_count
  42. target_group_arn = aws_alb_target_group.phantom_internal.arn
  43. target_id = aws_instance.phantom-server-instance[count.index].id
  44. port = 443
  45. }
  46. # Create a new alb listener
  47. resource "aws_alb_listener" "phantom_https_internal" {
  48. load_balancer_arn = aws_alb.phantom_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.phantom_internal.arn
  55. type = "forward"
  56. }
  57. }
  58. resource "aws_lb_listener" "phantom_listener_http" {
  59. load_balancer_arn = aws_alb.phantom_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. module "private_dns_record" {
  74. source = "../../submodules/dns/private_CNAME_record"
  75. name = "phantom"
  76. target_dns_names = [aws_alb.phantom_internal.dns_name]
  77. dns_info = var.dns_info
  78. providers = {
  79. aws.c2 = aws.c2
  80. }
  81. }
  82. #----------------------------------------------------------------------------
  83. # ALB Security Group
  84. #----------------------------------------------------------------------------
  85. resource "aws_security_group" "phantom_alb_internal" {
  86. vpc_id = var.vpc_id
  87. name = "phantom-alb-sg-internal"
  88. description = "ALB for Phantom"
  89. tags = merge(local.standard_tags, var.tags)
  90. }
  91. #----------------------------------------------------------------------------
  92. # INGRESS
  93. #----------------------------------------------------------------------------
  94. resource "aws_security_group_rule" "http_from_local" {
  95. description = "HTTP inbound from Internet"
  96. type = "ingress"
  97. from_port = "80"
  98. to_port = "80"
  99. protocol = "tcp"
  100. cidr_blocks = ["10.0.0.0/8"]
  101. security_group_id = aws_security_group.phantom_alb_internal.id
  102. }
  103. resource "aws_security_group_rule" "https_from_local" {
  104. description = "HTTPS inbound from Internet"
  105. type = "ingress"
  106. from_port = "443"
  107. to_port = "443"
  108. protocol = "tcp"
  109. cidr_blocks = ["10.0.0.0/8"]
  110. security_group_id = aws_security_group.phantom_alb_internal.id
  111. }
  112. #----------------------------------------------------------------------------
  113. # EGRESS
  114. #----------------------------------------------------------------------------
  115. resource "aws_security_group_rule" "phantom_alb_to_server" {
  116. description = "HTTPS to the Server"
  117. type = "egress"
  118. from_port = "443"
  119. to_port = "443"
  120. protocol = "tcp"
  121. source_security_group_id = aws_security_group.phantom_server.id
  122. security_group_id = aws_security_group.phantom_alb_internal.id
  123. }