Deploying a two-tier architecture using Terraform

Tobi
3 min readOct 16, 2022

--

Scenario:

Your team needs you to diagram and deploy a two-tier architecture for your company. For the Foundational project you are allowed to have all your code in a single main.tf file (known as a monolith) with hardcoded data.

  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) in one of the subnets. Each private subnet should be in a different AZ.
    3. A load balancer that will direct traffic to the public subnets.
    4. Deploy 1 EC2 t2.micro instance in each public subnet.

I will be using cloud9 for this project, you can use any IDE of your choice, just make sure AWS CLI and terraform are installed on it.

The first thing I’ll be doing is making a directory to create our main.tf

mkdir <directory_name>
cd <directory_name>

The next thing to do is to create the main.tf file

touch main.tf

We’ll be using the AWS provider to interact with the resources we’ll be provisioning.

The next thing to do is to create the VPC(10.0.0.0/16), 2 public subnet (10.0.1.0/24 and 10.0.2.0/24) and 2 private subnet (10.0.3.0/24 and 10.0.4.0/24)

The next thing to do is to create the internet Gateway, Route table and Route table Association for the public subnet

Now we can create the security group for ec2, alb and RDS

Next, we create an ec2 instance for both public subnets

Next, we create ALB

The last thing to do is the RDS

The next thing to do is to initialize the directory by using the command:

terraform init

output:

This means the directory has been initialized successfully.

The next thing is to validate the configuration files in the directory, you can use the command:

terraform validate

output:

Our configuration files is valid

We can view the execution plan which would let us preview the changes that terraform plans to make to our infrastructure, you can use the command:

terraform plan

output:

The next thing is to execute the actions that were proposed in the terraform plan, we can do that by using the command:

terraform plan

we’ll be prompted to approve the execution plan since we didn’t pass a — auto-approve option.

output:

We have successfully deployed our infrastructures using terraform. One thing to note when applying is that it takes about 4–6 minutes to create the RDS.

Remember to destroy everything you have created so you won’t get charged. You can use the command:

terraform destroy

Thank you for reading, I hope you enjoyed it.

--

--