Skip to content

Commit 41dda56

Browse files
committed
Grouping SG rules and naming them
1 parent 164d701 commit 41dda56

File tree

1 file changed

+46
-53
lines changed

1 file changed

+46
-53
lines changed

operations/deployment/terraform/modules/02_networking.tf

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -59,62 +59,55 @@ resource "aws_route_table_association" "public" {
5959
}
6060

6161

62+
resource "aws_security_group" "ec2_security_group" {
63+
name = "${var.aws_resource_identifier_supershort}-SG"
64+
description = "SG for ${var.aws_resource_identifier}"
65+
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
66+
egress {
67+
from_port = 0
68+
to_port = 0
69+
protocol = "-1"
70+
cidr_blocks = ["0.0.0.0/0"]
71+
}
72+
tags = {
73+
Name = "${var.aws_resource_identifier}-instance-sg"
74+
}
75+
}
6276

77+
data "aws_security_group" "ec2_security_group" {
78+
cout = var.create_vpc == "true" ? 1 : 0
79+
id = aws_security_group.ec2_security_group.id
80+
}
6381

64-
65-
resource "aws_security_group" "allow_http" {
66-
name = "${var.aws_resource_identifier_supershort}-http"
67-
description = "Allow HTTP traffic"
68-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
69-
ingress {
70-
description = "HTTP"
71-
from_port = 80
72-
to_port = 80
73-
protocol = "tcp"
74-
cidr_blocks = ["0.0.0.0/0"]
75-
}
76-
egress {
77-
from_port = 0
78-
to_port = 0
79-
protocol = "-1"
80-
cidr_blocks = ["0.0.0.0/0"]
81-
}
82+
resource "aws_security_group_rule" "ingress_http" {
83+
name = "Allow HTTP traffic"
84+
type = "ingress"
85+
description = "${var.aws_resource_identifier} - HTTP"
86+
from_port = 80
87+
to_port = 80
88+
protocol = "tcp"
89+
cidr_blocks = ["0.0.0.0/0"]
90+
security_group_id = aws_security_group.ec2_security_group.id
8291
}
83-
84-
resource "aws_security_group" "allow_https" {
85-
name = "${var.aws_resource_identifier_supershort}-https"
86-
description = "Allow HTTPS traffic"
87-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
88-
ingress {
89-
description = "HTTPS"
90-
from_port = 443
91-
to_port = 443
92-
protocol = "tcp"
93-
cidr_blocks = ["0.0.0.0/0"]
94-
}
95-
egress {
96-
from_port = 0
97-
to_port = 0
98-
protocol = "-1"
99-
cidr_blocks = ["0.0.0.0/0"]
100-
}
92+
93+
resource "aws_security_group_rule" "ingress_https" {
94+
name = "Allow HTTPS traffic"
95+
type = "ingress"
96+
description = "${var.aws_resource_identifier} - HTTPS"
97+
from_port = 443
98+
to_port = 443
99+
protocol = "tcp"
100+
cidr_blocks = ["0.0.0.0/0"]
101+
security_group_id = aws_security_group.ec2_security_group.id
101102
}
102103

103-
resource "aws_security_group" "allow_ssh" {
104-
name = "${var.aws_resource_identifier_supershort}-ssh"
105-
description = "Allow SSH traffic"
106-
vpc_id = var.create_vpc == "true" ? aws_vpc.main[0].id : null
107-
ingress {
108-
description = "SSH"
109-
from_port = 22
110-
to_port = 22
111-
protocol = "tcp"
112-
cidr_blocks = ["0.0.0.0/0"]
113-
}
114-
egress {
115-
from_port = 0
116-
to_port = 0
117-
protocol = "-1"
118-
cidr_blocks = ["0.0.0.0/0"]
119-
}
104+
resource "aws_security_group_rule" "ingress_ssh" {
105+
name = "Allow SSH traffic"
106+
type = "ingress"
107+
description = "SSH"
108+
from_port = 22
109+
to_port = 22
110+
protocol = "tcp"
111+
cidr_blocks = ["0.0.0.0/0"]
112+
security_group_id = aws_security_group.ec2_security_group.id
120113
}

0 commit comments

Comments
 (0)