elb-master.tf 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. resource "aws_lb" "alsi-master-alb" {
  2. # checkov:skip=CKV_AWS_150: Skip deletion protection - Test env
  3. name = "${var.prefix}-alsi-master-alb"
  4. internal = true
  5. load_balancer_type = "application"
  6. drop_invalid_header_fields = true
  7. security_groups = [aws_security_group.alsi-master-alb-sg.id]
  8. # Note, changing subnets results in recreation of the resource
  9. subnets = var.subnets
  10. enable_cross_zone_load_balancing = true
  11. access_logs {
  12. bucket = "xdr-elb-${var.environment}"
  13. enabled = true
  14. }
  15. tags = merge(local.standard_tags, var.tags)
  16. }
  17. #########################
  18. # Listeners
  19. resource "aws_lb_listener" "alsi-master-alb-listener-https" {
  20. load_balancer_arn = aws_lb.alsi-master-alb.arn
  21. port = "443"
  22. protocol = "HTTPS"
  23. ssl_policy = "ELBSecurityPolicy-FS-1-2-Res-2020-10" # PFS, TLS1.2, and GCM; most "restrictive" policy
  24. certificate_arn = aws_acm_certificate.cert_master.arn
  25. default_action {
  26. type = "forward"
  27. target_group_arn = aws_lb_target_group.alsi-master-alb-target-9000.arn
  28. }
  29. }
  30. # Only alb's can redirect
  31. resource "aws_lb_listener" "alsi-master-alb-listener-http" {
  32. load_balancer_arn = aws_lb.alsi-master-alb.arn
  33. port = "80"
  34. protocol = "HTTP"
  35. default_action {
  36. type = "redirect"
  37. redirect {
  38. port = "443"
  39. protocol = "HTTPS"
  40. status_code = "HTTP_301"
  41. }
  42. }
  43. }
  44. #########################
  45. # Targets
  46. resource "aws_lb_target_group" "alsi-master-alb-target-9000" {
  47. name = "${var.prefix}-alsi-master-9000"
  48. port = 9000
  49. protocol = "HTTPS"
  50. target_type = "instance"
  51. vpc_id = var.vpc_id
  52. tags = merge(local.standard_tags, var.tags)
  53. health_check {
  54. enabled = true
  55. path = "/api/v1/health"
  56. port = 9000
  57. protocol = "HTTPS"
  58. }
  59. }
  60. resource "aws_lb_target_group_attachment" "alsi-master-alb-target-9000-instance" {
  61. target_group_arn = aws_lb_target_group.alsi-master-alb-target-9000.arn
  62. target_id = aws_instance.master.id
  63. port = 9000
  64. }
  65. #----------------------------------------------------------------------------
  66. # Security Group for ALB
  67. #----------------------------------------------------------------------------
  68. resource "aws_security_group" "alsi-master-alb-sg" {
  69. name_prefix = "${var.prefix}-alsi-master-alb-sg"
  70. lifecycle { create_before_destroy = true } # handle updates gracefully
  71. description = "Security Group for the Cribl ALB"
  72. vpc_id = var.vpc_id
  73. tags = merge(local.standard_tags, var.tags)
  74. }
  75. #----------------------------------------------------------------------------
  76. # INGRESS
  77. #----------------------------------------------------------------------------
  78. resource "aws_security_group_rule" "alsi-master-alb-https-in" {
  79. type = "ingress"
  80. description = "HTTPS - Inbound"
  81. from_port = 443
  82. to_port = 443
  83. protocol = "tcp"
  84. cidr_blocks = local.cidr_map["vpc-access"]
  85. security_group_id = aws_security_group.alsi-master-alb-sg.id
  86. }
  87. resource "aws_security_group_rule" "alsi-master-http-in" {
  88. # Port 80 is open as a redirect to 443
  89. type = "ingress"
  90. description = "HTTP redirect HTTPS - Inbound"
  91. from_port = 80
  92. to_port = 80
  93. protocol = "tcp"
  94. cidr_blocks = local.cidr_map["vpc-access"]
  95. security_group_id = aws_security_group.alsi-master-alb-sg.id
  96. }
  97. #----------------------------------------------------------------------------
  98. # EGRESS
  99. #----------------------------------------------------------------------------
  100. resource "aws_security_group_rule" "alsi-master-alb-9000-out" {
  101. type = "egress"
  102. description = "9000 - Outbound"
  103. from_port = 9000
  104. to_port = 9000
  105. protocol = "tcp"
  106. source_security_group_id = aws_security_group.alsi_master_security_group.id
  107. security_group_id = aws_security_group.alsi-master-alb-sg.id
  108. }
  109. #----------------------------------------------------------------------------
  110. # DNS Entry
  111. #----------------------------------------------------------------------------
  112. resource "aws_route53_record" "alsi_master_alb" {
  113. zone_id = var.dns_info["private"]["zone_id"]
  114. name = "${var.prefix}-alsi"
  115. type = "CNAME"
  116. records = [aws_lb.alsi-master-alb.dns_name]
  117. ttl = "60"
  118. provider = aws.c2
  119. }