- Authors
- Written by :
Infrastructure as Code Made Simple: Terraform on AWS - Part 1
- Published on
- Published On:
What is Terraform?
Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that allows you to define, provision, and manage infrastructure using declarative configuration files. Instead of manually creating resources, Terraform automates the process, ensuring consistency and scalability.
AWS Setup
Before writing Terraform configurations, we need AWS access and proper authentication setup. This section covers everything you need to get started with Terraform on AWS, from account setup to credential configuration.
First Terraform Configuration
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
- Basic Configuration Structure
Terraform Core Commands
The most used Terraform commands for managing your infrastructure. This section covers all essential commands from initialization to resource management, helping you understand the complete Terraform workflow.
terraform init
terraform validate
terraform plan
terraform apply
State Management
Terraform stores resource metadata in a state file (terraform.tfstate). This helps Terraform track real-world resources.
- Local state – stored in your project folder
- Remote state – stored in AWS S3 or Terraform Cloud for collaboration
Variables & Outputs
Variables make Terraform configurations reusable and dynamic. Instead of hardcoding values (like region, instance type, or database name) in a file like terraform.tfvars.
Input Variable Example:
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
Output Variable Example:
output "instance_ip" {
value = aws_instance.example.public_ip
}
Output Values Example:
locals {
service_name = "forum"
owner = "Community Team"
}
Data Sources
Data sources allow Terraform to use information defined outside of Terraform, defined by another separate Terraform configuration, or modified by functions. They let you fetch existing resources instead of creating new ones.
AWS Data Sources
Data Source Example:
data "aws_ami" "example" {
most_recent = true
owners = ["self"]
tags = {
Name = "app-server"
Tested = "true"
}
}
Using Data Source in Resources:
resource "aws_instance" "web" {
ami = data.aws_ami.example.id
instance_type = "t2.micro"
}
Practical Activity –
Use Terraform to create:
- An EC2 instance → Launch a compute instance
- An S3 bucket → Store files in cloud
- An RDS database → Provision managed relational database
Recommended Course
For those who want a learning path, I highly recommend the Terraform Full Course on YouTube. This comprehensive tutorial is designed to provide hands-on experience with Terraform, covering all the essential concepts and practical examples that form the foundation of Infrastructure as Code. It's perfect for beginners and offers a solid foundation before moving on to more advanced Terraform topics like modules, workspaces, and enterprise features.