Skip to content

Commit 70b619c

Browse files
author
lucaronca
committed
Changes the security group handling by applying the rules granularly
1 parent b29958d commit 70b619c

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

main.tf

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,39 @@ resource "aws_security_group" "app-sg" {
4040
"%s-%s-%s",
4141
lookup(var.instance_tags, "name"), lookup(var.instance_tags, "environment"), var.security_group_name
4242
)
43+
}
4344

44-
// To Allow SSH Transport
45-
ingress {
46-
from_port = 22
47-
to_port = 22
48-
protocol = "tcp"
49-
cidr_blocks = ["0.0.0.0/0"]
50-
}
45+
resource "aws_security_group_rule" "app-sg-ssh" {
46+
security_group_id = aws_security_group.app-sg.id
47+
type = "ingress"
48+
from_port = 22
49+
to_port = 22
50+
protocol = "tcp"
51+
cidr_blocks = ["0.0.0.0/0"]
5152

52-
// To Allow Port 80 Transport
53-
ingress {
54-
from_port = 80
55-
to_port = 80
56-
protocol = "tcp"
57-
cidr_blocks = ["0.0.0.0/0"]
58-
}
53+
depends_on = [aws_security_group.app-sg]
54+
}
5955

60-
egress {
61-
from_port = 0
62-
to_port = 0
63-
protocol = "-1"
64-
cidr_blocks = ["0.0.0.0/0"]
65-
}
56+
resource "aws_security_group_rule" "app-sg-http" {
57+
security_group_id = aws_security_group.app-sg.id
58+
type = "ingress"
59+
from_port = 80
60+
to_port = 80
61+
protocol = "tcp"
62+
cidr_blocks = ["0.0.0.0/0"]
6663

67-
lifecycle {
68-
create_before_destroy = true
69-
}
64+
depends_on = [aws_security_group.app-sg]
65+
}
66+
67+
resource "aws_security_group_rule" "app-sg-out" {
68+
security_group_id = aws_security_group.app-sg.id
69+
type = "egress"
70+
from_port = 0
71+
to_port = 0
72+
protocol = "-1"
73+
cidr_blocks = ["0.0.0.0/0"]
74+
75+
depends_on = [aws_security_group.app-sg]
7076
}
7177

7278
resource "aws_security_group_rule" "app-sg-ssl" {
@@ -79,10 +85,6 @@ resource "aws_security_group_rule" "app-sg-ssl" {
7985

8086
count = var.allow_tls ? 1 : 0
8187
depends_on = [aws_security_group.app-sg]
82-
83-
lifecycle {
84-
create_before_destroy = true
85-
}
8688
}
8789

8890
#AWS Instance

0 commit comments

Comments
 (0)