Skip to content

Commit

Permalink
feature: allow referencing ipv6 and other security groups in the secu…
Browse files Browse the repository at this point in the history
…rity_group_egress_rules variable
  • Loading branch information
marwinbaumannsbp committed Oct 5, 2023
1 parent 2b49a50 commit 550e7ec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
19 changes: 11 additions & 8 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ resource "aws_security_group" "default" {
}

resource "aws_vpc_security_group_egress_rule" "default" {
for_each = var.subnet_ids != null && length(var.security_group_egress_rules) != 0 ? { for v in var.security_group_egress_rules : v.cidr_ipv4 => v } : {}

cidr_ipv4 = each.value.cidr_ipv4
description = each.value.description
from_port = each.value.from_port
ip_protocol = each.value.ip_protocol
security_group_id = aws_security_group.default[0].id
to_port = each.value.to_port
for_each = var.subnet_ids != null && length(var.security_group_egress_rules) != 0 ? { for v in var.security_group_egress_rules : v.description => v } : {}

cidr_ipv4 = each.value.cidr_ipv4
cidr_ipv6 = each.value.cidr_ipv6
description = each.value.description
from_port = each.value.from_port
ip_protocol = each.value.ip_protocol
prefix_list_id = each.value.prefix_list_id
referenced_security_group_id = each.value.referenced_security_group_id
security_group_id = aws_security_group.default[0].id
to_port = each.value.to_port
}

data "archive_file" "dummy" {
Expand Down
18 changes: 13 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,22 @@ variable "s3_object_version" {

variable "security_group_egress_rules" {
type = list(object({
cidr_ipv4 = string
description = string
from_port = optional(number, 0)
ip_protocol = optional(string, "-1")
to_port = optional(number, 0)
cidr_ipv4 = optional(string)
cidr_ipv6 = optional(string)
description = string
from_port = optional(number, 0)
ip_protocol = optional(string, "-1")
prefix_list_id = optional(string)
referenced_security_group_id = optional(string)
to_port = optional(number, 0)
}))
default = []
description = "Security Group egress rules"

validation {
condition = alltrue([for o in var.security_group_egress_rules : (o.cidr_ipv4 != null || o.cidr_ipv6 != null || o.prefix_list_id != null || o.referenced_security_group_id != null)])
error_message = "Although \"cidr_ipv4\", \"cidr_ipv6\", \"prefix_list_id\", and \"referenced_security_group_id\" are all marked as optional, you must provide one of them in order to configure the destination of the traffic."
}
}

variable "source_code_hash" {
Expand Down

0 comments on commit 550e7ec

Please sign in to comment.