alb.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. access_logs {
  11. bucket = "xdr-elb-${ var.environment }"
  12. enabled = true
  13. }
  14. idle_timeout = 1200
  15. tags = merge(var.standard_tags, var.tags, { Name = "phantom-alb-internal-${var.environment}" })
  16. }
  17. # Create a new target group
  18. resource "aws_alb_target_group" "phantom_internal" {
  19. name = "phantom-alb-targets"
  20. port = 443
  21. protocol = "HTTPS"
  22. vpc_id = var.vpc_id
  23. health_check {
  24. protocol = "HTTPS"
  25. port = "443"
  26. path = "/"
  27. matcher = "200,302"
  28. timeout = "4"
  29. interval = "5"
  30. unhealthy_threshold = 2
  31. healthy_threshold = 2
  32. }
  33. #stickiness {
  34. # type = "lb_cookie"
  35. # enabled = false
  36. #}
  37. tags = merge(var.standard_tags, var.tags)
  38. }
  39. resource "aws_lb_target_group_attachment" "phantom_internal" {
  40. count = var.phantom_instance_count
  41. target_group_arn = aws_alb_target_group.phantom_internal.arn
  42. target_id = aws_instance.phantom-server-instance[count.index].id
  43. port = 443
  44. }
  45. # Create a new alb listener
  46. resource "aws_alb_listener" "phantom_https_internal" {
  47. load_balancer_arn = aws_alb.phantom_internal.arn
  48. port = "443"
  49. protocol = "HTTPS"
  50. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  51. certificate_arn = aws_acm_certificate.cert_private.arn
  52. default_action {
  53. target_group_arn = aws_alb_target_group.phantom_internal.arn
  54. type = "forward"
  55. }
  56. }
  57. resource "aws_lb_listener" "phantom_listener_http" {
  58. load_balancer_arn = aws_alb.phantom_internal.arn
  59. port = "80"
  60. protocol = "HTTP"
  61. default_action {
  62. type = "redirect"
  63. redirect {
  64. port = "443"
  65. protocol = "HTTPS"
  66. status_code = "HTTP_301"
  67. }
  68. }
  69. }
  70. # #########################
  71. # # DNS Entry
  72. module "private_dns_record" {
  73. source = "../../submodules/dns/private_CNAME_record"
  74. name = "phantom"
  75. target_dns_names = [ aws_alb.phantom_internal.dns_name ]
  76. dns_info = var.dns_info
  77. providers = {
  78. aws.c2 = aws.c2
  79. }
  80. }
  81. #----------------------------------------------------------------------------
  82. # ALB Security Group
  83. #----------------------------------------------------------------------------
  84. resource "aws_security_group" "phantom_alb_internal" {
  85. vpc_id = var.vpc_id
  86. name = "phantom-alb-sg-internal"
  87. description = "ALB for Phantom"
  88. tags = merge(var.standard_tags, var.tags)
  89. }
  90. #----------------------------------------------------------------------------
  91. # INGRESS
  92. #----------------------------------------------------------------------------
  93. resource "aws_security_group_rule" "http_from_local" {
  94. description = "HTTP inbound from Internet"
  95. type = "ingress"
  96. from_port = "80"
  97. to_port = "80"
  98. protocol = "tcp"
  99. cidr_blocks = [ "10.0.0.0/8" ]
  100. security_group_id = aws_security_group.phantom_alb_internal.id
  101. }
  102. resource "aws_security_group_rule" "https_from_local" {
  103. description = "HTTPS inbound from Internet"
  104. type = "ingress"
  105. from_port = "443"
  106. to_port = "443"
  107. protocol = "tcp"
  108. cidr_blocks = [ "10.0.0.0/8" ]
  109. security_group_id = aws_security_group.phantom_alb_internal.id
  110. }
  111. #----------------------------------------------------------------------------
  112. # EGRESS
  113. #----------------------------------------------------------------------------
  114. resource "aws_security_group_rule" "phantom_alb_to_server" {
  115. description = "HTTPS to the Server"
  116. type = "egress"
  117. from_port = "443"
  118. to_port = "443"
  119. protocol = "tcp"
  120. source_security_group_id = aws_security_group.phantom_server.id
  121. security_group_id = aws_security_group.phantom_alb_internal.id
  122. }