security_groups.tf 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #----------------------------------------------------------------------------
  2. # Load Balancer ALB Security Group
  3. #----------------------------------------------------------------------------
  4. resource "aws_security_group" "alb" {
  5. vpc_id = var.vpc_id
  6. name_prefix = "${local.name}-alb"
  7. description = "ALB SG for ${var.hostname}"
  8. tags = merge(local.tags, { "Name" : local.name })
  9. }
  10. #----------------------------------------------------------------------------
  11. # INGRESS
  12. #----------------------------------------------------------------------------
  13. resource "aws_security_group_rule" "http_from_internet" {
  14. type = "ingress"
  15. description = "HTTP - Inbound from Internet"
  16. from_port = "80"
  17. to_port = "80"
  18. protocol = "tcp"
  19. cidr_blocks = var.inbound_cidrs # tfsec:ignore:aws-vpc-no-public-ingress-sgr
  20. security_group_id = aws_security_group.alb.id
  21. }
  22. resource "aws_security_group_rule" "https_from_internet" {
  23. type = "ingress"
  24. description = "HTTPS - Inbound from Internet"
  25. from_port = "443"
  26. to_port = "443"
  27. protocol = "tcp"
  28. cidr_blocks = var.inbound_cidrs # tfsec:ignore:aws-vpc-no-public-ingress-sgr
  29. security_group_id = aws_security_group.alb.id
  30. }
  31. #----------------------------------------------------------------------------
  32. # EGRESS
  33. #----------------------------------------------------------------------------
  34. resource "aws_security_group_rule" "alb_to_server" {
  35. type = "egress"
  36. description = "${var.hostname} to the Server"
  37. from_port = var.server_port
  38. to_port = var.server_port
  39. protocol = "tcp"
  40. source_security_group_id = var.server_security_group
  41. security_group_id = aws_security_group.alb.id
  42. }
  43. #----------------------------------------------------------------------------
  44. # Server Security Group
  45. #----------------------------------------------------------------------------
  46. resource "aws_security_group_rule" "server_from_alb" {
  47. type = "ingress"
  48. description = "ALB to ${var.hostname}"
  49. from_port = var.server_port
  50. to_port = var.server_port
  51. protocol = "tcp"
  52. source_security_group_id = aws_security_group.alb.id
  53. security_group_id = var.server_security_group
  54. }