This is now possible via configuring network settings for Cloud Functions particularly Egress Settings.
Taken from the Official Docs:
Via Console:
- Open the Functions Overview page in the Cloud Console
- Click Create function. Alternatively, click an existing function to go to its details page, and click Edit
- Expand the advanced settings by clicking Environment variables, networking, timeouts and more.
- In the Networking section, under Egress settings, select a Serverless VPC Access connector.
- Select the appropriate egress setting based on how you want to route outbound traffic through the connector.
Via gcloud:
gcloud functions deploy FUNCTION_NAME \
--vpc-connector CONNECTOR_NAME \
--egress-settings EGRESS_SETTINGS \
FLAGS...
where:
FUNCTION_NAME is the name of your function.
CONNECTOR_NAME is the name of the Serverless VPC Access connector to use. See the gcloud documentation for more information.
Note: You can omit the --vpc-connector flag if you are updating egress
settings on an existing function that already has a connector.
EGRESS_SETTINGS is one of the supported values for egress settings: see gcloud documentation.
FLAGS... refers to other flags you pass to the deploy command.
Select the appropriate egress setting based on how you want to route outbound traffic through the connector.
After this, you only need to
- Set up Cloud NAT and
- Specify a static IP address for NAT.
Create a Cloud NAT:
gcloud compute routers nats create nat-config \
--router=nat-router \
--auto-allocate-nat-external-ips \
--nat-all-subnet-ip-ranges \
--enable-logging
Specify IP addresses:
gcloud compute routers nats create nat-config \
--router=nat-router \
--nat-external-ip-pool=ip-address1,ip-address2