Google Cloud Functions do not natively support static IP addresses, as they are designed to be ephemeral and to scale automatically. However, you can work around this limitation using a VPC Connector and a Cloud NAT gateway with a static IP address.
Follow the below steps!
Step 1: Enable the required APIs
- Cloud Functions API
- VPC Connector API
- Cloud NAT API
Step 2: Create a VPC network
- Go to the VPC networks page in the Google Cloud Console: https://console.cloud.google.com/networking/networks/list
- Click “Create VPC network” and configure the network.
Step 3: Reserve a static IP address
- Go to the External IP addresses page in the Google Cloud Console: https://console.cloud.google.com/networking/addresses/list
- Click “Reserve a static address” and configure the address.
Step 4: Create a Cloud NAT gateway
- Go to the Cloud NAT page in the Google Cloud Console: https://console.cloud.google.com/networking/routes/nat/list
- Click “Create NAT gateway” and configure the gateway.
- In the “Cloud Router” section, select “Create a Cloud Router”.
- Select the static IP address you reserved earlier in the “NAT IP addresses” section.
Step 5: Create a Serverless VPC Connector
- Go to the Serverless VPC Access page in the Google Cloud Console: https://console.cloud.google.com/networking/connectors/list
- Click “Create connector” and configure the connector.
- Set the “VPC network” and “Region” to match your Cloud Function’s network and region.
Step 6: Deploy your Cloud Function with the VPC Connector
When deploying the Cloud Function, specify the
--vpc-connector flag and provide the full name of the VPC Connector you created, for example:
gcloud functions deploy FUNCTION_NAME --runtime RUNTIME --trigger-http --allow-unauthenticated --vpc-connector projects/PROJECT_ID/locations/REGION/connectors/CONNECTOR_NAME
Replace FUNCTION_NAME, RUNTIME, PROJECT_ID, REGION, and CONNECTOR_NAME with your specific information.
With this setup, all outgoing traffic from your Cloud Function will go through the VPC Connector and the Cloud NAT gateway using the static IP address you reserved.
Note that this does not apply to incoming traffic; you cannot assign a static IP address to incoming traffic for a Cloud Function.
Amit Doshi is a Cloud Engineer who has experienced more than 5 years in AWS, Azure, and Google Cloud. He is an IT professional responsible for designing, implementing, managing, and maintaining cloud computing infrastructure, applications, and services.