Introduction to Terraform
📚 Introduction
Infrastructure as Code (IaC) tools allow you to manage infrastructure using
configuration files instead of a graphical user interface. IaC helps you build,
change, and manage your infrastructure in a safe, consistent, and repeatable
way.
🚩 Challenges in IT Infrastructure
Business Challenges:
❖ Slow Deployment 🐢
❖ Expensive 💸
❖ Limited Automation 🤖
❖ Human Error 🚨
❖ Inconsistency 🔄
❖ Wasted Resources 🗑️
Teams Involved:
❖ Field Engineers🔧
❖ System/Network Administrators 🌐
❖ Storage Admins 💾
❖ Backup Admins 📦
❖ Application Team 📱
❖ AWS ☁️
❖ VMware 🖥️
❖ Data Center 🏢
Made By Mukesh Chaudhary
Challenges in IT Infrastructure
❖ Terraform is
HashiCorp's infrastructure as
Terraform
code tool. It lets you define
resources and infrastructure in
human-readable configuration
files and manages your
infrastructure's lifecycle.
✅ Advantages of Terraform:
● Multi-Cloud Management ☁️☁️
● Human-Readable Configuration 💽
● State Tracking 📊
● Version Control 🔄
👅 Installation
Linux
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor | sudo tee
/usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg]
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee
/etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform
🧉 HCL (Hashicorp Configuration Language)
HCL is the low-level syntax used by Terraform. You don’t need to know all the
details, just the basics!
Example:
resource "local_file" "pet" {
filename = "/root/pets.txt"
content = "I love pets! 🐶🐱"
}
🚀 Execution of Infrastructure
★ Init 🛠️
terraform init
➔ Scans your .tf files and installs required plugins.
★ Plan 📝
terraform plan
➔ Creates an execution plan.
★ Apply 🚀
terraform apply
➔ Executes the plan and automates infrastructure creation.
🐳 Terraform with Docker
Terraform Block
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "-> 2.21.0"
}
}
}
Provider Block
provider "docker" {}
Resource Block
resource "docker_image" "nginx" {
name = "nginx:latest"
keep_locally = false
}
resource "docker_container" "nginx" {
image = docker_image.nginx.latest
name = "tutorial"
ports {
internal = 80
external = 80
}
}
📦 Terraform Variables
Example:
variable "filename" {
default = "/home/ubuntu/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable! 🎉"
}
Accessing Variables:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}
Made By Mukesh Chaudhary
🌀 Terraform with AWS
Example: EC2 Instance
resource "aws_instance" "aws_ec2_test" {
count = 4
ami = "ami-08c40ec9ead489470"
instance_type = "t2.micro"
tags = {
Name = "TerraformTestServerInstance"
}
}
🐚 Terraform State
State Commands:
List Resources 📋
terraform state list
Show Resource 🔍
terraform state show <resource>
Remove Resource 🗑️
terraform state rm <resource>
Made By Mukesh Chaudhary
🛠️ Provisioners
Example:
resource "aws_instance" "web" {
provisioner "local-exec" {
command = "echo The server's IP address is ${self.private_ip}"
}
}
🔒 Sensitive Data
Example:
output "db_password" {
value = aws_db_instance.db.password
sensitive = true
}
🏁 Conclusion
Terraform is a powerful tool for managing infrastructure as code. With its
human-readable configuration, state management, and multi-cloud support, it’s a
must-have for modern DevOps practices. 🚀
🎉 Happy Terraforming! 🎉
Made By Mukesh Chaudhary