Friday, April 5, 2024
GCP Terraform

Membuat Infrastructure as Code dengan Terraform

Code

“Membuat Infrastructure as Code dengan Terraform”

Pengantar

Terraform adalah infrastructure as code dari HashiCorp. Ini adalah alat untuk membangun, mengubah, dan mengelola infrastruktur dengan cara yang aman dan berulang. Operator dan tim Infrastruktur dapat menggunakan Terraform untuk mengelola lingkungan dengan bahasa konfigurasi yang disebut HashiCorp Configuration Language (HCL) untuk penerapan otomatis yang dapat dibaca manusia.

Infrastructure as code adalah proses mengelola infrastruktur dalam file atau file daripada mengkonfigurasi sumber daya secara manual di antarmuka pengguna. Sumber daya dalam hal ini adalah setiap bagian dari infrastruktur di lingkungan tertentu, seperti mesin virtual, grup keamanan, antarmuka jaringan, dll. Pada tingkat tinggi, Terraform memungkinkan operator untuk menggunakan HCL untuk membuat file yang berisi definisi sumber daya yang mereka inginkan di hampir semua penyedia (AWS, Google Cloud, GitHub, Docker, dll.) dan mengotomatiskan pembuatan sumber daya tersebut pada saat berlaku.

Persyaratan

Cara Membuat Infrastructure

  • Buat file baru
touch main.tf
  • Buka Open Editor

Tambahkan script berikut -> ganti project ID dengan milikmu

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
    }
  }
}
provider "google" {
  version = "3.5.0"
  project = "<PROJECT_ID>"
  region  = "us-central1"
  zone    = "us-central1-c"
}
resource "google_compute_network" "vpc_network" {
  name = "terraform-network"
}
  • Intialisasi dan terapkan konfigurasi
terraform init
terraform apply
  • Cek di CLoud Shell
terraform show

Cara Mengubah Infrastructure

Menambahkan Resources

  • Tambahkan code berikut pada main.tf
resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }
  network_interface {
    network = google_compute_network.vpc_network.name
    access_config {
    }
  }
}
terraform apply

Mengganti Resources

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"
  tags         = ["web", "dev"]
  # ...
}
  • Apply
terraform apply

Destructive Changes

  boot_disk {
    initialize_params {
      image = "cos-cloud/cos-stable"
    }
  }
  • Kemudian Apply
terraform apply

Destroy Infrastructure

terraform destroy

Create Resource Dependencies

  • Recreate Network dan instance
terraform apply

Menambahkan IP static

  • Tambahkan code berikut di main.tf
resource "google_compute_address" "vm_static_ip" {
  name = "terraform-static-ip"
}
  • Kemudian Terraform Plan
terraform plan
  • Update bagian network_interface
  network_interface {
    network = google_compute_network.vpc_network.self_link
    access_config {
      nat_ip = google_compute_address.vm_static_ip.address
    }
  }
  • Run terraform plan
terraform plan -out static_ip
  • Apply konfig
terraform apply "static_ip"

Implicit dan Explicit Dependencies

  • Membuat bucket dan instance , tambahkan code berikut pada main.tf
# New resource for the storage bucket our application will use.
resource "google_storage_bucket" "example_bucket" {
  name     = "<UNIQUE-BUCKET-NAME>"
  location = "US"
  website {
    main_page_suffix = "index.html"
    not_found_page   = "404.html"
  }
}
# Create a new instance that uses the bucket
resource "google_compute_instance" "another_instance" {
  # Tells Terraform that this VM instance must be created only after the
  # storage bucket has been created.
  depends_on = [google_storage_bucket.example_bucket]
  name         = "terraform-instance-2"
  machine_type = "f1-micro"
  boot_disk {
    initialize_params {
      image = "cos-cloud/cos-stable"
    }
  }
  network_interface {
    network = google_compute_network.vpc_network.self_link
    access_config {
    }
  }
}
  • run plan
terraform plan
terraform apply

Provision Infrastructure

Defining a Provisioner

  • Update resource block
resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"
  tags         = ["web", "dev"]
  provisioner "local-exec" {
    command = "echo ${google_compute_instance.vm_instance.name}:  ${google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip} >> ip_address.txt"
  }
  # ...
}
  • Apply
terraform apply
  • Recreate intance dengan taint
terraform taint google_compute_instance.vm_instance
  • run apply
terraform apply

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Membuat Infrastructure as Code dengan Terraform. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

(Visited 420 times, 1 visits today)

Similar Posts