elb-master.tf 3.5 KB

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