Infrastructure as Code — Pola Terraform yang Bisa Diskalakan
Infrastructure as Code (IaC) bukan sekadar menulis file konfigurasi. IaC adalah soal membangun infrastruktur yang modular, bisa diskalakan, dan mudah dipelihara. Di artikel ini, kita akan bahas pola Terraform yang terbukti digunakan di sistem produksi berskala besar.
1. Desain Modular
Kenapa Harus Modular?
- Hindari copy-paste kode
- Bisa digunakan ulang & dites
- Pisahkan tanggung jawab (separation of concerns)
Contoh Struktur Modular
modules/
vpc/
eks/
rds/
s3/
alb/
environments/
staging/
main.tf
variables.tf
production/
main.tf
variables.tf
Gunakan variable
dan output
:
# modules/vpc/variables.tf
variable "cidr_block" {
type = string
}
# modules/vpc/main.tf
resource "aws_vpc" "main" {
cidr_block = var.cidr_block
}
Pemanggilan modul:
module "vpc" {
source = "../../modules/vpc"
cidr_block = "10.0.0.0/16"
}
2. Isolasi Environment dengan Workspace
Daripada menduplikasi file untuk setiap environment:
terraform workspace new staging
terraform workspace new production
Gunakan workspace-aware logic:
locals {
env = terraform.workspace
}
resource "aws_s3_bucket" "logs" {
bucket = "log-aplikasi-${local.env}"
}
💡 Tip: Gunakan
terragrunt
atauatmos
jika project semakin besar.
3. Remote State + Locking
Simpan state di remote storage dan aktifkan locking:
terraform {
backend "s3" {
bucket = "tf-state-hasan"
key = "envs/prod/terraform.tfstate"
region = "ap-southeast-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}
Keuntungan:
- Bisa kolaborasi antar tim
- Mencegah konflik state
- Ada jejak audit
4. Struktur DRY dengan Terragrunt
Terragrunt bantu kamu hindari pengulangan:
live/
prod/
vpc/
eks/
staging/
vpc/
eks/
modules/
vpc/
eks/
Contoh terragrunt.hcl
:
terraform {
source = "../../modules/vpc"
}
include {
path = find_in_parent_folders()
}
inputs = {
cidr_block = "10.0.0.0/16"
}
5. Konvensi Penamaan & Tag
Gunakan tag standar di semua resource:
locals {
common_tags = {
Environment = terraform.workspace
Owner = "tim-devops"
Project = "hasan-app"
}
}
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t3.micro"
tags = local.common_tags
}
Format nama standar:
<project>-<env>-<komponen>
# Contoh: hasanapp-prod-vpc
6. Praktik Keamanan
- Gunakan
aws_iam_policy_document
untuk generate IAM Policy - Gunakan Secrets Manager/Vault untuk menyimpan rahasia
- Jangan hardcode credential di file
Contoh:
data "aws_secretsmanager_secret_version" "db_password" {
secret_id = "rds-db-password"
}
output "db_password" {
value = data.aws_secretsmanager_secret_version.db_password.secret_string
sensitive = true
}
7. Integrasi dengan CI/CD
Integrasi Terraform ke dalam pipeline CI/CD
Contoh Github Actions
name: Terraform CI
on:
push:
branches: [main]
jobs:
terraform:
name: "Terraform Plan & Apply"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
- name: Terraform Init
run: terraform init
- name: Terraform Plan
run: terraform plan
- name: Terraform Apply
run: terraform apply -auto-approve
if: github.ref == 'refs/heads/main'
🔐 Gunakan GitHub Encrypted Secrets atau OpenID Connect untuk keamanan token.
8. Deployment Blue-Green atau Canary
Gunakan toggle atau abstraksi infra untuk rollout bertahap:
Contoh: switch Target Group di ALB
variable "active_target_group" {
default = "blue"
}
resource "aws_lb_listener_rule" "forward" {
listener_arn = aws_lb_listener.app.arn
actions = [
{
type = "forward"
target_group_arn = var.active_target_group == "blue" ? aws_lb_target_group.blue.arn : aws_lb_target_group.green.arn
}
]
}
Untuk canary rollout, kamu bisa gunakan AWS AppConfig atau feature flag seperti Unleash.
Kesimpulan
Menulis file .tf
saja tidak cukup. Terraform yang scalable butuh arsitektur yang baik, automation yang solid, dan disiplin. Pola-pola di atas bisa membantu kamu menjaga proyek tetap rapi saat mulai berkembang.
Punya pola lain yang sering kamu pakai di production? Yuk diskusi di LinkedIn.