To mitigate the risk of data exfiltration from your AlloyDB for PostgreSQL instances,
you can use VPC Service Controls to configure service perimeters and access
levels that protect your AlloyDB resources and data.

For a general overview of VPC Service Controls, its security benefits, and
its capabilities across Google Cloud products, see
[Overview of VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/overview.md.txt).

## Before you begin

1. In the Google Cloud console, go to the **Project Selector** page.

   [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)
2. Select or [create a Google Cloud project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects.md.txt).

   > [!NOTE]
   > **Note:** If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

3. Make sure that billing is enabled for your Google Cloud project. Learn how to [check if billing is enabled on a project](https://docs.cloud.google.com/billing/docs/how-to/verify-billing-enabled.md.txt).
4. Enable the Compute Engine API. [Enable the
   Compute Engine API](https://console.cloud.google.com/apis/enableflow?apiid=compute.googleapis.com)

5. Enable the Service Networking API. [Enable the
   Service Networking API](https://console.cloud.google.com/apis/enableflow?apiid=servicenetworking.googleapis.com)

6. Add the [Identity and Access Management (IAM) roles](https://docs.cloud.google.com/vpc-service-controls/docs/access-control.md.txt#required_roles) to the user or service account that you're using to set up and administer VPC Service Controls. For more information, see [IAM roles for administering VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/access-control.md.txt).
7. Review [limitations](https://docs.cloud.google.com/vpc-service-controls/docs/supported-products.md.txt#table_alloydb_api) when you use VPC Service Controls with AlloyDB.

> [!NOTE]
> **Note:** If your AlloyDB cluster resides in a [Shared VPC](https://docs.cloud.google.com/vpc/docs/shared-vpc.md.txt) network, you must include both your AlloyDB service project (for example, `projects/PROJECT_NUMBER`) and the host project or the Shared VPC network resource (for example, `//compute.googleapis.com/projects/HOST_PROJECT_ID/global/networks/SHARED_VPC_NETWORK`) in the [service
> perimeter](https://docs.cloud.google.com/vpc-service-controls/docs/troubleshooting#shared_vpc).

## Secure AlloyDB using VPC Service Controls

Before you begin, review [Overview of VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/overview.md.txt)
and [AlloyDB limitations when using VPC Service Controls](https://docs.cloud.google.com/vpc-service-controls/docs/supported-products.md.txt#table_alloydb_api).

Configuring VPC Service Controls for an AlloyDB project includes
the following steps:

1. [Create and manage a service perimeter](https://docs.cloud.google.com/alloydb/docs/vpc-sc/configure-vpc-service-controls.md.txt#create-manage-perimeters).

   First, you select the AlloyDB project that you want the VPC
   service perimeter to protect, and then you create and manage the service
   perimeter.
2. [Create and manage access levels](https://docs.cloud.google.com/alloydb/docs/vpc-sc/configure-vpc-service-controls.md.txt#create-manage-access-levels).

   Optionally, to permit external access to protected resources inside a perimeter,
   you can use access levels. Access levels apply only to requests for protected
   resources coming from outside the service perimeter. You can't use access levels
   to give protected resources or VMs permission to access data and services
   outside the perimeter.

## Create and manage a service perimeter

To create and manage a service perimeter, complete the following steps:

1. Select the AlloyDB project that you want the VPC service perimeter
   to protect.

2. Create a service perimeter by following the instructions in
   [Creating a service perimeter](https://docs.cloud.google.com/vpc-service-controls/docs/create-service-perimeters.md.txt).

3. Add more instances to the service perimeter. To add existing AlloyDB
   instances to the perimeter, follow the instructions in
   [Updating a service perimeter](https://docs.cloud.google.com/vpc-service-controls/docs/manage-service-perimeters.md.txt#update).

4. Add APIs to the service perimeter. To mitigate the risk of your data being
   exfiltrated from AlloyDB, you must restrict the AlloyDB API,
   Compute Engine API, Cloud Storage API, Artifact Registry API,
   Certificate Authority Service API, and Cloud Key Management Service API. For more
   information about updating service perimeters, see the
   [`gcloud access-context-manager perimeters update`](https://docs.cloud.google.com/sdk/gcloud/reference/access-context-manager/perimeters/update.md.txt)
   command.

   To add APIs as restricted services, follow these steps:

   ### Console

   1. In the Google Cloud console, go to the **VPC Service Controls** page.

      [Go to VPC Service Controls](https://console.cloud.google.com/projectselector2/security/service-perimeter)
   2. In the **VPC Service Controls** page, in the table, click the name of the service perimeter that you want to modify.
   3. Click **Edit**.
   4. In the **Edit VPC Service Perimeter** page, click **Add Services**.
   5. Add **AlloyDB API** , **Compute Engine API** , **Cloud Storage API** , **Artifact Registry API** , **Certificate Authority Service API** , and **Cloud KMS API**.
   6. Click **Save**.

   ### gcloud

   ```
   gcloud access-context-manager perimeters update PERIMETER_ID \
   --policy=POLICY_ID \
   --add-restricted-services=alloydb.googleapis.com,compute.googleapis.com,storage.googleapis.com,artifactregistry.googleapis.com,privateca.googleapis.com,cloudkms.googleapis.com
   ```

   Replace the following:
   - `PERIMETER_ID`: the ID of the perimeter or the fully qualified identifier for the perimeter.
   - `POLICY_ID`: the ID of the access policy.
5. If you
   [enabled advanced query insights features](https://docs.cloud.google.com/alloydb/docs/using-advanced-query-insights.md.txt#enable-advanced-features),
   add the `databaseinsights.googleapis.com` API to the service perimeter as a
   restricted service:

   ### Console

   1. In the Google Cloud console, go to the **VPC Service Controls** page.

      [Go to VPC Service Controls](https://console.cloud.google.com/projectselector2/security/service-perimeter)
   2. In the **VPC Service Controls** page, in the table, click the name of the service perimeter that you want to modify.
   3. Click **Edit**.
   4. In the **Edit VPC Service Perimeter** page, click **Add Services**.
   5. Add **databaseinsights.googleapis.com**.
   6. Click **Save**.

   ### gcloud

   ```
   gcloud access-context-manager perimeters update PERIMETER_ID \
   --policy=POLICY_ID \
   --add-restricted-services=databaseinsights.googleapis.com
   ```

   Replace the following:
   - `PERIMETER_ID`: the ID of the perimeter or the fully qualified identifier for the perimeter.
   - `POLICY_ID`: the ID of the access policy.

## Create and manage access levels

To create and manage access levels, follow the instructions in
[Allowing access to protected resources from outside a perimeter](https://docs.cloud.google.com/vpc-service-controls/docs/use-access-levels.md.txt).