Automating AWS Infrastructure with Terraform: My Mini Project (IaaS)
In this post, I’ll walk you through a mini project I recently completed, where I used Terraform to automate the setup of AWS infrastructure. The project was designed to create a custom Virtual Private Cloud (VPC) that hosts EC2 instances and provides internet connectivity.
Project Overview
The goal of this project was to implement a fully automated, scalable infrastructure on AWS using Infrastructure as Code (IaC) with Terraform. Here’s a breakdown of what I built:
VPC Creation: I set up a custom Virtual Private Cloud (VPC) to isolate resources in the cloud.
Public Subnet: The VPC includes a public subnet where I launched 3 EC2 instances. These instances are publicly accessible and ready to serve applications.
Custom Route Table: To manage network traffic, I created a custom route table that directs traffic between the VPC and the internet.
Internet Gateway: Finally, the route table is connected to an internet gateway, enabling the EC2 instances to access the internet and be reachable from outside.
Tools and Technologies
Terraform: Used to define, provision, and automate the cloud infrastructure.
AWS EC2: The compute service running in the public subnet.
VPC and Networking: For creating the isolated cloud network environment.
Why Terraform?
Terraform is an incredibly powerful tool for automating infrastructure deployment. Its declarative nature allowed me to define the entire infrastructure setup in simple configuration files. The flexibility to scale and adapt infrastructure with ease makes Terraform a go-to solution for modern DevOps and cloud practices.
Step 1: Setup a .tf file for VPC
Create a provider block with AWS and specify the AWS region
Under resource block mention the cidr block=10.0.0.0/16 and instance tenancy as default
Step 3: Setup a .tf file for Subnet
Under resource block mention the vpc_id as “aws_vpc.custom_vpc.id”
We also provide a custom name to subnet under tags block so that it can be distinguised easily.
Step 3: Create a security group for the VPC
We create a security group to control the inbound and outbound traffic
Step 4: Create a custom Route table and Internet Gateway
We create a custom route table and replace it with the default route table. The reason we do so is because custom route tables will allow to isolate different parts of your infrastructure within the same VPC.
Step 4: Route table association
We will create a separate .tf file to connect the custom route table with the public subnet
Step 5: Creat Ec2 insances
We will mention the ami id. Ami ids are specific to Amazon Machine Images so make sure you select the right one
Instance type = “t2.micro”
Link the VPC security group and the subnet id.
We will also mention the key name. We can do that by downloading a key pair with .pem format and store in the same folder and the project. Next we will just mention the file name in key_name.
We will create 3 EC2 instance with different names.
Step 5: Terraform command
terraform init #This will initialize the terraform code files
terraform plan #This will not deploy the resources, it helps you understand and visualize the changes Terraform will make to your infrastructure before actually applying them
terraform apply #Deploy infrastructure
terraform destroy #Kill all the resources
What I Learned
Through this project, I deepened my understanding of cloud networking, especially how VPCs, route tables, and internet gateways work together to provide connectivity in AWS. It also reinforced the importance of automating infrastructure as code, which leads to more reliable and reproducible setups.
Conclusion
This project was a fun and educational experience, and I look forward to building more complex infrastructure setups using Terraform. The ability to quickly launch infrastructure through automation is a game-changer in cloud computing!
Subscribe to my newsletter
Read articles from Shivam Soni directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shivam Soni
Shivam Soni
🌟I enjoy sharing my insights and experiences with the community through blog posts and speaking engagements.📝💬 My goal is to empower teams to achieve operational excellence through DevOps best practices and a culture of continuous improvement.💪🌟