Introduction
I used to hear people talk about Terraform and cloud, but I didn’t really understand what it was. So, I decided to research and practice to learn how to use it. Today, I’ll guide you through using Terraform and share some of my lab practices along the way.
In this tutorial, I’ll walk you through how to use Terraform to provision infrastructure on AWS. Terraform is an Infrastructure-as-Code (IaC) tool that helps automate and manage cloud resources effectively.
In this example, we will create:
- A VPC (Virtual Private Cloud)
- A Subnet inside the VPC
- An EC2 instance in the subnet
Prerequisites
- AWS account: Make sure you have access to an AWS account.
- Install Terraform: Download Terraform and install it on your machine.
- Install AWS CLI: Download at https://awscli.amazonaws.com/AWSCLIV2.msi and configure your credentials:
aws configure
Enter your Access Key, Secret Key, Region, and Output format.
Note:
– AWS Account + IAM account wil need to be granted as “Programatic” option to run the AWS Cli
– Make sure you enter the correctly AWS credential, and you can check via the command: aws sts get-caller-identity . if it lists the correct account then you can play with terraform . In my case, i got this
Step 1: Set up Your Project Directory
- Create a new folder for your Terraform project:
- Inside this folder, create a file named :
main.tf
Step 2: Create the Terraform Configuration File
Open the filemain.tf
we created above and prepare the code below to create VPC, Subetnet and EC2:
# Configure the AWS Provide, this part is always be required when starts with terraform
# Create a VPC
provider "aws" {
region = "us-east-1" # Change this to your desired AWS region
}
resource “aws_vpc” “main_vpc” {
cidr_block = “10.0.0.0/16”
tags = {
Name = “MyMainVPC”
}
}
# Create a Subnet inside the VPC
resource “aws_subnet” “public_subnet” {
vpc_id = aws_vpc.main_vpc.id
cidr_block = “10.0.1.0/24”
availability_zone = “us-east-1a”
tags = {
Name = “PublicSubnet”
}
}
# Create a Security Group to define access SSH. This example i use Linux so i use port 22, for Windows then port 3389 will be required for the RDP
resource “aws_security_group” “ssh_sg” {
vpc_id = aws_vpc.main_vpc.id
ingress {
from_port = 22
to_port = 22
protocol = “tcp”
cidr_blocks = [“0.0.0.0/0”]
}
egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags = {
Name = “AllowSSH”
}
}
# Create an EC2 instance, this part allow us to define the AMI that we can lookup from AWS portal, it can be any OS available from AWS
resource “aws_instance” “web_server” {
ami = “ami-0c55b159cbfafe1f0” # Amazon Linux 2 AMI ID
instance_type = “t2.micro”
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.ssh_sg.id]
tags = {
Name = "MyWebServer"
}
}
Note:
The line “ami = “AMI model” :
Step 3: Initialize Terraform
- Open your terminal and navigate to the project directory.
- Run the following command to initialize the Terraform working directory:
terraform init
Step 4: Review the Execution Plan
Before applying the changes, it’s a good idea to review what Terraform will create:
terraform plan
You’ll see a summary of all the resources Terraform will create.
Note: This step is always necessary to review the change, to avoid applying the mistaken
Step 5: Apply the Terraform Configuration
Now, let’s apply the configuration to create the resources on AWS:
terraform apply
it will looks like this:


Step 6: Verify the Resources
- Go to the AWS Management Console.
- Navigate to the VPC and EC2 sections to confirm that the VPC, Subnet, Security Group, and EC2 instance have been created.
Step 7: Clean Up the Resources (Optional)
If you no longer need these resources, you can destroy them using Terraform:
confirm removing the resource
Note:
Don`t forget to run destroy if you are working on lab
Conclusion
You have successfully used Terraform to create infrastructure on AWS. This basic example demonstrated how to:
- Set up a VPC and subnet.
- Create a security group to allow SSH access.
- Launch an EC2 instance inside the subnet.
You can expand on this configuration by adding the Elastic IP Address,S3 buckets, or Windows EC2 to explore your infrastructure as code (IaC).
The next part, i will mention more concepts like: Terraform state store in GIT and deploy S3 storage in AWS