Instalar e configurar o Terraform

Nesta página, descrevemos as etapas para instalar e configurar o Terraform para Google Cloud no Cloud Shell e em um shell local. O Cloud Shell é um ambiente shell interativo para o Google Cloud que permite aprender e testar o Google Cloud e gerenciar projetos e recursos do seu navegador da Web.

Cloud Shell

  1. Para usar um terminal on-line com a gcloud CLI e o Terraform já configurados, ative o Cloud Shell:

    Na parte de baixo desta página, uma sessão do Cloud Shell é iniciada e exibe um prompt de linha de comando. A inicialização da sessão pode levar alguns segundos.

  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. Para provisionar recursos do Google Cloud usando o Terraform, você precisa: ativar as APIs correspondentes:

    gcloud services enable "API"
    

    Substitua API pela API que você quer ativar.

  5. Para provisionar recursos do Google Cloud usando o Terraform, você precisa papéis do Identity and Access Management específicos a esses recursos.

    1. Na guia Referência dos papéis do IAM determinar os papéis necessários.
    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. Execute o seguinte comando para verificar se o Terraform está disponível:

    terraform
    

    A saída será semelhante a esta:

    
    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 local

  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. Ative as APIs necessárias:

    gcloud services enable "API"
    

    Em que API é a API que você quer ativar.

  5. Para provisionar um recurso do Google Cloud usando o Terraform, você precisa papéis do Identity and Access Management específicos a esses recursos.

    1. Na guia Referência dos papéis do IAM determinar os papéis necessários.
    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. Instalar o Terraform

  7. Execute o seguinte comando para verificar se o Terraform está disponível:

    terraform
    

    A saída será semelhante a esta:

    
     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
    

A seguir