How To LaunchWordpress on public subnet and MySQL on private subnet with NAT gateway on AWS using terraform.
Hybrid multi cloud task -4
We are going to create a Terraform code that will launch the WordPress and MySQL database with NAT Gateway that provides the internet access to the instance in the private subnet.
Task Description:
1. Write an Infrastructure as code using Terraform, which automatically create a VPC.
2. In that VPC we have to create 2 subnets:
a) Public Subnet [ Accessible for Public World ]
b) Private Subnet [ Restricted for Public World ]
3. Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.
4. Create a Routing Table for Internet Gateway so that instance can connect to outside world, update and associate it with public subnet.
5. Create a NAT gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC in the public network
6. Update the routing table of the private subnet, so that to access the internet it uses the NAT gateway created in the public subnet
7. Launch an EC2 instance which has Wordpress setup already having the security group allowing port 80 so that our client can connect to our Wordpress site. Also attach the key to instance for further login into it.
8. Launch an ec2 instance which has MySQL setup already with security group allowing port 3306 in private subnet so that our Wordpress VM can connect with the same. Also attach the key with the same.
Note: Wordpress instance has to be part of public subnet so that our client can connect our site.
MySQL instance has to be part of private subnet so that outside world can’t connect to it.
Don’t forgot to add Auto IP Assign and Auto DNS Name Assign option to be enabled.
What is NAT Gateway?
NAT stands for Network Address Translation. It is a managed service of AWS that makes it easy to connect to the Internet from instances within a private subnet in an Virtual Private Cloud (VPC).
What is Bastion host?
A bastion host is a server whose purpose is to provide access to a private network from an external network, such as the Internet. Because of its exposure to potential attack, a bastion host must minimize the chances of penetration.
STEPS
Provider
provider "aws" {region = "ap-south-1"profile = "atul"}
Step 1: Creating VPC
In one VPC aws provides already with precreated network so any instance can ping to other instance using private IP becuse of this feture we will connect our worpress running on public subnet to mysql running on private subnet.
First we create a VPC.
resource "aws_vpc" "task4_vpc" {
cidr_block = "192.168.0.0/16"
instance_tenancy = "default"
enable_dns_hostnames = true
tags = {
Name = "task4_vpc"
}}
Now we create two subnets in this VPC.One will be public and the other will be private.
resource "aws_subnet" "public_subnet" {
vpc_id = "${aws_vpc.task4_vpc.id}"
cidr_block = "192.168.10.0/24"
availability_zone = "ap-south-1a"
map_public_ip_on_launch = "true"
tags = {
Name = "public_subnet"
}
}
resource "aws_subnet" "private_subnet" {
vpc_id = "${aws_vpc.task4_vpc.id}"
cidr_block = "192.168.20.0/24"
availability_zone = "ap-south-1b"
map_public_ip_on_launch = "true"
tags = {
Name = "private_subnet"
}}
After creating the two subnets ,we will create the Internet Gateway so that the public subnet can have connectivity with the outer world.
resource "aws_internet_gateway" "task4_gateway" {
vpc_id = "${aws_vpc.task4_vpc.id}"
tags = {
Name = "task4_gateway"
}}
Now we will create routing table for Internet gateway,and we will add route to enter the public world via Internet Gateway.
resource "aws_route_table" "task4_route_public" {
vpc_id = "${aws_vpc.task4_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.task4_gateway.id
}
tags = {
Name = "task4_route_public"
}
}
Now we have to associate the routing table to the public subnet.
resource "aws_route_table_association" "a" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.task4_route_public.id
}
resource "aws_eip" "task4_eip" {
vpc=true
}
After Associating the routing table , we will create the NAT Gateway
resource "aws_nat_gateway" "task4_nat" {
allocation_id = aws_eip.task4_eip.id
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "task4_nat"
}}
Now we have to create a routing table for the private subnet,where our database is running ,so that database can go to public world but nobody from public world can connect to the database.So we are going to create and also associate the route table with private subnet.
resource "aws_route_table" "task4_route_private" {
vpc_id = "${aws_vpc.task4_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.task4_gateway.id
}
tags = {
Name = "task4_route_private"
}
}
#Associating the routing table to private subnet
resource "aws_route_table_association" "b" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.task4_route_private.id
}
After that we are going to create the WordPress instance and will allow port 80.
resource "aws_security_group" "wp_sg" {
name = "task4_sg"
description = "Allow ssh-22,http-80 protocols and NFS inbound traffic"
vpc_id = "${aws_vpc.task4_vpc.id}"
ingress {
description = "HTTP"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "SSH"
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 = "wp_sg"
}
}
The security group of bastion
resource "aws_security_group" "bastion" {
name = "bastion"
description = "Bastion host"
vpc_id = aws_vpc.task4_vpc.id
ingress {
description = "ssh"
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 ="bastion"
}}
Security group of MySQL ,and allowing port 3306
resource "aws_security_group" "sql_sg" {
name = "sg_mysql"
vpc_id = "${aws_vpc.task4_vpc.id}"
ingress {
protocol = "tcp"
from_port = 3306
to_port = 3306
security_groups = ["${aws_security_group.wp_sg.id}"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags ={
Name= "sql_sg"
}}
Security group of bastion that will allow mysql to connect
resource "aws_security_group" "bastion_allow" {
name = "bashion_allow"
description = "Allow bashion"
vpc_id = aws_vpc.task4_vpc.id
ingress {
description = "SSH"
from_port = 22
to_port = 22
protocol = "tcp"
security_groups = [aws_security_group.bastion.id]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name ="bastion_allow"
}}
Now creating Wordpress instance.
resource "aws_instance" "task4_wp" {
ami = "ami-000cbce3e1b899ebd"
instance_type = "t2.micro"
associate_public_ip_address = true
key_name = "key11"
vpc_security_group_ids = [aws_security_group.wp_sg.id]
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "task4_wp"
}
}
Creating Bastion instance
resource "aws_instance" "bastion_" {
ami = "ami-0732b62d310b80e97"
instance_type = "t2.micro"
key_name = "key11"
vpc_security_group_ids =[aws_security_group.bastion.id]
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "bastion"
}}
Creating MYSQL database in private subnet
resource "aws_instance" "task4_sql" {
ami = "ami-08706cb5f68222d09"
instance_type = "t2.micro"
key_name = "key11"
vpc_security_group_ids = [aws_security_group.sql_sg.id , aws_security_group.bastion_allow.id]
subnet_id = aws_subnet.private_subnet.id
tags = {
Name = "task4_sql"
}}
Now our complete code is ready to launch the complete infrastructure,and launch the wordpress website.
So first we have to initialize the terraform code,so that terraform can download the required plugin
terraform init
Now apply the terraform code.
terraform apply -auto-approve
github repo link : https://github.com/atuljha0036/multicloud4.git
Thanks for reading…..