Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Add multip-port-sg module #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions modules/multi-port-sg/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Multiple Port Security Group Rule

Create an `aws_security_group_rule` to allow ingress on some ports.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an example of how you intend the module to be used.

57 changes: 57 additions & 0 deletions modules/multi-port-sg/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* ## Multiple Ports Security Group Rule
*
* Create an `aws_security_group_rule` to allow ingress on some ports.
*
*/

variable "security_group_id" {
description = "security group to attach the ingress rules to"
type = string
}

variable "cidr_blocks" {
description = "List of CIDR block ranges that the SG allows ingress from"
type = list(string)
}

variable "description" {
description = "Use this string to add a description for the SG rule"
type = string
}

variable "tcp_ports" {
description = "TCP ports to open"
type = set(string)
default = []
}

variable "udp_ports" {
description = "UDP ports to open"
type = set(string)
default = []
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate the above into a variables.tf

# ingress rules for TCP
resource "aws_security_group_rule" "tcp-ingress" {
for_each = var.tcp_ports
type = "ingress"
description = "${var.description} (tcp)"
from_port = each.value
to_port = each.value
protocol = "tcp"
cidr_blocks = var.cidr_blocks
security_group_id = var.security_group_id
}

# ingress rule for UDP, if any ports were specified
resource "aws_security_group_rule" "udp-ingress" {
for_each = var.udp_ports
type = "ingress"
description = "${var.description} (udp)"
from_port = each.value
to_port = each.value
protocol = "udp"
cidr_blocks = var.cidr_blocks
security_group_id = var.security_group_id
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

4 changes: 4 additions & 0 deletions modules/multi-port-sg/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}