Terraform: Two-Tier Architecture

Jesmine Gandhi
Nerd For Tech
Published in
4 min readAug 25, 2022
Two-Tier Architecture using Terraform
Two-Tier Architecture using Terraform

OBJECTIVE:

Your team needs you to diagram and deploy a two-tier architecture for your company.

  1. Deploy a VPC with CIDR 10.0.0.0/16 with 2 public subnets with CIDR 10.0.1.0/24 and 10.0.2.0/24. Each public subnet should be in a different AZ for high availability.
  2. Create 2 private subnet with CIDR ‘10.0.3.0/24’ and ‘10.0.4.0/24’ with an RDS MySQL instance (micro). Each private subnet should be in a different AZ.
  3. A load balancer that will direct traffic to the public subnets.

LETS GET STARTED!

Pre-requisites:

  • Install Terraform and AWS CLI
  • GitHub Account
  • AWS IAM user account with appropriate permissions
  • Preferred IDE (I used VS Code)

STEP 1:

One of the best practice when creating an Infrastructure as Code is to create a variable file.

I have created several variables such as for AWS region, VPC, Database, Load Balancer. These variable will be used in different parts of the code.

STEP 2:

Creation of an AWS VPC with 2 public subnets and 2 private subnets

Below is the Terraform code gist for creating a VPC and “cidr_block” has been referred using a variable:

Below is the gist for creating 2 Public Subnets:

Below is the gist for creating Internet Gateway:

Below is the gist for creating 2 Private Subnets with cidr 10.0.3.0/24 and 10.0.4.0/24

STEP 3:

Creation of an AWS RDS MySQL Instance

Below is the gist of the code for creating RDS Database(t2 micro type):

Note: When you are providing any sensitive information such as username or password, these information should be set as sensitive as shown in “variables.tf” file above.

Sensitive information like these should be stored in a separate file called as “secret.tfvars”. More information can be found HERE.

STEP 4:

Creation of a Load Balancer

STEP 5:

Creation of a main.tf file which contains the list of providers(AWS in our case)

STEP 6:

Run Terraform Commands to create the infrastructure

After you have created all the necessary files, the first step is to initialized the terraform backend by using the following command:

terraform init

Run a terraform validate to make sure your code configuration is valid:

terraform validate

Next, run the below command to evaluates Terraform configuration-

terraform plan

This is optional, but I would recommend to run below command to make sure your format is neat:

terraform fmt

Next, run the below command to apply your configuration-

terraform apply -var-file="secret.tfvars"

The reason I have used a tag here is because I want the code to use my database credentials stored in the “secret.tfvars” file.

If everything runs smoothly with no error you should see a similar outcomes in your console.

STEP 7:

Verify Resources in the AWS Console

VPC:

Subnets:

Internet Gateway:

Load Balancer:

Database:

STEP 8:

Delete the Created Resources

Once you are done with creation of resources and you have verified it, next step is to destroy all the resources created in order to avoid any unnecessary charges from AWS. Use this command very carefully as this will delete the entire infrastructure you created. Use the following command to perform this:

terraform destroy -var-file="secret.tfvars"

NOTE: Making Modifications

When working in production environment, you would not necessarily delete the entire infrastructure. Just modify the required file/files(in this case I modified the storage value for Database from 5 to 10) and run “terraform plan”. This would show the necessary modifications as shown below:

And if you are satisfied with the modifications, run terraform apply -var-file=”secret.tfvars” to refresh the state file.

And Congratulations, you have successfully created and deployed a 2-tier architecture using Terraform.

Thank you for reading my article!!

--

--

Jesmine Gandhi
Nerd For Tech

DevOps Engineer | AWS Certified Developer Associate | Docker | Terraform