Apa itu Terraform?

Terraform adalah tool Infrastructure as Code (IaC) open-source yang dikembangkan oleh HashiCorp. Dengan Terraform, kita bisa mendefinisikan, membuat, dan mengelola infrastruktur IT (server, database, network, dll) menggunakan kode, bukan dengan klik manual di dashboard cloud provider.

Bayangin: dulu kamu harus login ke AWS Console → klik sana-sini → setting VPC → launch EC2 → configure security group. Sekarang? Cukup tulis kode, run command, dan boom—infrastruktur kamu jadi, konsisten, dan bisa di-version control pakai Git.

Konsep Dasar Terraform

1. Providers

Provider adalah plugin yang menghubungkan Terraform dengan platform cloud atau service tertentu. Terraform support 100+ provider: AWS, Azure, GCP, DigitalOcean, Kubernetes, GitHub, bahkan provider lokal kayak Docker.

provider "aws" {
  region = "ap-southeast-1"  # Singapore region
}

2. Resources

Resource adalah komponen infrastruktur yang mau kamu buat: EC2 instance, S3 bucket, database RDS, load balancer, dll.

resource "aws_instance" "web_server" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
  
  tags = {
    Name = "WebServer-Production"
  }
}

3. State File

Terraform menyimpan informasi infrastruktur yang sudah dibuat di file terraform.tfstate. File ini penting banget—jangan dihapus atau diedit manual! Terraform pakai state ini buat tahu apa yang sudah ada vs apa yang perlu diubah.

4. Modules

Module adalah reusable template. Misalnya kamu sering buat VPC dengan pola yang sama, tinggal bikin module sekali, terus dipakai berulang kali dengan parameter yang beda.

Workflow Terraform

Ada 4 command utama yang akan kamu pakai terus:

CommandFungsi
terraform initInisialisasi project, download provider plugins
terraform planPreview perubahan yang akan dilakukan (dry-run)
terraform applyEksekusi perubahan ke infrastruktur real
terraform destroyHapus semua resource yang dibuat Terraform

Contoh Real-World: Deploy Web Server di AWS

Berikut contoh lengkap deploy EC2 + Security Group:

# main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "ap-southeast-1"
}

# Security Group untuk allow HTTP & SSH
resource "aws_security_group" "web_sg" {
  name        = "web-security-group"
  description = "Allow HTTP and SSH"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  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"]
  }
}

# EC2 Instance
resource "aws_instance" "web" {
  ami           = "ami-0c02fb55956c7d316" # Amazon Linux 2
  instance_type = "t3.micro"
  security_groups = [aws_security_group.web_sg.name]

  user_data = <<-EOF
              #!/bin/bash
              yum update -y
              yum install -y httpd
              systemctl start httpd
              systemctl enable httpd
              echo "<h1>Hello from Terraform!</h1>" > /var/www/html/index.html
              EOF

  tags = {
    Name = "Terraform-WebServer"
  }
}

# Output IP Address
output "public_ip" {
  value = aws_instance.web.public_ip
}

Cara run:

terraform init      # Download AWS provider
terraform plan      # Cek apa yang akan dibuat
terraform apply     # Deploy!

Keunggulan Terraform

✅ Declarative

Kamu cukup bilang “saya mau apa” (contoh: 3 EC2 instance), Terraform yang urusin “gimana caranya”.

✅ Execution Plans

Sebelum eksekusi, Terraform kasih tau persis apa yang akan dibuat, diubah, atau dihapus. No surprise!

✅ State Management

Terraform ingat semua resource yang dibuat, jadi kalau kamu ubah kode, dia tahu apa yang perlu di-update.

✅ Multi-Cloud

Satu tool, banyak platform. Bisa manage AWS + Azure + GCP dalam satu codebase.

Best Practices

  1. Remote State: Jangan simpan state file di local atau Git. Pakai S3 + DynamoDB (AWS) atau Terraform Cloud buat kolaborasi tim.
  2. Variable & Output: Jangan hardcode value, pakai variables.tf dan outputs.tf biar modular.
  3. Workspace: Pisahkan environment (dev/staging/prod) pakai workspace atau folder terpisah.
  4. Version Lock: Tentukan versi provider dan Terraform di required_providers biar gak breaking change tiba-tiba.
  5. Formatting: Selalu pakai terraform fmt buat rapihin kode.

Kapan Pakai Terraform?

Terraform cocok buat:

  • Cloud Infrastructure: VM, network, storage di AWS/Azure/GCP
  • Kubernetes: Deploy cluster atau manage manifest
  • SaaS Integration: Setup Datadog, GitHub repo, Cloudflare DNS
  • On-Premise: Manage VMware, OpenStack, atau hardware lokal

Kesimpulan

Terraform mengubah cara kita manage infrastruktur dari manual dan error-prone jadi automated dan reliable. Dengan kode yang bisa di-review, di-test, dan di-version control, tim DevOps/SRE bisa kerja lebih cepat dan aman.

Leave a Reply

Your email address will not be published. Required fields are marked *