# Deploy a VPC with Terraform

Learn how to use Infrastructure Manager to deploy a Virtual Private Cloud (VPC).

This quickstart uses a Terraform configuration that is stored in a public GitHub
repository. The configuration defines the VPC to provision.

## Before you begin

<br />

## Grant permissions for resources in the configuration

You have granted permissions needed to run Infra Manager, but you
also need to grant permissions that are specific to the resources described
in the configuration you are deploying.

Grant permissions for the VPC, which is the resource
defined in the Terraform configuration.

```terraform
gcloud projects add-iam-policy-binding PROJECT_ID \
    --member=serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
    --role=roles/compute.networkAdmin
```

Replace the following:

- <var translate="no">SERVICE_ACCOUNT_NAME</var>: the name of the service account.
- <var translate="no">PROJECT_ID</var>: your project ID.

## Preview the deployment

Before you create a deployment, you can create a preview of the deployment.
You can use this preview to verify the resources that are planned to be
provisioned.

The following command has four values populated that don't have a default value.
These values are: your project ID, the service account name, the location
`us-central1`, and the name `quickstart-vpc` for the network you're creating.

To create a preview, use the following command:

```terraform
    gcloud infra-manager previews create projects/PROJECT_ID/locations/us-central1/previews/quickstart-preview \
        --service-account projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
        --git-source-repo=https://github.com/terraform-google-modules/terraform-google-network \
        --git-source-directory=modules/vpc \
        --git-source-ref=main \
        --input-values=project_id=PROJECT_ID,network_name=quickstart-vpc
```

After you create a preview, you can review the preview results.
This quickstart skips this step, but you can see
[Export and view preview results](https://docs.cloud.google.com/infrastructure-manager/docs/export-view-preview-results.md.txt)
if you want more details.

## Create a deployment

Use Infra Manager to create a deployment. This means that
Infra Manager provisions the resources that are defined in the
Terraform configuration.

The configuration for this quickstart has four values that don't have a default
value. The following command adds these values: your project ID, the
service account name, the location `us-central1`, and the name `quickstart-vpc`
for the network you're creating.

```terraform
gcloud infra-manager deployments apply projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment \
    --service-account=projects/PROJECT_ID/serviceAccounts/SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \
    --git-source-repo=https://github.com/terraform-google-modules/terraform-google-network \
    --git-source-directory=modules/vpc \
    --git-source-ref=main \
    --input-values=project_id=PROJECT_ID,network_name=quickstart-vpc
```

When the deployment is done, you see:

    Creating the deployment...done

The VPC is now created and configured as described in the
configuration.

## View the build results in Cloud Build

To view the Cloud Build job that Infra Manager used to
create the deployment, open the **Build History** page in the Google Cloud console:

[Open the Build History page](https://console.cloud.google.com/cloud-build/builds)

## View the status of the deployment

Now that the deployment is done, you can view its description to review
information about it, including the state.

View the description of the deployment:

```terraform
gcloud infra-manager deployments describe projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment
```

The output has details about the deployment, including the timestamp of
the deployment, the name of the latest revision, and the state.

For the state, you see `ACTIVE`.

## View details about the provisioned VPC

View details about the provisioned VPC:

```terraform
gcloud infra-manager resources list --revision=projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment/revisions/REVISION_ID
```

Replace <var translate="no">REVISION_ID</var> with the ID of the latest revision. This ID is
`r-0`, unless you deployed multiple times. You can see the ID of the latest
revision in the deployment description in the previous section.

## View the VPC in the console

View the VPC in the console:

[Go to VPC networks](https://console.cloud.google.com/networking/networks/list)

You see the VPC named `quickstart-vpc`. This is the
VPC that Infra Manager provisioned.

## Clean up

To avoid incurring charges to your Google Cloud account for the resources
used on this page, make sure that you delete resources that you no longer need.

### Delete the VPC

Delete the VPC and the metadata about the deployment:

```terraform
gcloud infra-manager deployments delete projects/PROJECT_ID/locations/us-central1/deployments/quickstart-deployment
```

### Optional: Delete the project

If you deployed the solution in a new Google Cloud project, and if you no longer the
project, then delete it by completing the following steps:

> [!CAUTION]
> **Caution:** If you delete a project, all the resources in the project are permanently deleted.

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

   [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)
2. In the project list, select the project that you want to delete, and then click **Delete**.
3. At the prompt, type the project ID, and then click **Shut down**.

## What's next

- To learn more about Infra Manager, see [How Infra Manager works](https://docs.cloud.google.com/infrastructure-manager/docs/overview.md.txt#how_works).
- Learn more about [Infra Manager and Terraform](https://docs.cloud.google.com/infrastructure-manager/docs/infra-manager-terraform.md.txt).
- [Update a deployment](https://docs.cloud.google.com/infrastructure-manager/docs/update-deployment.md.txt).
- [Automate deployments](https://docs.cloud.google.com/infrastructure-manager/docs/automate-git.md.txt).
- For a set of configurations and modules ready to use with Google Cloud, see [Terraform blueprints and modules for Google Cloud](https://docs.cloud.google.com/docs/terraform/blueprints/terraform-blueprints.md.txt).