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
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
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.
-
Verify that billing is enabled for your Google Cloud project.
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.enablepermission. 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.
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:
- Cloud Infrastructure Manager Admin (
roles/config.admin) - Service Account Admin (
roles/iam.serviceAccountAdmin) - Service Account User (
roles/iam.serviceAccountUser) - Project IAM Admin (
roles/resourcemanager.projectIamAdmin) - Compute Network Viewer (
roles/compute.networkViewer) - Cloud Build Viewer (
roles/cloudbuild.builds.viewer) - Storage Object Viewer (
roles/storage.objectViewer)
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:
In the Google Cloud console, go to the Infrastructure Manager page.
Click Create new deployment.
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:- In the Service account list, click New service account. The Create service account page opens in a new tab.
- In the Service account name field, enter
im-quickstart-sa, and then click Create and continue. 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.
- Cloud Infrastructure Manager Agent (
Click Continue, and then click Done.
Close the tab and return to the Create new Terraform deployment page.
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-networkGit directory:
modules/vpc
- Deployment ID:
Click Continue.
In the Terraform details section, under Input values, enter the following values for the pre-populated module variables:
- In the row for the
project_idkey, in the Value field, enter<var>PROJECT_ID</var>. Don't include quotation marks. - In the row for the
network_namekey, in the Value field, enterquickstart-vpc. Don't include quotation marks.
Replace PROJECT_ID with your Google Cloud project ID.
- In the row for the
Click Create preview.
On the details page for the
quickstart-preview-<var>UUID</var>preview, verify that the value in the State row is Succeeded.To create a deployment that uses the values from the preview, click Create deployment.
In the Deployment ID field, enter
quickstart-deployment.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:
In the Google Cloud console, go to the Infrastructure Manager page.
In the ID column, click
quickstart-deployment.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:
In the Google Cloud console, go to the VPC networks page.
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.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:
In the Google Cloud console, go to the Build history page.
In the builds table, find the most recent build with the region
us-central1. Builds for both thequickstart-preview-<var>UUID</var>preview and thequickstart-deploymentdeployment appear in this list.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:
In the Google Cloud console, go to the Infrastructure Manager page.
In the ID column, click
quickstart-deployment.On the deployment details page, click Delete.
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:
In the Google Cloud console, go to the Service accounts page.
Select the checkbox for
im-quickstart-sa, and then click Delete.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.
In the Google Cloud console, go to the Manage resources page.
In the project list, select the project that you want to delete, and then click Delete.
In the dialog, enter the project ID, and then click Shut down.
What's next
- Learn how Infra Manager works.
- Learn more about Terraform and Infra Manager.
- Update a deployment.
- Automate deployments.
- For a set of configurations and modules that are ready to use with Google Cloud, see Terraform blueprints and modules for Google Cloud in the Terraform on Google Cloud documentation.