Skip to content

Instantly share code, notes, and snippets.

@greyhoundforty
Created January 25, 2023 18:19
Show Gist options
  • Save greyhoundforty/754f959cba64df1fe5e251919f20bdf5 to your computer and use it in GitHub Desktop.
Save greyhoundforty/754f959cba64df1fe5e251919f20bdf5 to your computer and use it in GitHub Desktop.
Security Group Rules for Terraform for loop
variable "security_group_rules" {
description = "A list of security group rules to be added to the default vpc security group"
type = list(
object({
name = string
direction = string
remote = string
tcp = optional(
object({
port_max = optional(number)
port_min = optional(number)
})
)
udp = optional(
object({
port_max = optional(number)
port_min = optional(number)
})
)
icmp = optional(
object({
type = optional(number)
code = optional(number)
})
)
})
)
validation {
error_message = "Security group rules can only have one of `icmp`, `udp`, or `tcp`."
condition = (var.security_group_rules == null || length(var.security_group_rules) == 0) ? true : length(distinct(
# Get flat list of results
flatten([
# Check through rules
for rule in var.security_group_rules :
# Return true if there is more than one of `icmp`, `udp`, or `tcp`
true if length(
[
for type in ["tcp", "udp", "icmp"] :
true if rule[type] != null
]
) > 1
])
)) == 0 # Checks for length. If all fields all correct, array will be empty
}
validation {
error_message = "Security group rule direction can only be `inbound` or `outbound`."
condition = (var.security_group_rules == null || length(var.security_group_rules) == 0) ? true : length(distinct(
flatten([
# Check through rules
for rule in var.security_group_rules :
# Return false if direction is not valid
false if !contains(["inbound", "outbound"], rule.direction)
])
)) == 0
}
validation {
error_message = "Security group rule names must match the regex pattern ^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$."
condition = (var.security_group_rules == null || length(var.security_group_rules) == 0) ? true : length(distinct(
flatten([
# Check through rules
for rule in var.security_group_rules :
# Return false if direction is not valid
false if !can(regex("^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$", rule.name))
])
)) == 0
}
default = [
{
name = "inbound-vpn-udp"
direction = "inbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
udp = {
port_min = 51280
port_max = 51280
}
},
{
name = "inbound-http"
direction = "inbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
tcp = {
port_min = 80
port_max = 80
}
},
{
name = "inbound-https"
direction = "inbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
tcp = {
port_min = 443
port_max = 443
}
},
{
name = "inbound-ssh"
direction = "inbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
tcp = {
port_min = 22
port_max = 22
}
},
{
name = "inbound-icmp"
direction = "inbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
icmp = {
code = 0
type = 8
}
},
{
name = "all-outbound"
direction = "outbound"
remote = "0.0.0.0/0"
ip_version = "ipv4"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment