alb.tf 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #----------------------------------------------------------------------------
  2. # INTERNAL LB
  3. #----------------------------------------------------------------------------
  4. resource "aws_alb" "vmray_internal" {
  5. name = "vmray-alb-internal-${var.environment}"
  6. security_groups = [ aws_security_group.vmray_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 = "vmray-alb-internal-${var.environment}" })
  16. }
  17. # Create a new target group
  18. resource "aws_alb_target_group" "vmray_internal" {
  19. name = "vmray-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" "vmray_internal" {
  40. target_group_arn = aws_alb_target_group.vmray_internal.arn
  41. target_id = aws_instance.vmray-server-instance.id
  42. port = 443
  43. }
  44. # Create a new alb listener
  45. resource "aws_alb_listener" "vmray_https_internal" {
  46. load_balancer_arn = aws_alb.vmray_internal.arn
  47. port = "443"
  48. protocol = "HTTPS"
  49. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2019-08" # PFS, TLS1.2, most "restrictive" policy (took awhile to find that)
  50. certificate_arn = aws_acm_certificate.cert_private.arn
  51. default_action {
  52. target_group_arn = aws_alb_target_group.vmray_internal.arn
  53. type = "forward"
  54. }
  55. }
  56. resource "aws_lb_listener" "vmray_listener_http" {
  57. load_balancer_arn = aws_alb.vmray_internal.arn
  58. port = "80"
  59. protocol = "HTTP"
  60. default_action {
  61. type = "redirect"
  62. redirect {
  63. port = "443"
  64. protocol = "HTTPS"
  65. status_code = "HTTP_301"
  66. }
  67. }
  68. }
  69. # #########################
  70. # # DNS Entry
  71. module "private_dns_record" {
  72. source = "../../submodules/dns/private_CNAME_record"
  73. name = "vmray"
  74. target_dns_names = [ aws_alb.vmray_internal.dns_name ]
  75. dns_info = var.dns_info
  76. providers = {
  77. aws.c2 = aws.c2
  78. }
  79. }
  80. #----------------------------------------------------------------------------
  81. # ALB Security Group
  82. #----------------------------------------------------------------------------
  83. resource "aws_security_group" "vmray_alb_internal" {
  84. vpc_id = var.vpc_id
  85. name = "vmray-alb-sg-internal"
  86. description = "ALB for Phantom"
  87. tags = merge(var.standard_tags, var.tags)
  88. }
  89. #----------------------------------------------------------------------------
  90. # INGRESS
  91. #----------------------------------------------------------------------------
  92. resource "aws_security_group_rule" "http_from_local" {
  93. description = "HTTP inbound from Internet"
  94. type = "ingress"
  95. from_port = "80"
  96. to_port = "80"
  97. protocol = "tcp"
  98. cidr_blocks = concat(var.cidr_map["vpc-access"], var.cidr_map["vpc-private-services"])
  99. security_group_id = aws_security_group.vmray_alb_internal.id
  100. }
  101. resource "aws_security_group_rule" "https_from_local" {
  102. description = "HTTPS inbound from Internet"
  103. type = "ingress"
  104. from_port = "443"
  105. to_port = "443"
  106. protocol = "tcp"
  107. cidr_blocks = concat(var.cidr_map["vpc-access"], var.cidr_map["vpc-private-services"])
  108. security_group_id = aws_security_group.vmray_alb_internal.id
  109. }
  110. #----------------------------------------------------------------------------
  111. # EGRESS
  112. #----------------------------------------------------------------------------
  113. resource "aws_security_group_rule" "vmray_alb_to_server" {
  114. description = "HTTPS to the Server"
  115. type = "egress"
  116. from_port = "443"
  117. to_port = "443"
  118. protocol = "tcp"
  119. source_security_group_id = aws_security_group.vmray_server_sg.id
  120. security_group_id = aws_security_group.vmray_alb_internal.id
  121. }