Terraform 101: Hands-On Guide to AWS EC2 and S3 Setup using IaC


Imagine this—you need an EC2 instance, and S3 bucket setup in AWS. You could click around the AWS Console for 20 minutes… or write a few lines of code, run a couple of commands, and have it all up in seconds. Welcome to the world of Infrastructure as Code (IaC) with Terraform.
In this blog, we’ll dive into system provisioning the smart way—coding our infrastructure using Terraform. Whether you're new to cloud automation or just looking to sharpen your skills, this hands-on guide will walk you through creating an EC2 instance, and S3 bucket—all provisioned through code. No manual clicks, just clean, repeatable automation.
What is Terraform?
Terraform, developed by HashiCorp, is an open-source tool that lets you define and provision your infrastructure using a high-level configuration language called HCL (HashiCorp Configuration Language).
Think of it as the bridge between your code and the cloud. You define what resources you want—like EC2 instances, S3 buckets, or VPCs—and Terraform takes care of creating, updating, or destroying them, ensuring consistency across environments.
Why Terraform?
Platform-agnostic: Works with AWS, Azure, GCP, and more.
Declarative: You describe the desired state, and Terraform figures out how to get there.
Version-controlled: Your infrastructure code lives with your app code.
Reusable: Modular structure makes it easy to reuse across projects.
Prerequisites
Before jumping into the hands-on demo, make sure you have the following ready:
🔧 Tools & Accounts
AWS Account – Active account with programmatic access (Access Key & Secret Key)
IAM user with necessary permissions (EC2, S3)
Terraform installed
AWS CLI installed & configured – Run
aws configure
to set up your credentialsCode editor (e.g., VS Code, Sublime Text)
Step 1- Create a separate folder as Project directory using mkdir directory-name
Step 2- Now navigate to the directory using cd directory-name
Step 3- Create main.tf
file
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.68.0"
}
}
}
provider "aws" {
access_key = "your-accesss-key"
secret_key = "your-secret-key"
region = "ap-south-1"
}
In main.tf
you specify the version of Terraform, Cloud Provider, Source and Credentials.
Step 4- Create ec2.tf
file
resource "aws_instance" "My-instance" {
instance_type = "t2.micro"
ami = "ami-002f6e91abff6eb96"
count = 2
tags = {
Name = "ec2-instance-name"
}
}
In ec2.tf
we have specified the resource we want to use aws instance
, name of the instance, type, ami id and count.
Step 5- Create s3.tf
file
resource "aws_s3_bucket" "s3_bucket" {
bucket = "bucket-name"
tags = {
Name = "s3-bucket-name"
}
}
In s3.tf
we have specifies the resource type as aws s3 bucket
and bucket name.
Step 6- Now initialize terraform in the directory by executing this command in the terminal
terraform init
You will see this ouput
Step 7- First validate that the configuration is correct or not
terraform validate
Step 8- Now review the changes being made to the cloud
terraform plan
This command specifies the changes which will be made
Step 9- Now apply the terraform plan
terraform apply
It will ask for approval before creating, enter “yes” to continue.
After approval, it will start creating.
Step 9- Now check the AWS console to ensure that the resources are created
AWS EC2 Instance
AWS S3 Bucket
Step 10- Cleanup Resources
terraform destroy
It will ask for approval before destroying, enter “yes” to continue.
After approval, it will start terminating the resourecs.
You've just taken a big step into the world of Infrastructure as Code, where cloud resources are consistent, repeatable, and version-controlled.
Terraform doesn’t just simplify provisioning—it brings agility, automation, and peace of mind to your DevOps workflow. As you grow more comfortable, you can explore advanced topics like modules, remote state storage, and CI/CD integrations.
Want to Go Further?
Explore Terraform modules to organize your code better
Add more services like RDS, Lambda, or CloudFront
Use Terraform Cloud or backend state management with S3 and DynamoDB
🙌 Thanks for Reading!
If you found this guide helpful, feel free to share it with fellow DevOps enthusiasts or drop your thoughts in the comments. Happy Terraforming!
Subscribe to my newsletter
Read articles from Anurag Negi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
