Install and configure Terraform

This page describes the steps to install and configure Terraform for Google Cloud in Cloud Shell and in a local shell. Cloud Shell is an interactive shell environment for Google Cloud that lets you learn and experiment with Google Cloud and manage your projects and resources from your web browser.

Cloud Shell

  1. To use an online terminal with the gcloud CLI and Terraform already set up, activate Cloud Shell:

    At the bottom of this page, a Cloud Shell session starts and displays a command-line prompt. It can take a few seconds for the session to initialize.

  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. To provision Google Cloud resources using Terraform, you must enable the corresponding APIs:

    gcloud services enable "API"
    

    Replace API with the API you want to enable.

  5. To provision Google Cloud resources using Terraform, you require Identity and Access Management roles that are specific to these resources.

    1. From the IAM roles reference page, determine the required role(s).
    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. Run the following command to verify that Terraform is available:

    terraform
    

    The output should be similar to the following:

    
    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
    
    

Local shell

  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. Enable the required APIs:

    gcloud services enable "API"
    

    Where API is the API you want to enable.

  5. To provision Google Cloud resource using Terraform, you require Identity and Access Management roles that are specific to these resources.

    1. From the IAM roles reference page, determine the required role(s).
    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. Install Terraform.

  7. Run the following command to verify that Terraform is available:

    terraform
    

    The output should be similar to the following:

    
     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
    

What's next