-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
170 lines (140 loc) · 3.84 KB
/
main.tf
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# ------------------- root/main.tf --------------------------
#Create VPC and its subnets
# Custom VPC
resource "aws_vpc" "vpc" {
cidr_block = var.vpc_cidr
tags = {
Name = var.vpc_custom
}
}
# Public Subnet for bastion host
resource "aws_subnet" "subnet_public_bastion" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.subnet_cidr_bhec2
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "subnet_public_bastionhost"
}
}
# Private Subnet
resource "aws_subnet" "subnet_private" {
vpc_id = aws_vpc.vpc.id
cidr_block = var.subnet_cidr_pec2
availability_zone = "us-east-1b"
tags = {
Name = var.subnet_custom
}
}
# Internet Gateway
resource "aws_internet_gateway" "internet_gateway" {
vpc_id = aws_vpc.vpc.id
tags = {
Name = var.internet_gateway_tag
}
}
# Route Table
resource "aws_default_route_table" "default_route" {
default_route_table_id = aws_vpc.vpc.default_route_table_id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.internet_gateway.id
}
}
# Security Group
resource "aws_security_group" "bastion-host-sg" {
name_prefix = var.security_group_name
description = "Bastion Host instance security group"
vpc_id = aws_vpc.vpc.id
ingress {
description = "Allow SSH from my Public IP"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] #replace with your system ip
}
ingress {
description = "Allows HTTP Access to the Bastion Host"
from_port = 8080
to_port = 8080
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"]
}
tags = {
Name = "bastion-host-sg"
}
}
resource "aws_security_group" "private_sg" {
name_prefix = var.security_group_name
description = "Bastion Host instance security group"
vpc_id = aws_vpc.vpc.id
ingress {
description = "Allow SSH from my Public IP"
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.bastion-host-sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "private-sg"
}
}
# Create SSH Keys for EC2 Remote Access
resource "tls_private_key" "generated" {
algorithm = "RSA"
}
resource "local_file" "private_key_pem" {
content = tls_private_key.generated.private_key_pem
filename = "${var.common_ssh_key}.pem"
file_permission = "0400"
}
resource "aws_key_pair" "generated" {
key_name = var.common_ssh_key
public_key = tls_private_key.generated.public_key_openssh
}
# Public EC2 Instance - BastionHost -Ubuntu, 22.04 LTS
resource "aws_instance" "BastionHost-Uec2I" {
ami = "ami-053b0d53c279acc90" #ami-053b0d53c279acc90 #also use free tier t2.micro
instance_type = "t3.micro"
key_name = var.common_ssh_key
security_groups = [aws_security_group.bastion-host-sg.id]
subnet_id = aws_subnet.subnet_public_bastion.id
root_block_device {
volume_type = "gp3"
volume_size = 10
throughput = 500
delete_on_termination = true
}
tags = {
Name = "Bastion-Host-Uec2I"
}
}
# Private EC2 Instance - Ubuntu, 22.04 LTS
resource "aws_instance" "ubuntu-instance" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
key_name = var.common_ssh_key
security_groups = [aws_security_group.private_sg.id]
subnet_id = aws_subnet.subnet_private.id
root_block_device {
volume_type = "gp3" #Faster then regular gp3 and cost less
volume_size = 10
throughput = 300
delete_on_termination = true
}
tags = {
Name = "ubuntu-pvt-instance"
}
}