Installa e configura Terraform

Questa pagina descrive i passaggi per installare e configurare Terraform per Google Cloud in Cloud Shell e in una shell locale. Cloud Shell è un ambiente shell interattivo per Google Cloud che ti consente di imparare e sperimentare con Google Cloud, nonché di gestire progetti e risorse dal tuo browser web.

Cloud Shell

  1. Per utilizzare un terminale online con l'interfaccia a riga di comando gcloud e Terraform già configurati, attiva Cloud Shell:

    In fondo a questa pagina, viene avviata una sessione di Cloud Shell mostra un prompt della riga di comando. La sessione può richiedere alcuni secondi vengono inizializzate.

  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. Per eseguire il provisioning delle risorse Google Cloud utilizzando Terraform, devi attivare le API corrispondenti:

    gcloud services enable "API"
    

    Sostituisci API con API che vuoi abilitare.

  5. Per eseguire il provisioning delle risorse Google Cloud utilizzando Terraform, hai bisogno Ruoli di Identity and Access Management specifici di queste risorse.

    1. Da Riferimento per i ruoli IAM specifica i ruoli richiesti.
    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_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. Esegui il comando seguente per verificare che Terraform sia disponibile:

    terraform
    

    L'output dovrebbe essere simile al seguente:

    
    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 locale

  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. Abilita le API richieste:

    gcloud services enable "API"
    

    Dove API è l'API che vuoi abilitare.

  5. Per eseguire il provisioning della risorsa Google Cloud utilizzando Terraform, hai bisogno Ruoli di Identity and Access Management specifici di queste risorse.

    1. Da Riferimento per i ruoli IAM specifica i ruoli richiesti.
    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_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. Installa con Terraform.

  7. Esegui il comando seguente per verificare che Terraform sia disponibile:

    terraform
    

    L'output dovrebbe essere simile al seguente:

    
     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
    

Passaggi successivi