This tutorial shows you how to configure VPC Service Controls for
Backup and DR auto-protection policies that protect Compute Engine
instances and disks. You provision the workload project service agent and apply
the ingress and egress rules for your perimeter topology.

Auto-protection policies use the Backup and DR Service Agent in the workload
project to manage backup plan associations. This service agent uses the email
format
`service-WORKLOAD_PROJECT_NUMBER@gcp-sa-backupdr.iam.gserviceaccount.com`.
Complete this configuration before you add a perimeter-protected workload or
backup vault project to an auto-protection policy.

This tutorial is intended for security engineers, cloud architects, and backup
administrators who manage Compute Engine backups in
VPC Service Controls environments.

## Objectives

- Provision the Backup and DR Service Agent in a workload project inside a VPC Service Controls service perimeter.
- Identify the ingress and egress rules that match your perimeter topology.
- Apply the required ingress and egress rules to your workload and backup vault service perimeters.
- Verify the provisioned service agent and enforced perimeter rules.

## Costs


In this document, you use the following billable components of Google Cloud:


- [Backup and DR](https://cloud.google.com/backup-disaster-recovery/pricing)
- [Compute Engine](https://cloud.google.com/compute/all-pricing)


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

Initializing the service agent with `validateOnly` set to `true` doesn't create
a backup vault or backup plan. Charges apply when you create
backup vaults, store backups, or run Compute Engine resources.

## Before you begin

1. Identify two Google Cloud projects in your organization:

   - **Backup vault project**: contains your backup vault, backup plan, and auto-protection policy.
   - **Workload project**: contains the Compute Engine instances or disks to protect.
2. Confirm that an Access Context Manager access policy and service perimeter
   protect your backup vault project, your workload project, or both. In the
   VPC Service Controls documentation, see
   [Create a
   service perimeter](https://docs.cloud.google.com/vpc-service-controls/docs/create-service-perimeters).

3. Confirm that your administrator principal can access the target
   perimeters from your command environment, such as Cloud Shell, a
   workstation, or a CI/CD system. Administrators typically gain this access
   through an existing ingress rule with an `accessLevels`
   source.

<br />

### Required roles


To get the permissions that
you need to configure VPC Service Controls for auto-protection policies,

ask your administrator to grant you the
following IAM roles:

- Initialize the workload project service agent: [Backup and DR Admin](https://docs.cloud.google.com/iam/docs/roles-permissions/backupdr.md.txt#backupdr.admin) (`roles/backupdr.admin`) on your workload project
- Verify the workload project allow policy: [Security Reviewer](https://docs.cloud.google.com/iam/docs/roles-permissions/iam.md.txt#iam.securityReviewer) (`roles/iam.securityReviewer`) on your workload project
- Inspect and update service perimeters: [Access Context Manager Editor](https://docs.cloud.google.com/iam/docs/roles-permissions/accesscontextmanager.md.txt#accesscontextmanager.policyEditor) (`roles/accesscontextmanager.policyEditor`) on your organization or scoped access policy


For more information about granting roles, see [Manage access to projects, folders, and organizations](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access.md.txt).


These predefined roles contain

the permissions required to configure VPC Service Controls for auto-protection policies. To see the exact permissions that are
required, expand the **Required permissions** section:


#### Required permissions

The following permissions are required to configure VPC Service Controls for auto-protection policies:

- Initialize the workload project service agent: `backupdr.serviceConfig.initialize` on your workload project
- Verify the workload project allow policy: `resourcemanager.projects.getIamPolicy` on your workload project
- Inspect and update service perimeters:
  - `accesscontextmanager.policies.list` on your organization or scoped access policy
  - `accesscontextmanager.servicePerimeters.get` on your organization or scoped access policy
  - `accesscontextmanager.servicePerimeters.update` on your organization or scoped access policy


You might also be able to get
these permissions
with [custom roles](https://docs.cloud.google.com/iam/docs/creating-custom-roles.md.txt) or
other [predefined roles](https://docs.cloud.google.com/iam/docs/roles-overview.md.txt#predefined).

## Prepare the environment

Gather the project numbers and access policy ID that you need for the YAML
rules and commands. Run all commands from an environment authorized to access
your service perimeters.

1. Get the project number for your workload project. See the
   gcloud CLI documentation for
   [`gcloud projects describe`](https://docs.cloud.google.com/sdk/gcloud/reference/projects/describe):

   ```
   gcloud projects describe WORKLOAD_PROJECT_ID \
       --format="value(projectNumber)"
   ```

   Replace `WORKLOAD_PROJECT_ID` with your workload project
   ID.
2. Get the project number for your backup vault project:

   ```
   gcloud projects describe BACKUP_VAULT_PROJECT_ID \
       --format="value(projectNumber)"
   ```

   Replace `BACKUP_VAULT_PROJECT_ID` with your backup vault
   project ID.
3. Optional: If your projects are in different perimeters, get your access
   policy ID from the `NAME` column. See the gcloud CLI documentation for [`gcloud access-context-manager policies list`](https://docs.cloud.google.com/sdk/gcloud/reference/access-context-manager/policies/list.md.txt):

   ```
   gcloud access-context-manager policies list \
       --organization=ORGANIZATION_ID
   ```

   Replace `ORGANIZATION_ID` with your Google Cloud
   organization ID.

## Provision the Backup and DR Service Agent

If a workload project hasn't created a Backup and DR resource yet,
its Backup and DR Service Agent isn't provisioned. When the workload project is
inside a VPC Service Controls perimeter, you must provision this service
agent from an authorized principal before auto-protection policies can manage
backup plan associations.

> [!NOTE]
> **Note:** If only the backup vault project is inside a VPC Service Controls perimeter, Backup and DR can provision the workload service agent automatically. To verify the service agent before you apply backup vault perimeter rules for Scenario D, initialize it manually.

### Limitations

The `projects.locations.serviceConfig.initialize` request has the following
limitation:

- Setting `validateOnly` to `true` still validates the location and customer-managed encryption key (CMEK) organization policies for a default backup vault. If those policies restrict the requested region or require CMEK, then the request can fail.

### Initialize the service configuration

Call the
[`projects.locations.serviceConfig.initialize`](https://docs.cloud.google.com/backup-disaster-recovery/docs/reference/rest/v1/projects.locations.serviceConfig/initialize.md.txt)
method with `validateOnly` set to `true`. This request provisions the service
agent without creating a default backup vault or default backup plan.

From an environment authorized to access your VPC Service Controls
perimeter, send the following `POST` request:

```
curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://backupdr.googleapis.com/v1/projects/WORKLOAD_PROJECT_ID/locations/LOCATION/serviceConfig:initialize" \
    -d '{
      "resourceType": "compute.googleapis.com/Instance",
      "validateOnly": true
    }'
```

Replace the following:

- `WORKLOAD_PROJECT_ID`: the project ID of the workload project.
- `LOCATION`: the region of your workload resources and auto-protection policy (for example, `us-central1`).

When `validateOnly` is `true`, Backup and DR provisions
`service-WORKLOAD_PROJECT_NUMBER@gcp-sa-backupdr.iam.gserviceaccount.com`
and grants it the Backup and DR Service Agent (`roles/backupdr.serviceAgent`)
role. The method returns a completed operation without creating a default
backup vault or backup plan.

## Identify your perimeter topology

The required ingress and egress rules depend on how your service perimeters
enclose your backup vault and workload projects. The following table summarizes
the required steps for each topology.

| Perimeter topology | Service agent initialization (`validateOnly: true`) | Required perimeter rules |
|---|---|---|
| **Scenario A:** Same perimeter | Required | None. |
| **Scenario B:** Different perimeters | Required | Configure both the workload and backup vault perimeter rules. Also configure the [backup vault service agent rules for Compute Engine](https://docs.cloud.google.com/backup-disaster-recovery/docs/configuration/vpc-sc.md.txt#gce-vpc). |
| **Scenario C:** Only workload project in a perimeter | Required | Configure the workload perimeter rules. Also configure the [backup vault service agent rules for Compute Engine](https://docs.cloud.google.com/backup-disaster-recovery/docs/configuration/vpc-sc.md.txt#gce-vpc). |
| **Scenario D:** Only backup vault project in a perimeter | Recommended (or automatic) | Configure the backup vault perimeter rules. Also configure the [backup vault service agent rules for Compute Engine](https://docs.cloud.google.com/backup-disaster-recovery/docs/configuration/vpc-sc.md.txt#gce-vpc). |

### Scenario A: Projects in the same service perimeter

If your backup vault project and workload project share a
VPC Service Controls perimeter, you don't need extra ingress or egress
rules. After you provision the workload service agent, continue to
[Verify the configuration](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/vpc-sc.md.txt#verify-configuration).

### Scenarios B, C, and D: Projects across perimeter boundaries

When your backup vault project and workload project aren't in the same
perimeter, cross-project API calls occur across three operations:

1. **Creating the policy binding:** The administrator principal creates the binding in the backup vault project. The binding `scope` field references `projects/WORKLOAD_PROJECT_ID`.
2. **Managing backup plan associations:** The workload project's Backup and DR Service Agent creates or updates backup plan associations in the workload project. Those associations reference `projects/BACKUP_VAULT_PROJECT_ID/locations/LOCATION/backupPlans/BACKUP_PLAN_ID` in the backup vault project.
3. **Executing Compute Engine backups:** The backup vault service agent backs up Compute Engine instances and disks in the workload project.

In Scenarios B, C, and D, also configure the
[backup vault service agent rules for Compute Engine](https://docs.cloud.google.com/backup-disaster-recovery/docs/configuration/vpc-sc.md.txt#gce-vpc)
alongside the rules in the next section.

## Configure perimeter ingress and egress rules

For Scenarios B, C, and D, update the enforced ingress and egress policies on
the applicable perimeters. Complete the workload perimeter steps for Scenarios
B and C, and complete the backup vault perimeter steps for Scenarios B and D.

> [!CAUTION]
> **Caution:** The `--set-ingress-policies` and `--set-egress-policies` flags replace all enforced ingress or egress rules on a perimeter. Export existing rules first and save a backup copy. Include all existing rule entries in your YAML files before you run `update`.

### Configure workload perimeter rules (Scenarios B and C)

These rules allow the workload service agent to call `backupdr.googleapis.com`
in the backup vault project. These rules also allow the administrator principal
to reference the workload project when you create a policy binding.

1. Export and back up the existing workload perimeter rules. See the
   gcloud CLI documentation for [`gcloud access-context-manager perimeters describe`](https://docs.cloud.google.com/sdk/gcloud/reference/access-context-manager/perimeters/describe.md.txt):

   ```
   gcloud access-context-manager perimeters describe \
       WORKLOAD_PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --format="yaml(status.ingressPolicies,status.egressPolicies)" \
       > workload-perimeter-backup.yaml
   ```

   Replace the following:
   - `WORKLOAD_PERIMETER_NAME`: the workload service perimeter name.
   - `ACCESS_POLICY_ID`: your Access Context Manager access policy ID.
2. Create a file named `workload-egress.yaml` and copy any existing items from
   `status.egressPolicies` in `workload-perimeter-backup.yaml`. Then append
   the following egress rule:

   ```yaml
   # Egress rule in the WORKLOAD project's service perimeter
   - egressFrom:
       identities:
       - serviceAccount:service-WORKLOAD_PROJECT_NUMBER@gcp-sa-backupdr.iam.gserviceaccount.com
     egressTo:
       operations:
       - serviceName: backupdr.googleapis.com
         methodSelectors:
         - method: "*"
       resources:
       - projects/BACKUP_VAULT_PROJECT_NUMBER
   ```

   Replace the following:
   - `WORKLOAD_PROJECT_NUMBER`: your workload project number.
   - `BACKUP_VAULT_PROJECT_NUMBER`: your backup vault project number.
3. Create a file named `workload-ingress.yaml` and copy any existing items
   from `status.ingressPolicies` in `workload-perimeter-backup.yaml`. Then
   append the following ingress rule:

   ```yaml
   # Ingress rule in the WORKLOAD project's service perimeter
   - ingressFrom:
       identities:
       - ADMIN_PRINCIPAL
       sources:
       - resource: projects/BACKUP_VAULT_PROJECT_NUMBER
     ingressTo:
       operations:
       - serviceName: backupdr.googleapis.com
         methodSelectors:
         - method: "*"
       resources:
       - projects/WORKLOAD_PROJECT_NUMBER
   ```

   Replace the following:
   - `ADMIN_PRINCIPAL`: the user or service account that creates the policy binding (for example, `user:dana@example.com` or `serviceAccount:tf-runner@PROJECT_ID.iam.gserviceaccount.com`).
   - `BACKUP_VAULT_PROJECT_NUMBER`: your backup vault project number.
   - `WORKLOAD_PROJECT_NUMBER`: your workload project number.
4. Apply the updated rules to your workload perimeter. See the
   gcloud CLI documentation for [`gcloud access-context-manager perimeters update`](https://docs.cloud.google.com/sdk/gcloud/reference/access-context-manager/perimeters/update.md.txt):

   ```
   gcloud access-context-manager perimeters update \
       WORKLOAD_PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --set-egress-policies=workload-egress.yaml \
       --set-ingress-policies=workload-ingress.yaml
   ```

   Replace `WORKLOAD_PERIMETER_NAME` with your workload
   service perimeter name, and replace `ACCESS_POLICY_ID`
   with your Access Context Manager access policy ID.

### Configure backup vault perimeter rules (Scenarios B and D)

These rules allow the workload service agent to call `backupdr.googleapis.com`
in the backup vault project. These rules also allow the administrator principal
to reference the workload project from the backup vault project. The
administrator principal still needs ingress access to this perimeter from its
command environment to manage Backup and DR resources.

1. Export and back up the existing backup vault perimeter rules:

   ```
   gcloud access-context-manager perimeters describe \
       BACKUP_VAULT_PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --format="yaml(status.ingressPolicies,status.egressPolicies)" \
       > vault-perimeter-backup.yaml
   ```

   Replace the following:
   - `BACKUP_VAULT_PERIMETER_NAME`: the backup vault service perimeter name.
   - `ACCESS_POLICY_ID`: your Access Context Manager access policy ID.
2. Create a file named `vault-ingress.yaml` and copy any existing items from
   `status.ingressPolicies` in `vault-perimeter-backup.yaml`. Then append the
   following ingress rule:

   ```yaml
   # Ingress rule in the BACKUP VAULT project's service perimeter
   - ingressFrom:
       identities:
       - serviceAccount:service-WORKLOAD_PROJECT_NUMBER@gcp-sa-backupdr.iam.gserviceaccount.com
       sources:
       - resource: projects/WORKLOAD_PROJECT_NUMBER
     ingressTo:
       operations:
       - serviceName: backupdr.googleapis.com
         methodSelectors:
         - method: "*"
       resources:
       - projects/BACKUP_VAULT_PROJECT_NUMBER
   ```

   Replace the following:
   - `WORKLOAD_PROJECT_NUMBER`: your workload project number.
   - `BACKUP_VAULT_PROJECT_NUMBER`: your backup vault project number.
3. Create a file named `vault-egress.yaml` and copy any existing items from
   `status.egressPolicies` in `vault-perimeter-backup.yaml`. Then append the
   following egress rule:

   ```yaml
   # Egress rule in the BACKUP VAULT project's service perimeter
   - egressFrom:
       identities:
       - ADMIN_PRINCIPAL
     egressTo:
       operations:
       - serviceName: backupdr.googleapis.com
         methodSelectors:
         - method: "*"
       resources:
       - projects/WORKLOAD_PROJECT_NUMBER
   ```

   Replace the following:
   - `ADMIN_PRINCIPAL`: the user or service account that creates the policy binding (for example, `user:dana@example.com` or `serviceAccount:tf-runner@PROJECT_ID.iam.gserviceaccount.com`).
   - `WORKLOAD_PROJECT_NUMBER`: your workload project number.
4. Apply the updated rules to your backup vault perimeter:

   ```
   gcloud access-context-manager perimeters update \
       BACKUP_VAULT_PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --set-ingress-policies=vault-ingress.yaml \
       --set-egress-policies=vault-egress.yaml
   ```

   Replace `BACKUP_VAULT_PERIMETER_NAME` with your backup
   vault service perimeter name, and replace
   `ACCESS_POLICY_ID` with your Access Context Manager access
   policy ID.

## Verify the configuration

Confirm that the Backup and DR Service Agent has its required role binding in
the workload project. For Scenarios B, C, and D, also confirm that each updated
perimeter enforces your new rules.

1. Check the workload project allow policy for the
   `roles/backupdr.serviceAgent` role. See the gcloud CLI documentation for [`gcloud projects get-iam-policy`](https://docs.cloud.google.com/sdk/gcloud/reference/projects/get-iam-policy):

   ```
   gcloud projects get-iam-policy WORKLOAD_PROJECT_ID \
       --flatten="bindings[].members" \
       --filter="bindings.role:roles/backupdr.serviceAgent" \
       --format="value(bindings.members)"
   ```

   Replace `WORKLOAD_PROJECT_ID` with your workload project
   ID.

   The output is similar to the following:

   ```
   serviceAccount:service-WORKLOAD_PROJECT_NUMBER@gcp-sa-backupdr.iam.gserviceaccount.com
   ```
2. For Scenarios B, C, and D, inspect each updated perimeter:

   ```
   gcloud access-context-manager perimeters describe \
       PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --format="yaml(status.ingressPolicies,status.egressPolicies)"
   ```

   Replace `PERIMETER_NAME` with each updated perimeter name,
   and replace `ACCESS_POLICY_ID` with your access policy ID.

   Confirm that `status.ingressPolicies` and `status.egressPolicies` list
   `backupdr.googleapis.com` for the expected identities and project numbers.

## Clean up

To undo the changes from this tutorial and avoid unexpected charges, revert the
perimeter rules in existing projects or delete dedicated test projects.

### Revert perimeter rules

If you modified existing service perimeters, remove the rules that you added
and restore your previous configuration.

1. Recreate your original ingress and egress YAML files from
   `workload-perimeter-backup.yaml` and `vault-perimeter-backup.yaml`.

2. Restore the previous policies on each modified perimeter:

   ```
   gcloud access-context-manager perimeters update \
       PERIMETER_NAME \
       --policy=ACCESS_POLICY_ID \
       --set-ingress-policies=INGRESS_YAML_FILE \
       --set-egress-policies=EGRESS_YAML_FILE
   ```

   Replace the following:
   - `PERIMETER_NAME`: the perimeter name to restore.
   - `ACCESS_POLICY_ID`: your Access Context Manager access policy ID.
   - `INGRESS_YAML_FILE`: the YAML file with original ingress rules.
   - `EGRESS_YAML_FILE`: the YAML file with original egress rules.

   > [!CAUTION]
   > **Caution:** Only use `--clear-ingress-policies` or `--clear-egress-policies` on a dedicated test perimeter that had no prior rules. Clearing policies on a shared perimeter removes all enforced rules and can block authorized access.

### Delete dedicated test projects

If you created dedicated backup vault or workload projects for this tutorial,
delete those projects to stop all billing. See the Resource Manager documentation for [Shut down (delete) a project](https://docs.cloud.google.com/resource-manager/docs/delete-restore-projects.md.txt#shutting_down_projects).

## What's next

- Create and apply auto-protection policies in [Automate resource protection](https://docs.cloud.google.com/backup-disaster-recovery/docs/protect/automate-protection.md.txt).
- Configure the backup vault service agent rules for Compute Engine in [Configure VPC Service Controls for Backup and DR](https://docs.cloud.google.com/backup-disaster-recovery/docs/configuration/vpc-sc.md.txt#gce-vpc).
- Test ingress and egress rules safely in dry-run mode by reading the VPC Service Controls documentation for [Ingress and egress rules](https://docs.cloud.google.com/vpc-service-controls/docs/ingress-egress-rules.md.txt#using-dryrun-ingress-egress-rules).
- Diagnose blocked API calls by reading the VPC Service Controls documentation for [Troubleshoot VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/troubleshooting.md.txt).