Menginstal dan mengonfigurasi Terraform

Halaman ini menjelaskan langkah-langkah untuk menginstal dan mengonfigurasi Terraform untuk Google Cloud di Cloud Shell dan di shell lokal. Cloud Shell adalah lingkungan shell interaktif untuk Google Cloud yang memungkinkan Anda mempelajari dan bereksperimen dengan Google Cloud serta mengelola project dan resource dari browser web.

Cloud Shell

  1. Untuk menggunakan terminal online dengan gcloud CLI dan Terraform yang sudah disiapkan, aktifkan Cloud Shell:

    Di bagian bawah halaman ini, sesi Cloud Shell akan dimulai dan menampilkan perintah command line. Perlu waktu beberapa detik hingga sesi dimulai.

  2. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Untuk menyediakan resource Google Cloud menggunakan Terraform, Anda harus mengaktifkan API yang sesuai:

    gcloud services enable "API"
    

    Ganti API dengan API yang ingin Anda aktifkan.

  5. Untuk menyediakan resource Google Cloud menggunakan Terraform, Anda memerlukan peran Identity and Access Management yang khusus untuk resource ini.

    1. Dari halaman referensi peran IAM, tentukan peran yang diperlukan.
    2. Grant roles to your user account. Run the following command once for each of the following IAM roles:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
      • Replace PROJECT_ID with your project ID.
      • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

      • Replace ROLE with each individual role.
  6. Jalankan perintah berikut untuk memverifikasi bahwa Terraform tersedia:

    terraform
    

    Outputnya akan mirip dengan berikut ini:

    
    Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    
    

Shell lokal

  1. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  2. Make sure that billing is enabled for your Google Cloud project.

  3. Install the Google Cloud CLI, then initialize it by running the following command:

    gcloud init
  4. Aktifkan API yang diperlukan:

    gcloud services enable "API"
    

    Dengan API adalah API yang ingin Anda aktifkan.

  5. Untuk menyediakan resource Google Cloud menggunakan Terraform, Anda memerlukan peran Identity and Access Management yang khusus untuk resource ini.

    1. Dari halaman referensi peran IAM, tentukan peran yang diperlukan.
    2. Grant roles to your user account. Run the following command once for each of the following IAM roles:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
      • Replace PROJECT_ID with your project ID.
      • Replace USER_IDENTIFIER with the identifier for your user account. For example, user:myemail@example.com.

      • Replace ROLE with each individual role.
  6. Instal Terraform.

  7. Jalankan perintah berikut untuk memverifikasi bahwa Terraform tersedia:

    terraform
    

    Outputnya akan mirip dengan berikut ini:

    
     Usage: terraform [global options] <subcommand> [args]
    
    The available commands for execution are listed below.
    The primary workflow commands are given first, followed by
    less common or more advanced commands.
    
    Main commands:
      init          Prepare your working directory for other commands
      validate      Check whether the configuration is valid
      plan          Show changes required by the current configuration
      apply         Create or update infrastructure
      destroy       Destroy previously-created infrastructure
    

Langkah selanjutnya