Search code examples
google-cloud-platformgoogle-cloud-functionsgoogle-cloud-networkinggoogle-cloud-network-load-balancer

Is it possible to route Google Cloud Functions egress traffic through multiple rotating IPs?


My app uses a Cloud Function (2nd gen), running multiple instances and triggered by PubSub, to make outbound requests to customers sites (essentially for synthetic monitoring).

However, the platform several of those sites are hosted on has bot protection, and I've been told by the company they don't have a way of whitelisting IPs for it, so it would be best if I rotate through IPs (ie a Function selects/is assigned an IP when created).

Cloud NAT seems to be the recommendation for setting up static external IPs, but adding multiple IPs there doesn't cycle through them until they reach TCP connection limits.

Load balancers might have a way to do it, but from what I've found, egress is still routed through Cloud NAT, and uses the IP assigned there.

Is there another way to make requests from Cloud Functions from varying IPs?


Solution

  • Now that specific Cloud Function using that specific VPC Connector will route its outbound traffic through that specific Cloud NAT Gateway.

    You can repeat this process as many times as necessary. To make this work with your Cloud Function you will have to deploy them as multiple Cloud Functions rather than a single Cloud Function. For example, if you presently have a function named myCloudFunction then you would need to deploy it as three separate and distinct Cloud Functions, each using a different configuration:

    • myCloudFunction1 - uses Serverless VPC Connector 1 and Cloud NAT Gateway 1
    • myCloudFunction2 - uses connector 2 and gateway 2
    • myCloudFunction3 - uses connector 3 and gateway 3

    You would then need to find a way to load balance the requests across those three functions, for example with another Cloud Function whose job it is to send the request through those functions. My recommendation would be something like:

    • myCloudFunction - your original function but instead of connecting to your ultimate destination you round-robin connect to one of the three Cloud Functions as a proxy server
    • myProxyFunction1 - a simple HTTP proxy that just forwards the request along, same config as myCloudFunction1 above
    • myProxyFunction2 - same config as myCloudFunction2
    • myProxyFunction3 - same config as myCloudFunction3