> ## 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.

# Mengimpor & Membaca Berkas Konfigurasi Eksternal

> Teknik membaca data eksternal di Terraform: fungsi file, templatefile, data source HTTP, dan data source AWS VPC/AMI eksisting.

Sering kali dalam menyusun arsitektur infrastruktur, kita tidak ingin menulis skrip panjang, file JSON policy, atau template konfigurasi Nginx langsung di dalam blok HCL `main.tf`.

Terraform menyediakan fungsi bawaan (*built-in functions*) dan blok **Data Source** untuk mengimpor dan membaca berkas eksternal dari sistem file komputermu maupun dari cloud.

***

## 1. Fungsi `file()` — Membaca Berkas Statis

Fungsi `file()` membaca konten berkas statis di komputermu dan mengembalikannya sebagai teks *string*.

### Contoh 1: Mengimpor Public Key SSH

Daripada menulis string kunci SSH sepanjang 500 karakter di dalam kode, simpan di file `~/.ssh/id_rsa.pub` atau `./keys/id_rsa.pub`:

```hcl theme={null}
resource "aws_key_pair" "deployer" {
  key_name   = "deployer-key"
  public_key = file("${path.module}/keys/id_rsa.pub")
}
```

*Variabel `${path.module}` memastikan Terraform mencari path relatif terhadap lokasi folder file `.tf` saat ini.*

### Contoh 2: Mengimpor IAM Policy JSON

Simpan kebijakan izin AWS di file `policies/s3-readonly-policy.json`:

```hcl theme={null}
resource "aws_iam_policy" "s3_read_policy" {
  name        = "S3ReadOnlyAccessPolicy"
  description = "Kebijakan akses baca S3"
  policy      = file("${path.module}/policies/s3-readonly-policy.json")
}
```

***

## 2. Fungsi `templatefile()` — Membaca Skrip Dinamis

Jika berkas eksternal kamu membutuhkan penyuntikan variabel dinamis (seperti IP database, port, atau domain), gunakan fungsi `templatefile()`.

### Buat Berkas Skrip `scripts/user_data.sh.tftpl`:

```bash theme={null}
#!/bin/bash
apt-get update -y
apt-get install -y nginx

# Menyuntikkan variabel dinamis dari Terraform
echo "Server Role: ${server_role}" > /var/www/html/index.html
echo "Database Host: ${db_endpoint}" >> /var/www/html/index.html
echo "Port: ${app_port}" >> /var/www/html/index.html

systemctl restart nginx
```

### Panggil Menggunakan `templatefile()` di `main.tf`:

```hcl theme={null}
resource "aws_instance" "web" {
  ami           = "ami-0df7a207adb9748c7"
  instance_type = "t2.micro"

  # Mengimpor template skrip dengan menyuntikkan parameter nilai:
  user_data = templatefile("${path.module}/scripts/user_data.sh.tftpl", {
    server_role = "Frontend-Worker"
    db_endpoint = aws_db_instance.my_database.endpoint
    app_port    = 8080
  })
}
```

***

## 3. Blok `data` (Data Source) — Membaca Resource Cloud Tanpa Membuat Ulang

Blok `data` memungkinkan Terraform membaca informasi sumber daya yang sudah ada di cloud provider secara otomatis (*read-only*).

### Mengambil AMI Ubuntu 22.04 Terbaru Secara Dinamis:

Alih-alih mencari ID AMI AWS secara manual di konsol (karena ID AMI berbeda di setiap region):

```hcl theme={null}
data "aws_ami" "latest_ubuntu" {
  most_recent = true
  owners      = ["099720109477"] # ID Resmi Canonical (Pembuat Ubuntu)

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}

# Gunakan data source tersebut langsung pada instance EC2:
resource "aws_instance" "web" {
  ami           = data.aws_ami.latest_ubuntu.id
  instance_type = "t2.micro"
}
```

### Mengambil Informasi VPC Default AWS:

```hcl theme={null}
data "aws_vpc" "default_vpc" {
  default = true
}

output "default_vpc_id" {
  value = data.aws_vpc.default_vpc.id
}
```

*Dengan blok `data`, Terraform selalu mendapatkan data terbaru secara dinamis dari cloud tanpa kamu harus melakukan hardcode ID sama sekali!*


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