> ## Documentation Index
> Fetch the complete documentation index at: https://docs.skanesgalab.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Instalasi, Format & Penggunaan Dasar Terraform

> Panduan instalasi Terraform & OpenTofu, struktur format HCL (.tf), blok provider, resource, output, dan perintah esensial CLI.

## Instalasi Terraform & OpenTofu

Pilihlah salah satu alat yang kamu sukai (keduanya menggunakan sintaks dan perintah yang identik):

### Menginstal Terraform (Linux Ubuntu/Debian)

```bash theme={null}
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /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 install terraform
```

### Menginstal OpenTofu (Alternatif Open-Source)

```bash theme={null}
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
chmod +x install-opentofu.sh
./install-opentofu.sh --install-method deb
rm install-opentofu.sh
# Verifikasi: tofu -version
```

***

## Anatomi Berkas Konfigurasi HCL (`.tf`)

Terraform membaca semua berkas yang berakhiran `.tf` di dalam sebuah direktori. Blok dasar HCL terdiri dari:

```hcl theme={null}
# 1. Menentukan Provider yang Dibutuhkan
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# 2. Konfigurasi Provider
provider "aws" {
  region = "ap-southeast-1" # Region Singapura
}

# 3. Blok Resource (Sumber Daya yang Ingin Dibuat)
# Format: resource "<tipe_resource>" "<nama_label_lokal>"
resource "aws_s3_bucket" "my_bucket" {
  bucket = "belajar-terraform-yofi-unique-12345"

  tags = {
    Environment = "Dev"
    ManagedBy   = "Terraform"
  }
}

# 4. Blok Output (Menampilkan Informasi Hasil Pembuatan)
output "bucket_arn" {
  description = "Amazon Resource Name dari bucket yang dibuat"
  value       = aws_s3_bucket.my_bucket.arn
}
```

***

## 4 Alur Perintah Utama (Core Commands)

Buka terminal pada direktori yang berisi file konfigurasi `.tf`:

### 1. Inisialisasi (`terraform init`)

Mengunduh binary plugin provider dari registry ke folder lokal tersembunyi `.terraform/`.

```bash theme={null}
terraform init
```

### 2. Format & Validasi Kode

```bash theme={null}
# Merapikan indentasi seluruh file .tf secara otomatis
terraform fmt

# Memvalidasi apakah sintaks HCL valid dan bebas error
terraform validate
```

### 3. Simulasi Rencana (`terraform plan`)

Terraform membandingkan konfigurasi kodemu dengan status infrastruktur nyata di cloud:

```bash theme={null}
terraform plan
```

*Output akan menampilkan ringkasan, misalnya: `Plan: 1 to add, 0 to change, 0 to destroy`.*

### 4. Eksekusi Pembuatan (`terraform apply`)

Mengeksekusi pembuatan resource nyata ke cloud:

```bash theme={null}
terraform apply
# Ketik 'yes' untuk mengonfirmasi persetujuan
# Atau gunakan flag -auto-approve jika dijalankan di pipeline CI/CD:
terraform apply -auto-approve
```

### 5. Menghapus Infrastruktur (`terraform destroy`)

Jika latihan sudah selesai dan kamu tidak ingin terkena tagihan sewa cloud:

```bash theme={null}
terraform destroy -auto-approve
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.