security-groups.tf 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #----------------------------------------------------------------------------
  2. # ALB Security Group
  3. #----------------------------------------------------------------------------
  4. resource "aws_security_group" "lb_server_external" {
  5. vpc_id = var.vpc_id
  6. name_prefix = "${var.name}-alb-sg-external"
  7. description = "${var.name} LB SG"
  8. tags = var.tags
  9. }
  10. #----------------------------------------------------------------------------
  11. # INGRESS
  12. #----------------------------------------------------------------------------
  13. resource "aws_security_group_rule" "allow_from_any" {
  14. count = var.allow_from_any ? 1 : 0
  15. description = "${var.name} - Allow from Any"
  16. type = "ingress"
  17. from_port = var.listener_port
  18. to_port = var.listener_port
  19. protocol = "tcp"
  20. cidr_blocks = [ "0.0.0.0/0" ]
  21. security_group_id = aws_security_group.lb_server_external.id
  22. }
  23. #----------------------------------------------------------------------------
  24. # EGRESS
  25. #----------------------------------------------------------------------------
  26. resource "aws_security_group_rule" "alb_to_servers" {
  27. type = "egress"
  28. from_port = var.target_port
  29. to_port = var.target_port
  30. protocol = "tcp"
  31. source_security_group_id = var.target_security_group
  32. description = "${var.name} - Allows the ALB to talk to the servers"
  33. security_group_id = aws_security_group.lb_server_external.id
  34. }
  35. resource "aws_security_group_rule" "alb_to_health" {
  36. count = var.target_port != var.healthcheck_port ? 1 : 0
  37. type = "egress"
  38. from_port = var.healthcheck_port
  39. to_port = var.healthcheck_port
  40. protocol = "tcp"
  41. source_security_group_id = var.target_security_group
  42. description = "${var.name} - Allows the ALB to talk to the Health check"
  43. security_group_id = aws_security_group.lb_server_external.id
  44. }