Create your own Custom AWS VPC using Terraform and design 2-Tier architecture and deploy WordPress.
Abhinav Paul
Infra Dev Specialist | Devops | Cloud | AWS | Terraform | Shell Scripting | Gitlab CI CD | Python | Linux | Ansible
Create your own VPC using terraform Code
Here I am creating one Public and one Private Subnet with an Internet gateway attached and NAT gateway attached also. After that creating a wordpress instance in the public subnet and creating a database instance MYSQL under private subnet and communicating with each other.
I have used LINUX AMI
Below is the sample diagram You can check what we are going to deploy here.
I will include Load Balancer , Auto Scaling in future and it will be more complex infrastructure.
Performing the following steps:
1. Write an Infrastructure as code using terraform, which automatically create a VPC.
2. In that VPC we have to create 2 subnets:
1. public subnet [ Accessible for Public World! ]
2. 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.
Defining the Provider.
provider "aws" { region = "ap-south-1" }
Creating Custom VPC according to your CIDR range
resource "aws_vpc" "sunny" { cidr_block = "10.0.0.0/16" tags = { Name = "main" } } }
Creating a Public Subnet for my webserver instance
resource "aws_subnet" "public" { vpc_id = "${aws_vpc.sunny.id}" cidr_block = "10.0.1.0/24" availability_zone = "ap-south-1a" map_public_ip_on_launch = "true" tags = { Name = "public" } }
Create a Private Subnet for my database instance
resource "aws_subnet" "private" { vpc_id = "${aws_vpc.sunny.id}" cidr_block = "10.0.2.0/24" availability_zone = "ap-south-1a" map_public_ip_on_launch = "false" tags = { Name = "private" } }
Attaching an Internet Gateway
resource "aws_internet_gateway" "gw" { vpc_id = "${aws_vpc.sunny.id}" tags = { Name = "main" } }
Create a Route Table
resource "aws_route_table" "r" { vpc_id = "${aws_vpc.sunny.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_internet_gateway.gw.id}" } tags = { Name = "public route" } }
Associate the route table with the Public subnet
resource "aws_route_table_association" "public" { subnet_id = aws_subnet.public.id route_table_id = aws_route_table.r.id }
Assigning Elastic IP for providing static IP
resource "aws_eip" "nat" { vpc = true }
NAT gateway creation.
They will charge You if you use NAT gateway so be very careful for using this service. Kindly check the pricing in AWS site.
resource "aws_nat_gateway" "gw" { allocation_id = "${aws_eip.nat.id}" subnet_id = "${aws_subnet.public.id}" tags = { Name = "gw NAT" } }
Creating Private Route Table
resource "aws_route_table" "pvt" { vpc_id = "${aws_vpc.sunny.id}" route { cidr_block = "0.0.0.0/0" gateway_id = "${aws_nat_gateway.gw.id}" } tags = { Name = "private route" } }
Associating the private route table with private subnet
resource "aws_route_table_association" "private" { subnet_id = aws_subnet.private.id route_table_id = aws_route_table.pvt.id }
Security Group for web server
resource "aws_security_group" "webserver" { name = "allow_http" description = "Allow http inbound traffic" vpc_id = "${aws_vpc.sunny.id}" ingress { description = "ssh" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "HTTP from VPC" 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 = "wordpress" } }
Security Group for Database Server
resource "aws_security_group" "mysqlallow" { name = "mysqlallow" description = "ssh allow to the mysql" vpc_id = "${aws_vpc.sunny.id}" ingress { description = "ssh" security_groups=[ "${aws_security_group.webserver.id}" ] from_port = 22 to_port = 22 protocol = "tcp" } ingress { description = "MYSQL" security_groups=[ "${aws_security_group.webserver.id}" ] from_port = 3306 to_port = 3306 protocol = "tcp" } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "mysqlallow" } }
Now we are launching the WordPress on public subnet and installing the packages required for launching successfully.
resource "aws_instance" "wordpress" { ami = "ami-08706cb5f68222d09" instance_type = "t2.micro" availability_zone ="ap-south-1a" subnet_id = "${aws_subnet.public.id}" key_name = "terraform1" security_groups = ["${aws_security_group.webserver.id}"] connection { user = "ec2-user" private_key = file("C:/Users/AVINAVA/task_1/terraform1.pem") host = "${aws_instance.wordpress.public_ip}" } provisioner "remote-exec" { inline = [ "sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd", "sudo service httpd start", "sudo chkconfig httpd on", "sudo curl https://wordpress.org/latest.tar.gz --output wordpress.tar.gz", "sudo tar xf wordpress.tar.gz", "sudo cp -r wordpress /var/www/html/", "sudo service mysqld start", ] } tags = { Name = "wordpress" } }
Creating a Database server on private subnet and it will accessible only from webserver running on public subnet.
resource "aws_instance" "mysql" { ami = "ami-08706cb5f68222d09" instance_type = "t2.micro" key_name = "terraform1" security_groups = ["${aws_security_group.mysqlallow.name}"] subnet_id = "${aws_subnet.public.id}" availability_zone ="ap-south-1a" provisioner "file" { source = "my.cnf" destination = "/tmp/my.cnf" } provisioner "remote-exec" { inline = [ "sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd", "sudo cp /tmp/my.cnf /etc/my.cnf", "sudo chkconfig mysqld on", "sudo service mysqld start", "mysqladmin -u root password *******************", "/etc/init.d/mysqld restart", ] } connection { user = "ec2-user" private_key = file("C:/Users/AVINAVA/task_1/terraform1.pem") host = "${aws_instance.web.public_ip}" } tags = { Name = "mysql" } }
We need to edit this file vi /etc/my.cnf and add the following :
bind-address = 0.0.0.0
Create some user on your database server and create a Database. Below are few samples as an example
create database wordpress; CREATE USER 'jerry'@'localhost' IDENTIFIED BY 'xxxxxxxxxxxxx'; CREATE USER 'jerry'@'%' IDENTIFIED BY 'xxxxxxxxxxxx'; GRANT ALL PRIVILEGES ON *.* to jerry@localhost IDENTIFIED BY 'xxxxxxxx' WITH GRANT OPTION; GRANT ALL PRIVILEGES ON *.* to jerry@'%' IDENTIFIED BY 'xxxxxxxx' WITH GRANT OPTION; FLUSH PRIVILEGES; EXIT;
Security Group for MYSQL and only WordPress server can able to login to database and anyone from outside won't able to login and that is the reason I have the security group ID for the WordPress server :
Security Group for WordPress :
Restart the database :
/etc/init.d/mysqld restart
Give Your Database Private IP and Database User ID and Database Password.
The Final Output !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
That's All Guys I will be adding Load Balancer and Auto-scaling feature on top of this to make things more complicated in my later article.
Infra Dev Specialist | Devops | Cloud | AWS | Terraform | Shell Scripting | Gitlab CI CD | Python | Linux | Ansible
4 年Thank you so much :)
AWS Community Builders ?| Infra Transformation Senior Analyst | DevOps Practitioner ??| ML & AI Enthusiast ??
4 年Such a good stuff...like your all articles..