WordPress and MySQL Deployment on AWS With Custom VPC, Subnet, NAT Gateway and Security Group using Terraform

Ayush Kumar singh
3 min readOct 7, 2020

What We’ll Do ??

We will create a web portal for our company with all the security as much as possible. We will be using WordPress software with a dedicated database server i.e. MySQL Server. This MySQL Server will run in my Private Subnet and WordPress in Public Subnet Attached with A NAT gateway. Only WordPress Site Should be Public To The World.

How We’ll Do ??

  1. We’ll Create a VPC For Our Web Portal.
  2. In that VPC we have to create 2 subnets:

-Public Subnet [ Accessible for Public World! ]

-Private Subnet [ Restricted for Public World! ]

3. Create a public-facing internet gateway and attach it to our VPC.

4. Create a routing table for the Internet gateway and associate it with the public subnet.

5. Create a NAT gateway for our VPC and attach this gateway to our public Subnet then Update the routing table of the private subnet, so that to access the internet it uses the NAT gateway created in the public subnet.

6. Create a Security Group For WordPress And Will Allow Port 80 And Allow World to Access it.

7. Another Security Group For SQL Which Only Allow 3306 Port From WordPress Security Group Only.

8. Launch WordPress Instance with Public Subnet, Security Group And MySQL Instance With Private Subnet, Security Group.

Lets Code! {All My Code Is Available Here}

  1. Add AWS Provider

2. VPC and Subnet

This Code will Creata A New VPC with 2 Subnet ( Public & Private)

3. Internet Gateway And Routing Table Association

This Code will Create 1 Internet Gateway and a Routing Table which is associated to Our Public Subnet

4. NAT Gateway And Routing Table Association

For NAT Gateway we Need to Have a Permanent IP so We First Created 1 Elastic IP then Created The NAT Gateway and Associated it With Our Private Subnet.

5. Creating A Security Group

A Security Grp will be Created That Allows SSH and HTTP..Similerly We Can create another Security Group for MySQL also That allow 3306 port.

This MySQL Security Group will give Access to Only WordPress Instance or Any instance Which Belongs to WordPress Secrity Group.

6. Instance With WordPress and MySQL

It Will launch 2 Instance 1 with Wordpress in Public Subnet and MySQL Instance in Private Subnet but Attached with Their Respective Security Grp.

Done !!

Start The Terraform And Your Infrastructure will be Ready In Minutes.

To Run the Terraform File

  • Download Terraform From Hashicorp From Here-
  • Add Terraform Path to Environment Variable (For Windows Users)
  • Open Terminal And Navigate to The Folder Where .tf is Present
  • Type “Terraform Init” -This will install All required Plugins
  • Type “Terraform apply -auto-approve” -That’s it Now Your Infrastructure Will get Ready In minutes

Thanks For Reading 🙏

Connect With Me:

LinkedIn: Here

Github Repo: Here

--

--

No responses yet