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

Commit 032afe3

Browse files
committed
Move ingress/egress to aws_security_group_rule in rds
When I make an RDS instance, it kept saying it needed to change stuff. This is also best practice from what I can tell (if users of the module need to add rules to the SG also)
1 parent 2df1d82 commit 032afe3

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

rds/main.tf

+30-22
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,40 @@ variable "subnet_ids" {
8787
type = "list"
8888
}
8989

90-
resource "aws_security_group" "main" {
91-
name = "${var.name}-rds"
92-
description = "Allows traffic to RDS from other security groups"
93-
vpc_id = "${var.vpc_id}"
90+
resource "aws_security_group_rule" "main-ingress-cidrs" {
91+
security_group_id = "${aws_security_group.main.id}"
92+
type = "ingress"
93+
cidr_blocks = ["${var.ingress_allow_cidr_blocks}"]
94+
from_port = "${var.port}"
95+
to_port = "${var.port}"
96+
protocol = "TCP"
97+
}
9498

95-
ingress {
96-
from_port = "${var.port}"
97-
to_port = "${var.port}"
98-
protocol = "TCP"
99-
security_groups = ["${var.ingress_allow_security_groups}"]
100-
}
99+
resource "aws_security_group_rule" "main-ingress-sgs" {
100+
security_group_id = "${aws_security_group.main.id}"
101+
type = "ingress"
102+
count = "${length(var.ingress_allow_security_groups)}"
103+
source_security_group_id = "${element(var.ingress_allow_security_groups, count.index)}"
101104

102-
ingress {
103-
from_port = "${var.port}"
104-
to_port = "${var.port}"
105-
protocol = "TCP"
106-
cidr_blocks = ["${var.ingress_allow_cidr_blocks}"]
107-
}
105+
from_port = "${var.port}"
106+
to_port = "${var.port}"
107+
protocol = "TCP"
108+
}
108109

109-
egress {
110-
from_port = 0
111-
to_port = 0
112-
protocol = -1
113-
cidr_blocks = ["0.0.0.0/0"]
114-
}
110+
resource "aws_security_group_rule" "main-egress-all" {
111+
security_group_id = "${aws_security_group.main.id}"
112+
type = "egress"
113+
from_port = 0
114+
to_port = 0
115+
protocol = -1
116+
cidr_blocks = ["0.0.0.0/0"]
117+
}
115118

119+
120+
resource "aws_security_group" "main" {
121+
name = "${var.name}-rds"
122+
description = "Allows traffic to RDS from other security groups"
123+
vpc_id = "${var.vpc_id}"
116124
tags {
117125
Name = "RDS (${var.name})"
118126
}

0 commit comments

Comments
 (0)