Instala y configura Terraform

En esta página, se describen los pasos para instalar y configurar Terraform para Google Cloud en Cloud Shell y en una shell local. Cloud Shell es un entorno de shell interactivo para Google Cloud que te permite aprender y experimentar con Google Cloud, además de administrar tus proyectos y recursos desde tu navegador web.

Cloud Shell

  1. Para usar una terminal en línea con gcloud CLI y Terraform ya configurados, activa Cloud Shell.

    En la parte inferior de esta página, se inicia una sesión de Cloud Shell y se muestra una instrucción de línea de comandos. La sesión puede tardar unos segundos en inicializarse.

  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 aprovisionar recursos de Google Cloud con Terraform, debes habilitar las APIs correspondientes:

    gcloud services enable "API"
    

    Reemplaza API por la API que deseas habilitar.

  5. Para aprovisionar recursos de Google Cloud con Terraform, necesitas roles de Identity and Access Management específicos de estos recursos.

    1. En Referencia de los roles de IAM determina los roles necesarios.
    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. Ejecuta el siguiente comando para verificar que Terraform esté disponible:

    terraform
    

    El resultado debería ser similar al siguiente ejemplo:

    
    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. Habilita las API necesarias:

    gcloud services enable "API"
    

    En las que API es la API que deseas habilitar.

  5. Para aprovisionar recursos de Google Cloud con Terraform, necesitas roles de Identity and Access Management específicos de estos recursos.

    1. En Referencia de los roles de IAM determina los roles necesarios.
    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. Instala Terraform.

  7. Ejecuta el siguiente comando para verificar que Terraform esté disponible:

    terraform
    

    El resultado debería ser similar al siguiente ejemplo:

    
     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
    

¿Qué sigue?