Skip to content

Commit 8e95ce5

Browse files
committed
added infrastructure using terraform
1 parent 6896564 commit 8e95ce5

File tree

12 files changed

+220
-111
lines changed

12 files changed

+220
-111
lines changed

terraform/main.tf

Lines changed: 160 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,165 @@
1-
module "vpc" {
2-
source = "./modules/vpc"
1+
# VPC Steup
2+
resource "aws_vpc" "vpc_node" {
3+
cidr_block = var.vpc_cidr
4+
tags = {
5+
Name = "Kubernetes-VPC"
6+
}
37
}
48

5-
module "ec2" {
6-
source = "./modules/ec2"
9+
resource "aws_internet_gateway" "igw" {
10+
vpc_id = aws_vpc.vpc_node.id
11+
tags = {
12+
Name = "IGW"
13+
}
714
}
815

9-
module "kubernates" {
10-
source = "./modules/kubernates"
16+
resource "aws_subnet" "sub_public" {
17+
vpc_id = aws_vpc.vpc_node.id
18+
cidr_block = var.public_subnet_cidr
19+
availability_zone = var.availability_zone
20+
map_public_ip_on_launch = true
21+
tags = {
22+
Name = "Public Subnet 1"
23+
}
1124
}
25+
26+
resource "aws_subnet" "sub_private" {
27+
vpc_id = aws_vpc.vpc_node.id
28+
cidr_block = var.private_subnet_cidr
29+
availability_zone = var.availability_zone
30+
map_public_ip_on_launch = false
31+
tags = {
32+
Name = "Private Subnet 1"
33+
}
34+
}
35+
36+
resource "aws_eip" "lb" {
37+
domain = "vpc"
38+
}
39+
40+
resource "aws_nat_gateway" "ng" {
41+
allocation_id = aws_eip.lb.id
42+
subnet_id = aws_subnet.sub_public.id
43+
44+
tags = {
45+
Name = "gw NAT"
46+
}
47+
}
48+
49+
50+
resource "aws_route_table" "rt_public" {
51+
vpc_id = aws_vpc.vpc_node.id
52+
route {
53+
cidr_block = var.rt_cidr
54+
gateway_id = aws_internet_gateway.igw.id
55+
}
56+
tags = {
57+
Name = "RT_PUBLIC"
58+
}
59+
}
60+
61+
resource "aws_route_table" "rt_private" {
62+
vpc_id = aws_vpc.vpc_node.id
63+
route {
64+
cidr_block = var.rt_cidr
65+
nat_gateway_id = aws_nat_gateway.ng.id
66+
}
67+
tags = {
68+
Name = "RT_PRIVATE"
69+
}
70+
}
71+
72+
resource "aws_route_table_association" "rta_public" {
73+
subnet_id = aws_subnet.sub_public.id
74+
route_table_id = aws_route_table.rt_public.id
75+
}
76+
77+
resource "aws_route_table_association" "rta_private" {
78+
subnet_id = aws_subnet.sub_private.id
79+
route_table_id = aws_route_table.rt_private.id
80+
}
81+
82+
resource "aws_security_group" "aws_sg_master" {
83+
name = "k8s-master-sg"
84+
description = "Security group for Kubernetes master node"
85+
vpc_id = aws_vpc.vpc_node.id
86+
87+
}
88+
89+
resource "aws_vpc_security_group_ingress_rule" "master_ingress_https" {
90+
security_group_id = aws_security_group.aws_sg_master.id
91+
cidr_ipv4 = aws_vpc.vpc_node.cidr_block
92+
from_port = 22
93+
ip_protocol = "tcp"
94+
to_port = 22
95+
}
96+
97+
resource "aws_vpc_security_group_ingress_rule" "master_ingress_k8s" {
98+
security_group_id = aws_security_group.aws_sg_master.id
99+
cidr_ipv4 = aws_vpc.vpc_node.cidr_block
100+
from_port = 6443
101+
ip_protocol = "tcp"
102+
to_port = 6443
103+
}
104+
105+
resource "aws_vpc_security_group_egress_rule" "master_egress" {
106+
security_group_id = aws_security_group.aws_sg_master.id
107+
cidr_ipv4 = "0.0.0.0/0"
108+
ip_protocol = "-1"
109+
}
110+
111+
resource "aws_security_group" "aws_sg_worker" {
112+
name = "k8s-worker-sg"
113+
description = "Security group for Kubernetes worker node"
114+
vpc_id = aws_vpc.vpc_node.id
115+
116+
}
117+
118+
resource "aws_vpc_security_group_ingress_rule" "worker_ingress_https" {
119+
security_group_id = aws_security_group.aws_sg_worker.id
120+
cidr_ipv4 = aws_vpc.vpc_node.cidr_block
121+
from_port = 22
122+
ip_protocol = "tcp"
123+
to_port = 22
124+
}
125+
126+
resource "aws_vpc_security_group_ingress_rule" "worker_ingress_k8s" {
127+
security_group_id = aws_security_group.aws_sg_worker.id
128+
cidr_ipv4 = aws_vpc.vpc_node.cidr_block
129+
from_port = 6443
130+
ip_protocol = "tcp"
131+
to_port = 6443
132+
}
133+
134+
resource "aws_vpc_security_group_egress_rule" "worker_egress" {
135+
security_group_id = aws_security_group.aws_sg_worker.id
136+
cidr_ipv4 = "0.0.0.0/0"
137+
ip_protocol = "-1"
138+
}
139+
140+
# EC2 Instances for Master and Worker Node
141+
142+
resource "aws_instance" "master" {
143+
ami = var.ami_id
144+
instance_type = "t3.medium"
145+
key_name = var.key_name
146+
security_groups = [aws_security_group.aws_sg_master.id]
147+
subnet_id = aws_subnet.sub_public.id
148+
149+
tags = {
150+
Name = "K8s Master"
151+
}
152+
}
153+
154+
resource "aws_instance" "worker" {
155+
count = var.worker_count
156+
ami = var.ami_id
157+
instance_type = "t3.small"
158+
key_name = var.key_name
159+
security_groups = [aws_security_group.aws_sg_worker.id]
160+
subnet_id = aws_subnet.sub_private.id
161+
162+
tags = {
163+
Name = "K8s Worker ${count.index + 1}"
164+
}
165+
}

terraform/modules/ec2/main.tf

Whitespace-only changes.

terraform/modules/ec2/outputs.tf

Whitespace-only changes.

terraform/modules/ec2/variables.tf

Whitespace-only changes.

terraform/modules/kubernates/main.tf

Whitespace-only changes.

terraform/modules/kubernates/outputs.tf

Whitespace-only changes.

terraform/modules/kubernates/variables.tf

Whitespace-only changes.

terraform/modules/vpc/main.tf

Lines changed: 0 additions & 76 deletions
This file was deleted.

terraform/modules/vpc/outputs.tf

Whitespace-only changes.

terraform/modules/vpc/variables.tf

Lines changed: 0 additions & 29 deletions
This file was deleted.

terraform/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "master_public_ip" {
2+
value = aws_instance.master.public_ip
3+
description = "Public IP address of the master instance"
4+
}
5+
6+
output "worker_public_ips" {
7+
value = aws_instance.worker.*.public_ip
8+
description = "Public IP addresses of the worker instances"
9+
}

terraform/variables.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,55 @@
1+
#VPC
12
variable "region" {
23
description = "The AWS region to deploy to"
34
default = "us-east-1"
45
}
6+
7+
variable "vpc_cidr" {
8+
description = "CIDR block for the VPC"
9+
type = string
10+
default = "10.0.0.0/16"
11+
}
12+
13+
variable "public_subnet_cidr" {
14+
description = "CIDR block for public subnet"
15+
type = string
16+
default = "10.0.1.0/24"
17+
}
18+
19+
variable "private_subnet_cidr" {
20+
description = "CIDR block for private subnet"
21+
type = string
22+
default = "10.0.11.0/24"
23+
}
24+
25+
variable "availability_zone" {
26+
description = "Availability Zone"
27+
type = string
28+
default = "us-east-1a"
29+
}
30+
31+
variable "rt_cidr" {
32+
description = "CIDR Block for the VPC"
33+
type = string
34+
default = "0.0.0.0/0"
35+
}
36+
37+
#EC2
38+
39+
variable "key_name" {
40+
description = "Key Pair Name"
41+
type = string
42+
default = "test-k8"
43+
}
44+
45+
variable "worker_count" {
46+
description = "Worker Nodes Count"
47+
type = number
48+
default = 2
49+
}
50+
51+
variable "ami_id" {
52+
description = "AMI ID for Master Node"
53+
type = string
54+
default = "ami-0e86e20dae9224db8"
55+
}

0 commit comments

Comments
 (0)