Function Connect To VPC Resources Only Remediation
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the misconfiguration "Cloud Functions Should Connect To Resources In VPC only" for GCP using GCP console, follow the below steps:
- Open the Google Cloud Console and select the project where the Cloud Function is located.
- In the left-hand navigation menu, select "VPC Network" and then select "VPC networks".
- Select the VPC network that you want to use for your Cloud Function.
- In the "VPC Network Details" page, select "Add subnet".
- In the "Create subnet" page, enter a name for the subnet and select the region where you want the subnet to be located.
- Configure the subnet IP range and select the VPC network that you want to use for your Cloud Function.
- Click "Create" to create the subnet.
- In the left-hand navigation menu, select "Cloud Functions" and then select the Cloud Function that you want to configure.
- In the "Cloud Function Details" page, click on the "Edit" button.
- Scroll down to the "VPC connector" section and select the VPC connector that you created in step 6.
- Click "Save" to save the changes.
After following these steps, your Cloud Function will be configured to connect to resources in the specified VPC only.
Using CLI
To remediate the misconfiguration of allowing Cloud Functions to connect to resources outside of VPC on GCP, you can follow the below steps using the GCP CLI:
- First, create a VPC network and subnet in your GCP project using the following command:
gcloud compute networks create [NETWORK_NAME] --subnet-mode custom
- Next, create a subnet within the VPC network using the following command:
gcloud compute networks subnets create [SUBNET_NAME] --network [NETWORK_NAME] --region [REGION] --range [IP_RANGE]
Replace [SUBNET_NAME], [NETWORK_NAME], [REGION], and [IP_RANGE] with appropriate values.
- Now, you need to deploy the Cloud Function within the VPC network by specifying the
--vpc-connectorflag with thegcloud functions deploycommand. The--vpc-connectorflag specifies the name of the Serverless VPC Access connector to use for the function.
gcloud functions deploy [FUNCTION_NAME] --vpc-connector [CONNECTOR_NAME] --entry-point [ENTRY_POINT] --runtime [RUNTIME] --trigger-http
Replace [FUNCTION_NAME], [CONNECTOR_NAME], [ENTRY_POINT], and [RUNTIME] with appropriate values.
- Finally, create a Serverless VPC Access connector using the following command:
gcloud compute networks vpc-access connectors create [CONNECTOR_NAME] --network [NETWORK_NAME] --region [REGION] --range [IP_RANGE]
Replace [CONNECTOR_NAME], [NETWORK_NAME], [REGION], and [IP_RANGE] with appropriate values.
By following the above steps, you can remediate the misconfiguration of allowing Cloud Functions to connect to resources outside of VPC on GCP and ensure that all Cloud Functions are only able to connect to resources within the VPC network.
Using Python
To remediate this misconfiguration in GCP, you can follow the below steps using Python:
Step 1: Create a VPC connector
- First, create a VPC connector in the same region as your Cloud Function.
- You can create a VPC connector using the
google.cloud.functions.v1.VpcConnectorclient library in Python.
from google.cloud.functions.v1 import VpcConnector
from google.cloud.functions_v1.types import VpcConnector
client = VpcConnector()
parent = "projects/<your-project-id>/locations/<your-region>"
connector = VpcConnector(display_name="<your-connector-name>", uri="<your-connector-uri>")
response = client.create_vpc_connector(parent=parent, connector=connector)
Step 2: Update the Cloud Function to use the VPC connector
- Next, update the Cloud Function to use the VPC connector you created in step 1.
- You can update the Cloud Function using the
google.cloud.functions.v1.CloudFunctionsServiceClientclient library in Python.
from google.cloud.functions.v1 import CloudFunctionsServiceClient
from google.cloud.functions_v1.types import CloudFunction
client = CloudFunctionsServiceClient()
function_name = "<your-function-name>"
function = client.get_cloud_function(name=function_name)
function.vpc_connector = "<your-connector-name>"
response = client.update_cloud_function(update_mask=field_mask, function=function)
Step 3: Test the updated Cloud Function
- Finally, test the updated Cloud Function to ensure that it can only connect to resources in the VPC.
- You can test the Cloud Function by invoking it and verifying that it can only access resources in the VPC.
Note: Make sure to replace the placeholders <your-project-id>, <your-region>, <your-connector-name>, <your-connector-uri>, and <your-function-name> with the appropriate values for your GCP environment.
Using Terraform
# Create (or reference) a VPC Access Connector that your Cloud Function will use
resource "google_vpc_access_connector" "FUNCTION_VPC_CONNECTOR" {
name = "FUNCTION_VPC_CONNECTOR_NAME" # e.g. "projects/PROJECT_ID/locations/REGION/connectors/FUNCTION-VPC-CONNECTOR"
region = "FUNCTION_REGION" # e.g. "us-central1"
network = "VPC_NETWORK_NAME" # e.g. "default" or "my-vpc"
ip_cidr_range = "10.8.0.0/28" # adjust to an unused RFC1918 range in your VPC
}
# Gen 1 Cloud Function configured to use the VPC connector and send all egress via VPC
resource "google_cloudfunctions_function" "FUNCTION_NAME" {
name = "FUNCTION_NAME" # replace with your function name
description = "My secured Cloud Function"
runtime = "FUNCTION_RUNTIME" # e.g. "python39"
region = "FUNCTION_REGION" # must match connector region
entry_point = "FUNCTION_ENTRYPOINT" # e.g. "handler"
trigger_http = true
source_archive_bucket = "SOURCE_BUCKET_NAME"
source_archive_object = "SOURCE_ARCHIVE_OBJECT"
# Attach the VPC connector so the function uses VPC networking
vpc_connector = google_vpc_access_connector.FUNCTION_VPC_CONNECTOR.name
# Ensure all outbound traffic from the function goes through the VPC connector
vpc_connector_egress_settings = "ALL_TRAFFIC" # options: "ALL_TRAFFIC" or "PRIVATE_RANGES_ONLY"
}
# If you are using Cloud Functions Gen 2 instead, the equivalent is:
resource "google_cloudfunctions2_function" "FUNCTION2_NAME" {
name = "FUNCTION2_NAME" # projects/PROJECT_ID/locations/REGION/functions/FUNCTION2_NAME
location = "FUNCTION_REGION"
build_config {
runtime = "FUNCTION_RUNTIME"
entry_point = "FUNCTION_ENTRYPOINT"
source {
storage_source {
bucket = "SOURCE_BUCKET_NAME"
object = "SOURCE_ARCHIVE_OBJECT"
}
}
}
service_config {
# Attach the same VPC connector
vpc_connector = google_vpc_access_connector.FUNCTION_VPC_CONNECTOR.name
# Route all egress through the VPC
vpc_connector_egress_settings = "ALL_TRAFFIC"
}
event_trigger {
trigger_region = "FUNCTION_REGION"
event_type = "google.cloud.functions.v2.functionCreated" # REPLACE with your actual trigger
}
}
Changing or adding the VPC connector settings will trigger a new deployment of the Cloud Function (configuration update) but not an outage-causing resource replacement with a different name.
To verify, terraform plan should show:
- a new
google_vpc_access_connectorresource (if not already existing), and - an in-place update to the
google_cloudfunctions_functionorgoogle_cloudfunctions2_functionaddingvpc_connectorandvpc_connector_egress_settings = "ALL_TRAFFIC".