security-groups.tf 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally Open
  21. security_group_id = aws_security_group.lb_server_external.id
  22. }
  23. resource "aws_security_group_rule" "allow_http_rediret" {
  24. count = var.redirect_80 ? 1 : 0
  25. description = "${var.name} - Allow from Any"
  26. type = "ingress"
  27. from_port = 80
  28. to_port = 80
  29. protocol = "tcp"
  30. cidr_blocks = ["0.0.0.0/0"] # tfsec:ignore:aws-vpc-no-public-ingress-sgr Intentionally Open
  31. security_group_id = aws_security_group.lb_server_external.id
  32. }
  33. #----------------------------------------------------------------------------
  34. # EGRESS
  35. #----------------------------------------------------------------------------
  36. resource "aws_security_group_rule" "alb_to_servers" {
  37. type = "egress"
  38. from_port = var.target_port
  39. to_port = var.target_port
  40. protocol = "tcp"
  41. source_security_group_id = var.target_security_group
  42. description = "${var.name} - Allows the ALB to talk to the servers"
  43. security_group_id = aws_security_group.lb_server_external.id
  44. }
  45. resource "aws_security_group_rule" "alb_to_health" {
  46. count = var.target_port != var.healthcheck_port ? 1 : 0
  47. type = "egress"
  48. from_port = var.healthcheck_port
  49. to_port = var.healthcheck_port
  50. protocol = "tcp"
  51. source_security_group_id = var.target_security_group
  52. description = "${var.name} - Allows the ALB to talk to the Health check"
  53. security_group_id = aws_security_group.lb_server_external.id
  54. }