Multi-Tier Architecture on AWS (VPC, EC2, RDS) using Terraform
Tags: aws, terraform
Published: 2021-04-20 16:52:21

Hello folks!
🏁 Our scenario for today is creating a multi-tier architecture with WordPress in the Front End and MySQL DB in the backend, also the Internet Facing Website should be in a public subnet with connectivity with the outside world and the DB Server is isolated from the outside world situated in a private subnet with connectivity only with the WordPress Instance.

We shall not be looking into What is IAC and Terraform, as it was already covered in the previous blog, if you haven't yet please give it a read - https://dev.to/rajitpaul/high-availity-architecture-setup-on-aws-using-terraform-402b

📍 First, off we will start by creating a VPC, with two subnets ( public & private ) followed by an internet gateway created and attached with the vpc, next we shall create a route table and add routes into it also associating it to the public subnet.

📍Next, we shall create a key pair and two security groups one acting as our custom sg for the ec2 instance we shall launch later and another acting as a default vpc sg.

📍Finally, we shall launch an ec2 instance in the public subnet also create a db-instance powered by AWS RDS in the private subnet.

<h3>#terraform plan</h3> 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi4pm71lacu5goy24ugf.png)

<h3>#terraform validate</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nh1bjizidjcz3qiapiv2.png)

<h3>#terraform apply</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xcbp2j606pnikr5df51t.png)

<h2>Our Infrastructure is halfway!</h2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxhkele419lnzpxmuiqd.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yepzq9wnuooh9e4zbzhc.png)

📍Now we shall be cloning a git repo in our ec2-instance and running a py script to set up the WordPress on EC2,
GitHub Repo - https://github.com/AWS-Cloud-Community-LPU/Scripts-For-Webinar.git
We shall be taking the help of terraform null resource, connection, and remote-exec provisioner for this purpose!

<h3>Once the terraform script is successfully executed we shall have WordPress deployed on EC2</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ym0prhrl1s75k46sk3a7.png)

<h4>Now we shall be fetching all the details from RDS such as DBname, User, Password & Endpoint and populate that on WordPress installation and this shall be generating a wp-config.php file which we shall copy to the WordPress dir on EC2 instance, for that we shall SSH to the EC2 Instance</h4>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdsd6ziy8rleqxk3irww.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ksclpxhvqvy1oapxvcr.png)

<h3>Our Multi Tier Infrastructure is deployed, let us create our first blog</h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y3ybcng5g9fgw1gzzjnd.png)


Code- https://github.com/RajitPaul11/terraform_aws/tree/main/Multi-Tier-Architecture_Vpc

                              <--->

<h3><u>Multi-Tier-Architecture using NAT GW</u></h3>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc7jfoxbiujr74n7dr5o.jpg)
<i>image credits:https://symbols.getvecta.com/</i>

Adding a Nat Gateway to the architecture so that the DB Instance can connect to the Internet.
The NAT Gateway sits on the public subnet and acts as a one-way gateway to the Internet for our Database Instance.

Code- https://github.com/RajitPaul11/terraform_aws/tree/main/Multi-Tier-Arch-VPC-NATGW

⚡⚡Thanks for Reading⚡⚡