Skip to content

Commit bb649c4

Browse files
Anthony KleinAnthony Klein
Anthony Klein
authored and
Anthony Klein
committed
first commit
0 parents  commit bb649c4

9 files changed

+423
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.DS_Store
2+
/.terraform
3+
/.terraform.lock.hcl
4+
/terraform.tfstate
5+
/terraform.tfstate.backup
10.5 MB
Binary file not shown.

main.tf

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Create a Key Pair
2+
resource "tls_private_key" "deployer" {
3+
algorithm = "RSA"
4+
rsa_bits = 2048
5+
}
6+
7+
resource "aws_key_pair" "my_key_pair" {
8+
key_name = "tf-key-pair"
9+
public_key = tls_private_key.deployer.public_key_openssh
10+
}
11+
12+
resource "local_file" "private_key" {
13+
content = tls_private_key.deployer.private_key_pem
14+
filename = "${path.module}/tf-key-pair.pem"
15+
file_permission = "0400"
16+
}
17+
18+
# Create a VPC
19+
resource "aws_vpc" "kode_vpc" {
20+
cidr_block = var.vpc_cidr
21+
22+
tags = {
23+
Name = var.vpc_name
24+
}
25+
}
26+
27+
# Create an Internet Gateway
28+
resource "aws_internet_gateway" "kode_igw" {
29+
vpc_id = aws_vpc.kode_vpc.id
30+
31+
tags = {
32+
Name = "kode-igw"
33+
}
34+
}
35+
36+
# Create a Route Table
37+
resource "aws_route_table" "kode_rt" {
38+
vpc_id = aws_vpc.kode_vpc.id
39+
40+
route {
41+
cidr_block = "0.0.0.0/0"
42+
gateway_id = aws_internet_gateway.kode_igw.id
43+
}
44+
45+
tags = {
46+
Name = "kode-route-table"
47+
}
48+
}
49+
50+
# Associate the Route Table with the Subnets
51+
resource "aws_route_table_association" "kode_rta_1" {
52+
subnet_id = aws_subnet.kode_subnet_1.id
53+
route_table_id = aws_route_table.kode_rt.id
54+
}
55+
56+
resource "aws_route_table_association" "kode_rta_2" {
57+
subnet_id = aws_subnet.kode_subnet_2.id
58+
route_table_id = aws_route_table.kode_rt.id
59+
}
60+
61+
# Create Subnets CIDR Blocks
62+
resource "aws_subnet" "kode_subnet_1" {
63+
vpc_id = aws_vpc.kode_vpc.id
64+
cidr_block = var.subnet1_cidr
65+
availability_zone = var.subnet1_az
66+
}
67+
68+
resource "aws_subnet" "kode_subnet_2" {
69+
vpc_id = aws_vpc.kode_vpc.id
70+
cidr_block = var.subnet2_cidr
71+
availability_zone = var.subnet2_az
72+
}
73+
74+
# Create a Bastion Host
75+
resource "aws_instance" "bastion" {
76+
ami = var.ec2_ami # Reference the variable
77+
instance_type = "t2.micro"
78+
subnet_id = aws_subnet.kode_subnet_1.id
79+
key_name = aws_key_pair.my_key_pair.key_name
80+
associate_public_ip_address = true
81+
vpc_security_group_ids = [aws_security_group.bastion_sg.id]
82+
83+
tags = {
84+
Name = "BastionHost"
85+
}
86+
}
87+
88+
# Create an Application Load Balancer
89+
resource "aws_lb" "kode_alb" {
90+
name = "kode-alb"
91+
internal = false
92+
load_balancer_type = "application"
93+
security_groups = [aws_security_group.kode_sg.id]
94+
subnets = [
95+
aws_subnet.kode_subnet_1.id,
96+
aws_subnet.kode_subnet_2.id
97+
]
98+
99+
enable_deletion_protection = false
100+
}
101+
102+
# Create a Target Group
103+
resource "aws_lb_target_group" "kode_tg" {
104+
name = "kode-tg"
105+
port = 80
106+
protocol = "HTTP"
107+
vpc_id = aws_vpc.kode_vpc.id
108+
109+
health_check {
110+
interval = 30
111+
path = "/"
112+
timeout = 5
113+
healthy_threshold = 5
114+
unhealthy_threshold = 2
115+
matcher = "200-304"
116+
}
117+
}
118+
119+
# Register the EC2 Instances with the Target Group
120+
resource "aws_lb_target_group_attachment" "kode_tg_attachment" {
121+
count = 2
122+
target_group_arn = aws_lb_target_group.kode_tg.arn
123+
target_id = aws_instance.kode_web[count.index].id
124+
port = 80
125+
}
126+
127+
# Create a Listener
128+
resource "aws_lb_listener" "kode_listener" {
129+
load_balancer_arn = aws_lb.kode_alb.arn
130+
port = 80
131+
protocol = "HTTP"
132+
133+
default_action {
134+
type = "forward"
135+
target_group_arn = aws_lb_target_group.kode_tg.arn
136+
}
137+
}
138+
139+
# Create Web EC2 Instances
140+
resource "aws_instance" "kode_web" {
141+
count = 2
142+
ami = "ami-07d2649d67dbe8900" # Ubuntu Server 24.04 LTS AMI
143+
instance_type = "var.ec2_instance_type"
144+
subnet_id = aws_subnet.kode_subnet_1.id
145+
vpc_security_group_ids = [aws_security_group.kode_sg.id]
146+
key_name = aws_key_pair.my_key_pair.key_name # key pair name
147+
associate_public_ip_address = true
148+
149+
user_data = <<-EOF
150+
#!/bin/bash -ex
151+
152+
apt-get update -y
153+
apt-get install nginx -y
154+
155+
# Replace the default Nginx HTML file
156+
echo '<html>
157+
<head><title>Klein's Custom Nginx Page</title></head>
158+
<body><h1>Hello, World from Terraform!</h1></body>
159+
</html>' > /var/www/html/index.nginx-debian.html
160+
161+
systemctl start nginx
162+
systemctl enable nginx
163+
EOF
164+
165+
tags = {
166+
Name = var.instance_name
167+
}
168+
}

outputs.tf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Output the ID of the VPC
2+
output "vpc_id" {
3+
description = "The ID of the VPC"
4+
value = aws_vpc.kode_vpc.id
5+
}
6+
7+
# Output the ID of the EC2 instances
8+
output "instance_ids" {
9+
description = "The IDs of the EC2 instances"
10+
value = aws_instance.kode_web[*].id
11+
}
12+
13+
# Output the DNS name of the Application Load Balancer
14+
output "alb_dns_name" {
15+
description = "The DNS name of the Application Load Balancer"
16+
value = aws_lb.kode_alb.dns_name
17+
}
18+
19+
# Output the public IP addresses of the EC2 instances
20+
output "instance_public_ips" {
21+
description = "The public IP addresses of the EC2 instances"
22+
value = [for instance in aws_instance.kode_web : instance.public_ip]
23+
}
24+
25+
# Output the ARN of the Target Group
26+
output "target_group_arn" {
27+
description = "The ARN of the Target Group"
28+
value = aws_lb_target_group.kode_tg.arn
29+
}
30+

provider.tf

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
# Configure the AWS Provider
11+
provider "aws" {
12+
profile = "default"
13+
region = "us-east-1"
14+
15+
assume_role {
16+
role_arn = "arn:aws:iam::925717497924:role/terraform"
17+
}
18+
}
19+

sg.tf

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Bastion Host Security Group
2+
resource "aws_security_group" "bastion_sg" {
3+
vpc_id = aws_vpc.kode_vpc.id
4+
5+
ingress {
6+
from_port = 22
7+
to_port = 22
8+
protocol = "tcp"
9+
cidr_blocks = var.bastion_ip # Replace with your IP in tfvars
10+
}
11+
12+
egress {
13+
from_port = 0
14+
to_port = 0
15+
protocol = "-1"
16+
cidr_blocks = ["0.0.0.0/0"]
17+
}
18+
}
19+
20+
# EC2 Web Server Security Group
21+
resource "aws_security_group" "kode_sg" {
22+
vpc_id = aws_vpc.kode_vpc.id
23+
24+
ingress {
25+
from_port = 80
26+
to_port = 80
27+
protocol = "tcp"
28+
cidr_blocks = ["0.0.0.0/0"]
29+
}
30+
31+
ingress {
32+
from_port = 22
33+
to_port = 22
34+
protocol = "tcp"
35+
cidr_blocks = var.public_ip # Restrict SSH access to your IP
36+
}
37+
38+
ingress {
39+
from_port = 22
40+
to_port = 22
41+
protocol = "tcp"
42+
security_groups = [aws_security_group.bastion_sg.id] # Allow SSH from Bastion Host
43+
}
44+
45+
egress {
46+
from_port = 0
47+
to_port = 0
48+
protocol = "-1"
49+
cidr_blocks = ["0.0.0.0/0"]
50+
}
51+
}

ssh_load_balancer.tf

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# Define the Target Group for SSH
2+
resource "aws_lb_target_group" "ssh_target_group" {
3+
name = "ssh-tg"
4+
port = 22
5+
protocol = "TCP"
6+
vpc_id = aws_vpc.kode_vpc.id
7+
8+
health_check {
9+
port = "22"
10+
protocol = "TCP"
11+
interval = 30
12+
timeout = 5
13+
healthy_threshold = 3
14+
unhealthy_threshold = 2
15+
}
16+
}
17+
18+
# Security Group for the Load Balancer
19+
resource "aws_security_group" "lb_sg" {
20+
vpc_id = aws_vpc.kode_vpc.id
21+
22+
ingress {
23+
from_port = 22
24+
to_port = 22
25+
protocol = "tcp"
26+
cidr_blocks = ["70.175.32.184/32"] # my public IP
27+
}
28+
29+
egress {
30+
from_port = 0
31+
to_port = 0
32+
protocol = "-1"
33+
cidr_blocks = ["0.0.0.0/0"]
34+
}
35+
}
36+
37+
# Create the Load Balancer for SSH
38+
resource "aws_lb" "ssh_lb" {
39+
name = "ssh-lb"
40+
internal = false
41+
load_balancer_type = "network"
42+
security_groups = [aws_security_group.lb_sg.id]
43+
subnets = [
44+
aws_subnet.kode_subnet_1.id,
45+
aws_subnet.kode_subnet_2.id
46+
]
47+
48+
enable_deletion_protection = false
49+
}
50+
51+
# Create a Listener for SSH
52+
resource "aws_lb_listener" "ssh_listener" {
53+
load_balancer_arn = aws_lb.ssh_lb.arn
54+
port = 22
55+
protocol = "TCP"
56+
57+
default_action {
58+
type = "forward"
59+
target_group_arn = aws_lb_target_group.ssh_target_group.arn
60+
}
61+
}
62+
63+
# Register the EC2 Instances with the SSH Target Group
64+
resource "aws_lb_target_group_attachment" "ssh_tg_attachment" {
65+
count = 2
66+
target_group_arn = aws_lb_target_group.ssh_target_group.arn
67+
target_id = aws_instance.kode_web[count.index].id
68+
port = 22
69+
}
70+
71+
# Register the First EC2 Instance with the SSH Target Group
72+
#resource "aws_lb_target_group_attachment" "ssh_tg_attachment" {
73+
# target_group_arn = aws_lb_target_group.ssh_target_group.arn
74+
# target_id = aws_instance.kode_web[0].id # Register only the first instance
75+
# port = 22
76+
#}

terraform.tfvars

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bastion_ip = ["70.175.32.184/32"] # my public IP address
2+
public_ip = ["70.175.32.184/32"] # my public IP address
3+
ec2_instance_type = "t2.micro"
4+
instance_name = "kode_web_instance"

0 commit comments

Comments
 (0)