CREATE AWS VPC WITH PUBLIC SUBNET, PRIVATE SUBNET, INTERNET GATEWAY THEN DEPLOY WORDPRESS AND DATABASE SERVER
Nishant Singh
Senior Software Engineer@HCL Tech | Red Hat Certified System Administrator | AWS Certified Solution Architect-Associate | AWS Certified Developer Associate | AWS Cloud Practitioner Certified
What is VPC ?
Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.
What is subnet ?
Subnet is “part of the network”, in other words, part of entire availability zone. Each subnet must reside entirely within one Availability Zone and cannot span zones.
What is Internet Gateway?
- An internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the internet.
- An internet gateway serves two purposes: to provide a target in your VPC route tables for internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.
- An internet gateway supports IPv4 and IPv6 traffic. It does not cause availability risks or bandwidth constraints on your network traffic.
Problem Statement:
Statement: We have to create a web portal for our company with all the security as much as possible.
So, we use Wordpress software with dedicated database server.
Database should not be accessible from the outside world for security purposes.
We only need to public the WordPress to clients.
So here are the steps for proper understanding!
Steps:
1) Write a 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) 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.
6) 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 assignment option to be enabled.
Try each step first manually and write Terraform code for the same.
This will give u proper understanding of workflow of task.
These are some basic command of terraform code.
- terraform init (To install all the required plugins)
- terraform validate (To check the code)
- terraform apply -auto-approve (To apply all the resources and run the code)
- terraform destroy -auto-approve (To delete all services initiated by terraform code in a single go)
Solution:
Before going to the solution firstly we have to login through aws CLI using aws configure command.
Create a VPC:
provider "aws" { region="ap-south-1" profile="nishant" } resource "aws_vpc" "myvpc" { cidr_block = "192.168.0.0/16" instance_tenancy = "default" tags = { Name = "myvpc" } } output "myvpc"{ value=aws_vpc.myvpc.id }
When we will apply the terraform code we will get the output
Create the Subnet:
Now we have to create two types of subnets like public subnet for WordPress and private subnet for mysql database server using terraform code.
Public Subnet(For Wordpress):
resource "aws_subnet" "mypublicsubnet" { vpc_id = aws_vpc.myvpc.id cidr_block = "192.168.0.0/24" availability_zone = "ap-south-1a" map_public_ip_on_launch = true tags = { Name = "mypublicsubnet" } }
When we will apply it we will get the output
Private Subnet(For Mysql):
resource "aws_subnet" "myprivatesubnet" { vpc_id = aws_vpc.myvpc.id cidr_block = "192.168.1.0/24" availability_zone = "ap-south-1b" tags = { Name = "myprivatesubnet" } }
When we apply this terraform code we will get the output
Create the Internet Gateway:
Now for the connectivity of the subnets like to go in and go out we have to create one public facing gateway called internet gateway.
resource "aws_internet_gateway" "myigw" { vpc_id = aws_vpc.myvpc.id tags = { Name = "myigw" } }
We will get the output after applying it
Create the Routing Table:
For internet gateway we have to configure one routing table which provides ip range to DHCP internally and attaching these routing table to public subnet.
#creating routing table resource "aws_route_table" "myroute" { vpc_id = aws_vpc.myvpc.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.myigw.id } tags = { Name = "myroute" } } # attaching the routing table resource "aws_route_table_association" "mypubroutetable" { subnet_id = aws_subnet.mypublicsubnet.id route_table_id = aws_route_table.myroute.id }
Now when we apply this code we will get the output of it.
Create Security Groups:
Create two security groups one for wordpress which allow 80 port and second for mysql which allows only 3306 port.I also attach the key in both the EC2 instance means we can do ssh whenever we need. SSH work on port 22 means allow 22 port in both the security groups.
Public Security Group (For Wordpress):
resource "aws_security_group" "mysg1" { name = "mysg1" description = "Allow 80 port" vpc_id = aws_vpc.myvpc.id ingress { description = "SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "TCP" from_port = 80 to_port = 80 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 = "mysg1" } }
when we will apply it and we will get the output of it
Private Security Group(For Mysql):
resource "aws_security_group" "mysg2" { name = "mysg2" description = "Allow 3306 port" vpc_id = aws_vpc.myvpc.id ingress { description = "SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "HTTP" from_port = 3306 to_port = 3306 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 = "mysg2" } }
when we will apply it and we will get the output
Create/Launch the EC2 instance:
Mysql Instance:
resource "aws_instance" "mysqlos" { ami = "ami-0019ac6129392a0f2" instance_type = "t2.micro" key_name="mykey1332" vpc_security_group_ids = ["${aws_security_group.mysg2.id}" ] subnet_id = aws_subnet.myprivatesubnet.id tags = { Name = "mysqlos" } }
When we will apply then we will get the output
Wordpress Instance:
resource "aws_instance" "wpos" { ami = "ami-000cbce3e1b899ebd" instance_type = "t2.micro" key_name="mykey1332" vpc_security_group_ids = ["${aws_security_group.mysg1.id}" ] subnet_id = aws_subnet.mypublicsubnet.id tags = { Name = "wpos" } }
When we will apply it we will get the output
Now I apply the complete process on the whole code
First we have to download the plugins for running code by typing terraform init command in cmd.
After downloading the plugin we have to check its validation by typing terraform validate command in cmd.
Now we have to apply the code by typing terraform apply -auto-approve command in cmd.
Now the outputs is given below
Now we see that wordpress is successfully deployed. Now delete the complete setup by typing terraform destroy -auto-approve command in cmd.
GITHUB URL:
THANK YOU ALL FOR VISITING MY ARTICLE!!!