2
votes

I have observed a surprising charge for network egress from Google Cloud Functions, and I'm trying to understand why it's happening and how to avoid it.

Stackdriver monitoring suggests that the problematic function is my ingest function, which reads files from a GCS bucket, processes them and inserts rows into a Cloud SQL instance. See the correlation here between network egress from the cloud function and received bytes for the SQL instance.

bytes in/out of cloud function and cloud sql

The function and the cloud SQL instance are both in us-central1 (though different zones within that region). The function connects to the SQL instance via the provided unix socket. The SQL instance does have a public IP address.

Based on the charges so far, I appear to be paying $0.1 per GiB, which looks like general Internet egress prices.

My only theory right now is that because of the public IP I am being charged public internet egress prices for the traffic to the SQL instance. Is this plausible? Is there some other reason for the charge? If this is true I assume the charge will go away if I disable the public IP for the SQL instance?

1

1 Answers

3
votes

I was able to solve this problem by adding a private IP address for the SQL instance, a Serverless VPC Connector, and changing database connection code to use the private IP address explicitly.

Note that if the instance has both a public and private IP address, then connecting via the unix socket at /cloudsql/<instance_connection_name> will still incur egress charges. If the instance only has a private IP, the unix socket method uses the private network and does not incur charges. Therefore in order to retain a public IP and not incur egress charges from cloud functions, you'll need to use the private IP instead of the unix socket.