Ex. 12 - Configure public Virtual Private Cloud with one public subnet and one private subnet then launch &access any virtual machine within the private subnet
Ex. 12 - Configure public Virtual Private Cloud with one public subnet and one private subnet then launch &access any virtual machine within the private subnet
12 Configure public Virtual Private Cloud with one public subnet and one
private subnet then launch &access any virtual machine within
the private subnet
Aim
To Configure public Virtual Private Cloud with one public subnet and one
private subnet then launch &access any virtual machine
within the private subnet
Algorithm
Steps to Configure
1️. Create VPC & Subnets
● Create a VPC with CIDR 10.0.0.0/16
● Create a Public Subnet (10.0.1.0/24)
● Create a Private Subnet (10.0.2.0/24)
2️. Setup Networking
● Attach an Internet Gateway for public access.
● Create a Route Table for the public subnet to allow internet traffic.
● Create a NAT Gateway in the public subnet so that private instances can access the
internet securely.
● Create a Route Table for the private subnet to send outbound traffic via the NAT
Gateway.
3️. Security Groups
● Bastion Host Security Group (allows SSH from anywhere).
● Private EC2 Security Group (allows SSH only from the Bastion Host).
4️. Deploy EC2 Instances
● Bastion Host in the public subnet (to act as a jump server).
● Private EC2 Instance in the private subnet (no direct internet access).
5.Connect to Private EC2
● SSH into Bastion Host and then into the Private EC2 instance.
# 1️.Create VPC
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "MyVPC"
}
}
# 2️.Create Public Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = true
availability_zone = "us-east-1a"
tags = {
Name = "PublicSubnet"
}
}
tags = {
Name = "PrivateSubnet"
}
}
tags = {
Name = "InternetGateway"
}
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
tags = {
Name = "PublicRouteTable"
}
}
tags = {
Name = "NATGateway"
}
}
# 9️.Create Route Table for Private Subnet (NAT Gateway for Internet Access)
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.nat_gw.id
}
tags = {
Name = "PrivateRouteTable"
}
}
ingress {
from_port = 22
to_port = 22
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 = "BastionSecurityGroup"
}
}
# 1️2. Security Group for Private EC2 (Only Allows SSH from Bastion)
resource "aws_security_group" "private_sg" {
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.bastion_sg.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "PrivateSecurityGroup"
}
}
tags = {
Name = "BastionHost"
}
}
tags = {
Name = "PrivateEC2"
}
}
Steps to Deploy
1️.Initialize Terraform
Bash
terraform init
2️.Plan
bash
terraform plan
3️.Apply
bash
terraform apply -auto-approve