DEV Community

Cover image for Multi-Tier Architecture on AWS (VPC, EC2, RDS) using Terraform
Rajit Paul
Rajit Paul

Posted on โ€ข Edited on

1 1

Multi-Tier Architecture on AWS (VPC, EC2, RDS) using Terraform

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.

#terraform plan

Alt Text

#terraform validate

Alt Text

#terraform apply

Alt Text

Our Infrastructure is halfway!

Alt Text

Alt Text

๐Ÿ“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!

Once the terraform script is successfully executed we shall have WordPress deployed on EC2

Alt Text

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

Alt Text
Alt Text

Our Multi Tier Infrastructure is deployed, let us create our first blog

Alt Text

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

                          <--->
Enter fullscreen mode Exit fullscreen mode

Multi-Tier-Architecture using NAT GW

Alt Text
image credits:https://symbols.getvecta.com/

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โšกโšก

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

Sentry image

See why 4M developers consider Sentry, โ€œnot bad.โ€

Fixing code doesnโ€™t have to be the worst part of your day. Learn how Sentry can help.

Learn more

๐Ÿ‘‹ Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay