Deploy a VPC with Terraform

You can deploy a Virtual Private Cloud (VPC) network by using Infrastructure Manager and the Google Cloud console. This quickstart is for cloud administrators, platform engineers, and developers who want to manage Google Cloud infrastructure as code. A VPC network provides isolated networking for resources such as virtual machine (VM) instances, databases, and apps.

With Infra Manager, you define the network in a Terraform configuration instead of creating it manually. Infra Manager runs Terraform for you and stores the Terraform state. As a result, you can reproduce, update, and delete your infrastructure consistently. For more information, see Overview of Terraform on Google Cloud.

In this quickstart, you preview and create a deployment from a Terraform configuration in a public GitHub repository.

Before you begin

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Infrastructure Manager and Compute Engine APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

Required roles

To get the permissions that you need to complete this quickstart, ask your administrator to grant you the following IAM roles on the project:

For more information about granting roles, see Manage access to projects, folders, and organizations.

You might also be able to get the required permissions through custom roles or other predefined roles.

Preview and create a deployment

When you create a deployment, Infra Manager provisions the resources that are defined in the Terraform configuration. Before you create the deployment, you create a preview to verify which resources Infra Manager plans to provision.

To preview and create a deployment that provisions a VPC network, do the following:

  1. In the Google Cloud console, go to the Infrastructure Manager page.

    Go to Infrastructure Manager

  2. Click Create new deployment.

  3. On the Create new Terraform deployment page, in the Deployment details section, enter the following values:

    • Deployment ID: quickstart-preview
    • Region: us-central1
    • Terraform version: 1.5.7
    • Service account: Create a new service account named im-quickstart-sa. To create the service account and grant it the required roles, do the following:

      1. In the Service account list, click New service account. The Create service account page opens in a new tab.
      2. In the Service account name field, enter im-quickstart-sa, and then click Create and continue.
      3. In the Grant this service account access to project section, use the Select a role list to grant the following roles. To add the second role, click Add another role.

        • Cloud Infrastructure Manager Agent (roles/config.agent): lets Infra Manager run Terraform and store the deployment's state and logs.
        • Compute Network Admin (roles/compute.networkAdmin): lets Infra Manager create and manage the VPC network.

        For more information about Infra Manager roles, see Access control with IAM.

      4. Click Continue, and then click Done.

      5. Close the tab and return to the Create new Terraform deployment page.

      6. In the Service account list, click Refresh list, and then select im-quickstart-sa.

    • Source of Terraform configuration: Git

    • Git repository: https://github.com/terraform-google-modules/terraform-google-network

    • Git directory: modules/vpc

  4. Click Continue.

  5. In the Terraform details section, under Input values, enter the following values for the pre-populated module variables:

    • In the row for the project_id key, in the Value field, enter <var>PROJECT_ID</var>. Don't include quotation marks.
    • In the row for the network_name key, in the Value field, enter quickstart-vpc. Don't include quotation marks.

    Replace PROJECT_ID with your Google Cloud project ID.

  6. Click Create preview.

  7. On the details page for the quickstart-preview-<var>UUID</var> preview, verify that the value in the State row is Succeeded.

  8. To create a deployment that uses the values from the preview, click Create deployment.

  9. In the Deployment ID field, enter quickstart-deployment.

  10. Click Create deployment.

    Infra Manager provisions the VPC network. When the deployment is complete, you receive a notification in the Google Cloud console.

You created a deployment by using Infra Manager. In the following sections, you verify the deployment, view the provisioned network, and review the Cloud Build job that ran Terraform.

View the status of the deployment

After Infra Manager creates the deployment, you can view details about it, such as its state, its latest revision, and the source of its Terraform configuration.

To view the details of the deployment, do the following:

  1. In the Google Cloud console, go to the Infrastructure Manager page.

    Go to Infrastructure Manager

  2. In the ID column, click quickstart-deployment.

  3. On the deployment details page, verify that the value in the State row is Active.

View the provisioned VPC network

To confirm that Infra Manager provisioned the network that the Terraform configuration defines, view the network in the Google Cloud console:

  1. In the Google Cloud console, go to the VPC networks page.

    Go to VPC networks

    The list of networks includes quickstart-vpc, which is the network that Infra Manager provisioned. The list shows details about the network, such as its subnet creation mode, its maximum transmission unit (MTU), and its dynamic routing mode.

  2. To view more details about the network, such as its subnets, firewall rules, and routes, click quickstart-vpc.

View the build results in Cloud Build

Infra Manager uses Cloud Build to run Terraform for each deployment revision. To inspect the Terraform commands that created your deployment, or to troubleshoot a deployment that fails, view the Cloud Build build logs:

  1. In the Google Cloud console, go to the Build history page.

    Go to Build history

  2. In the builds table, find the most recent build with the region us-central1. Builds for both the quickstart-preview-<var>UUID</var> preview and the quickstart-deployment deployment appear in this list.

  3. Click the build ID to open the Build details page.

    The Build details page shows the status of each build step and the build log, which includes the output of the Terraform commands. If a deployment fails, use the build log to identify the cause. For more information, see Troubleshoot deployment errors.

You can also open the build for a deployment from the deployment details page in Infra Manager by clicking the Cloud Build logs link.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, delete the deployment when you no longer need it. Deployment previews don't incur charges to your Google Cloud account. If you want to delete the preview, see Delete previews.

Delete the deployment

Delete the deployment and the provisioned VPC network:

To delete the deployment, do the following:

  1. In the Google Cloud console, go to the Infrastructure Manager page.

    Go to Infrastructure Manager

  2. In the ID column, click quickstart-deployment.

  3. On the deployment details page, click Delete.

  4. In the confirmation dialog, enter quickstart-deployment, and then click Confirm.

Delete the service account

After Infra Manager deletes the deployment, delete the im-quickstart-sa service account so that it doesn't keep its roles in your project:

  1. In the Google Cloud console, go to the Service accounts page.

    Go to Service accounts

  2. Select the checkbox for im-quickstart-sa, and then click Delete.

  3. In the dialog, click Delete.

Optional: Delete the project

If you created the deployment in a new Google Cloud project and you no longer need the project, then delete it.

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.

  3. In the dialog, enter the project ID, and then click Shut down.

What's next