-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsec_grp.tf
More file actions
executable file
·108 lines (93 loc) · 2.5 KB
/
sec_grp.tf
File metadata and controls
executable file
·108 lines (93 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Fetch self public IP dynamically
data "http" "myip" {
url = "https://ipv4.icanhazip.com"
}
locals {
my_ip = "${chomp(data.http.myip.response_body)}/32"
}
# 1. PUBLIC SECURITY GROUP (Bastion & ALB Entry)
resource "aws_security_group" "public_sg" {
# Using name_prefix instead of name to avoid dependency locks
name_prefix = "public-sg-"
description = "Public SG: SSH from self IP, HTTP open"
vpc_id = aws_vpc.upgrad.id
# SSH - only from your specific local IP
ingress {
description = "SSH from self IP"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = [local.my_ip]
}
# HTTP - open for the world to hit the ALB
ingress {
description = "HTTP from anywhere"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
tags = { Name = "public-sg" }
}
# 2. PRIVATE SECURITY GROUP (Apps & Jenkins)
resource "aws_security_group" "private_sg" {
# Using name_prefix instead of name to avoid dependency locks
name_prefix = "private-sg-"
description = "Private SG: access only from public SG and ALB"
vpc_id = aws_vpc.upgrad.id
# SSH - Access from Bastion
ingress {
description = "SSH from public instances and internal VPC"
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.public_sg.id]
cidr_blocks = ["172.16.0.0/16"]
}
# HTTP (Port 80) - Traffic for Apps
ingress {
description = "HTTP access from ALB for Apps"
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [aws_security_group.alb_sg.id]
}
# Jenkins (Port 8080) - Traffic from ALB and Bastion
ingress {
description = "Jenkins access from ALB and Bastion"
from_port = 8080
to_port = 8080
protocol = "tcp"
security_groups = [
aws_security_group.public_sg.id,
aws_security_group.alb_sg.id
]
}
# ICMP (Ping) - Debugging
ingress {
description = "Allow Ping from within VPC"
from_port = -1
to_port = -1
protocol = "icmp"
cidr_blocks = ["172.16.0.0/16"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
tags = { Name = "private-sg" }
}