variables.tf 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. variable "layer" {
  2. type = string
  3. description = "Stack layer, used in names (for example access)."
  4. }
  5. variable "region" {
  6. type = string
  7. description = "AWS region name (for example us-east-1). Used in names."
  8. }
  9. variable "purpose" {
  10. type = string
  11. description = "Role in the group name (for example connector)."
  12. }
  13. variable "vpc_id" {
  14. type = string
  15. description = "VPC the security group belongs to. From the vpc module's id."
  16. }
  17. variable "extra_outbound_rules" {
  18. type = list(object({
  19. protocol = string
  20. port_range = string
  21. }))
  22. default = []
  23. description = "Outbound rules to any IPv4 address, appended to the five defaults. port_range is a port (443), a range (30000-31000) or \"all\" (ports 0-65535 of that one protocol)."
  24. validation {
  25. condition = alltrue([
  26. for r in var.extra_outbound_rules :
  27. contains(["tcp", "udp"], r.protocol) && (r.port_range == "all" || try(regex("^(0|[1-9][0-9]{0,4})(-(0|[1-9][0-9]{0,4}))?$", r.port_range) != null && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false))
  28. ])
  29. error_message = "Each extra outbound rule needs protocol tcp or udp and a port_range of a port or range between 0 and 65535 in ascending order (443, 30000-31000), or all."
  30. }
  31. }
  32. variable "inbound_from_member_rules" {
  33. type = list(object({
  34. protocol = string
  35. port_range = string
  36. }))
  37. default = []
  38. description = "Inbound rules that admit only members of this security group itself (a rule whose source is the group). Empty means no inbound rule at all."
  39. validation {
  40. condition = alltrue([
  41. for r in var.inbound_from_member_rules :
  42. contains(["tcp", "udp"], r.protocol) && (r.port_range == "all" || try(regex("^(0|[1-9][0-9]{0,4})(-(0|[1-9][0-9]{0,4}))?$", r.port_range) != null && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false))
  43. ])
  44. error_message = "Each inbound rule needs protocol tcp or udp and a port_range of a port or range between 0 and 65535 in ascending order (443, 30000-31000), or all."
  45. }
  46. }
  47. variable "inbound_from_other_groups" {
  48. type = list(object({
  49. protocol = string
  50. port_range = string
  51. security_group_id = string
  52. }))
  53. default = []
  54. description = "Inbound rules that admit only members of the given OTHER security group (one rule per entry, source = that group, never an address, IPv6 range or prefix list). Additive to inbound_from_member_rules. Empty means none. Ports 1 to 65535, tcp or udp."
  55. validation {
  56. condition = alltrue([
  57. for r in var.inbound_from_other_groups :
  58. contains(["tcp", "udp"], r.protocol) && try(can(regex("^[1-9][0-9]{0,4}(-[1-9][0-9]{0,4})?$", r.port_range)) && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false)
  59. ])
  60. error_message = "Each rule needs protocol tcp or udp and a port_range of a port or range between 1 and 65535 in ascending order (9100, 30000-31000)."
  61. }
  62. validation {
  63. condition = alltrue([for r in var.inbound_from_other_groups : can(regex("^sg-[0-9a-f]{8,17}$", r.security_group_id))])
  64. error_message = "Each security_group_id must be a security group id (sg- and 8 to 17 hex digits), not an address, a CIDR or an empty value."
  65. }
  66. validation {
  67. condition = length(distinct([for r in var.inbound_from_other_groups : join("/", [r.protocol, r.port_range, r.security_group_id])])) == length(var.inbound_from_other_groups)
  68. error_message = "Each (protocol, port_range, security_group_id) may appear only once."
  69. }
  70. }
  71. variable "outbound_to_member_rules" {
  72. type = list(object({
  73. protocol = string
  74. port_range = string
  75. }))
  76. default = []
  77. description = "Outbound rules that reach only members of this security group itself (a rule whose destination is the group, no address). Empty means none."
  78. validation {
  79. condition = alltrue([
  80. for r in var.outbound_to_member_rules :
  81. contains(["tcp", "udp"], r.protocol) && (r.port_range == "all" || try(regex("^(0|[1-9][0-9]{0,4})(-(0|[1-9][0-9]{0,4}))?$", r.port_range) != null && tonumber(split("-", r.port_range)[0]) <= tonumber(reverse(split("-", r.port_range))[0]) && tonumber(reverse(split("-", r.port_range))[0]) <= 65535, false))
  82. ])
  83. error_message = "Each outbound-to-member rule needs protocol tcp or udp and a port_range of a port or range between 0 and 65535 in ascending order (443, 30000-31000), or all."
  84. }
  85. }