Skip to content

Commit f1aec94

Browse files
committed
feat!: First commit
0 parents  commit f1aec94

File tree

3 files changed

+306
-0
lines changed

3 files changed

+306
-0
lines changed

key-pair-main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# aws ec2 create-key-pair --key-name web-ec2-key-pair --query 'KeyMaterial' --output text > web-ec2-key-pair.pem
2+
# Generates a secure private key and encodes it as PEM
3+
resource "tls_private_key" "key_pair" {
4+
algorithm = "RSA"
5+
rsa_bits = 4096
6+
}
7+
# Create the Key Pair
8+
resource "aws_key_pair" "key_pair" {
9+
key_name = "web-ec2-key-pair"
10+
public_key = tls_private_key.key_pair.public_key_openssh
11+
}
12+
# Save file
13+
resource "local_file" "ssh_key" {
14+
filename = "${aws_key_pair.key_pair.key_name}.pem"
15+
content = tls_private_key.key_pair.private_key_pem
16+
}

main.tf

Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
# Creation Dependencie:
2+
# VPC - Subnet+IGW - RT
3+
# VPC - Subnet+EIP - NGW - RT
4+
# VPC - Subnet+SG - EC2
5+
# AccessKey - EC2
6+
# VPC - Subnet+SG+DBSubnetGroup - RDS
7+
8+
# 1. Create VPC
9+
resource "aws_vpc" "vpc-load-balancer" {
10+
cidr_block = "10.0.0.0/26"
11+
enable_dns_hostnames = "true"
12+
tags = {
13+
Name = "vpc-load-balancer"
14+
}
15+
}
16+
17+
# 2. Create Subnet
18+
resource "aws_subnet" "subnet-public" {
19+
vpc_id = aws_vpc.vpc-load-balancer.id
20+
availability_zone = "us-east-1a"
21+
cidr_block = "10.0.0.0/27"
22+
map_public_ip_on_launch = true
23+
tags = {
24+
Name = "subnet-public"
25+
}
26+
}
27+
28+
resource "aws_subnet" "subnet-private" {
29+
vpc_id = aws_vpc.vpc-load-balancer.id
30+
availability_zone = "us-east-1a"
31+
cidr_block = "10.0.0.32/28"
32+
map_public_ip_on_launch = false
33+
tags = {
34+
Name = "subnet-private"
35+
}
36+
}
37+
38+
resource "aws_subnet" "subnet-private2" {
39+
vpc_id = aws_vpc.vpc-load-balancer.id
40+
availability_zone = "us-east-1b"
41+
cidr_block = "10.0.0.48/28"
42+
map_public_ip_on_launch = false
43+
tags = {
44+
Name = "subnet-private2"
45+
}
46+
}
47+
48+
# 3. Create Internet-Gateway
49+
resource "aws_internet_gateway" "igw-web"{
50+
vpc_id = aws_vpc.vpc-load-balancer.id
51+
tags = {
52+
Name = "igw-web"
53+
}
54+
}
55+
56+
# 4. Create Elastic IP
57+
resource "aws_eip" "elastic-ip-nat-gateway" {
58+
domain = "vpc"
59+
60+
tags = {
61+
Name = "elastic-ip-nat-gateway"
62+
}
63+
}
64+
65+
resource "aws_eip" "elastic-ip-nat-gateway2" {
66+
domain = "vpc"
67+
68+
tags = {
69+
Name = "elastic-ip-nat-gateway2"
70+
}
71+
}
72+
73+
# 5. Create NAT-Gateway
74+
resource "aws_nat_gateway" "nat_gateway" {
75+
allocation_id = aws_eip.elastic-ip-nat-gateway.id
76+
subnet_id = aws_subnet.subnet-private.id
77+
depends_on = [ aws_nat_gateway.nat_gateway ]
78+
}
79+
80+
resource "aws_nat_gateway" "nat_gateway2" {
81+
allocation_id = aws_eip.elastic-ip-nat-gateway2.id
82+
subnet_id = aws_subnet.subnet-private2.id
83+
depends_on = [ aws_nat_gateway.nat_gateway2 ]
84+
}
85+
86+
# 6. Create Route-Table
87+
resource "aws_route_table" "rt-public"{
88+
vpc_id = aws_vpc.vpc-load-balancer.id
89+
route {
90+
cidr_block = "0.0.0.0/0"
91+
gateway_id = aws_internet_gateway.igw-web.id
92+
}
93+
tags = {
94+
Name = "rt-public"
95+
}
96+
}
97+
98+
resource "aws_route_table" "rt-private"{
99+
vpc_id = aws_vpc.vpc-load-balancer.id
100+
route {
101+
cidr_block = "0.0.0.0/0"
102+
gateway_id = aws_nat_gateway.nat_gateway.id
103+
}
104+
tags = {
105+
Name = "rt-private"
106+
}
107+
}
108+
109+
resource "aws_route_table" "rt-private2"{
110+
vpc_id = aws_vpc.vpc-load-balancer.id
111+
route {
112+
cidr_block = "0.0.0.0/0"
113+
gateway_id = aws_nat_gateway.nat_gateway2.id
114+
}
115+
tags = {
116+
Name = "rt-private2"
117+
}
118+
}
119+
120+
# 7. Assign subnet to route table
121+
resource "aws_route_table_association" "rta-public"{
122+
subnet_id = aws_subnet.subnet-public.id
123+
route_table_id = aws_route_table.rt-public.id
124+
}
125+
126+
resource "aws_route_table_association" "rta-private"{
127+
subnet_id = aws_subnet.subnet-private.id
128+
route_table_id = aws_route_table.rt-private.id
129+
}
130+
131+
# 8. Create security group to allow port: Http, Https, SSH, RDP
132+
resource "aws_security_group" "security-group-web" {
133+
name = "Allow_inbound_traffic"
134+
description = "Allow https, http, ssh inbound traffic"
135+
vpc_id = aws_vpc.vpc-load-balancer.id
136+
137+
ingress {
138+
description = "HTTPS"
139+
from_port = 443
140+
to_port = 443
141+
protocol = "tcp"
142+
cidr_blocks = ["0.0.0.0/0"]
143+
}
144+
145+
ingress {
146+
description = "HTTP"
147+
from_port = 80
148+
to_port = 80
149+
protocol = "tcp"
150+
cidr_blocks = ["0.0.0.0/0"]
151+
}
152+
153+
ingress {
154+
description = "SSH"
155+
from_port = 22
156+
to_port = 22
157+
protocol = "tcp"
158+
cidr_blocks = ["0.0.0.0/0"]
159+
}
160+
161+
egress {
162+
from_port = 0
163+
to_port = 0
164+
protocol = "-1"
165+
cidr_blocks = ["0.0.0.0/0"]
166+
}
167+
168+
tags = {
169+
Name = "security-group-web"
170+
}
171+
}
172+
173+
resource "aws_security_group" "security-group-database" {
174+
name = "Allow_inbound_traffic_database"
175+
description = "Allow mysql inbound traffic to database"
176+
vpc_id = aws_vpc.vpc-load-balancer.id
177+
178+
ingress {
179+
from_port = 3306
180+
to_port = 3306
181+
protocol = "tcp"
182+
cidr_blocks = ["0.0.0.0/0"]
183+
security_groups = [ aws_security_group.security-group-web.id ] # Keep the instance private by only allowing traffic from the web server.
184+
}
185+
186+
egress {
187+
from_port = 0
188+
to_port = 0
189+
protocol = "-1"
190+
cidr_blocks = ["0.0.0.0/0"]
191+
}
192+
193+
tags = {
194+
Name = "security-group-database"
195+
}
196+
}
197+
198+
# 9.a Create Amazon Linux-Apache2 EC2-instance
199+
resource "aws_instance" "web-linux" {
200+
ami = "ami-03a6eaae9938c858c"
201+
instance_type = "t2.micro"
202+
key_name = aws_key_pair.key_pair.key_name
203+
availability_zone = "us-east-1a"
204+
subnet_id = aws_subnet.subnet-public.id
205+
vpc_security_group_ids = [ aws_security_group.security-group-web.id ]
206+
207+
user_data = <<-EOF
208+
#!/bin/bash
209+
sudo yum update -y
210+
sudo yum install -y httpd.x86_64
211+
sudo yum install git -y
212+
sudo systemctl start httpd.service
213+
sudo systemctl enable httpd.service
214+
EOF
215+
216+
tags = {
217+
Name = "web-linux"
218+
}
219+
}
220+
221+
# 9.b Create Windows-IIS EC2-instance
222+
resource "aws_instance" "web-windows" {
223+
ami = "ami-0be0e902919675894"
224+
instance_type = "t2.micro"
225+
key_name = aws_key_pair.key_pair.key_name
226+
availability_zone = "us-east-1a"
227+
subnet_id = aws_subnet.subnet-public.id
228+
vpc_security_group_ids = [ aws_security_group.security-group-web.id ]
229+
230+
user_data = <<-EOF
231+
<powershell>
232+
Install-WindowsFeature -name Web-Server -IncludeManagementTools
233+
New-Item -Path C:\inetpub\wwwroot\index.html -ItemType File -Value "Hello World Page" -Force
234+
</powershell>
235+
EOF
236+
237+
tags = {
238+
Name = "web-windows"
239+
}
240+
}
241+
242+
# 10. Create a RDS Database Instance
243+
resource "aws_db_subnet_group" "db-subnet-group-mysql" {
244+
name = "db-subnet-group-mysql"
245+
subnet_ids = [ aws_subnet.subnet-private.id, aws_subnet.subnet-private2.id ]
246+
}
247+
248+
/*
249+
* allocated_storage: This is the amount in GB
250+
* storage_type: Type of storage we want to allocate(options avilable "standard" (magnetic), "gp2" (general purpose SSD), or "io1" (provisioned IOPS SSD)
251+
* engine: Database engine(for supported values check https://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html) eg: Oracle, Amazon Aurora,Postgres
252+
* engine_version: engine version to use
253+
* instance_class: instance type for rds instance
254+
* name: The name of the database to create when the DB instance is created.
255+
* username: Username for the master DB user.
256+
* password: Password for the master DB user
257+
* db_subnet_group_name: DB instance will be created in the VPC associated with the DB subnet group. If unspecified, will be created in the default VPC
258+
* vpc_security_group_ids: List of VPC security groups to associate.
259+
* allows_major_version_upgrade: Indicates that major version upgrades are allowed. Changing this parameter does not result in an outage and the change is asynchronously applied as soon as possible.
260+
* auto_minor_version_upgrade:Indicates that minor engine upgrades will be applied automatically to the DB instance during the maintenance window. Defaults to true.
261+
* backup_retention_period: The days to retain backups for. Must be between 0 and 35. When creating a Read Replica the value must be greater than 0
262+
* backup_window: The daily time range (in UTC) during which automated backups are created if they are enabled. Must not overlap with maintenance_window
263+
* maintainence_window: The window to perform maintenance in. Syntax: "ddd:hh24:mi-ddd:hh24:mi".
264+
* multi_az: Specifies if the RDS instance is multi-AZ
265+
* skip_final_snapshot: Determines whether a final DB snapshot is created before the DB instance is deleted. If true is specified, no DBSnapshot is created. If false is specified, a DB snapshot is created before the DB instance is deleted, using the value from final_snapshot_identifier. Default is false
266+
*/
267+
resource "aws_db_instance" "db-mysql" {
268+
identifier = "db-mysql-instance"
269+
allocated_storage = 20
270+
storage_type = "gp2"
271+
engine = "mysql"
272+
engine_version = "8.0.33"
273+
instance_class = "db.t2.micro"
274+
username = "admin"
275+
password = "admnin123"
276+
parameter_group_name = "default.mysql8.0"
277+
db_subnet_group_name = aws_db_subnet_group.db-subnet-group-mysql.name
278+
vpc_security_group_ids = [ aws_security_group.security-group-database.id ]
279+
allow_major_version_upgrade = true
280+
auto_minor_version_upgrade = true
281+
backup_retention_period = 35
282+
backup_window = "22:00-23:00"
283+
maintenance_window = "Sat:00:00-Sat:03:00"
284+
multi_az = true
285+
skip_final_snapshot = true
286+
publicly_accessible = true
287+
}

provider.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
provider "aws" {
2+
region = "us-east-1"
3+
}

0 commit comments

Comments
 (0)