This is the Terraform equivalent of the Serving traffic from multiple regions tutorial I published on Cloud Run documentation.
This example uses Terraform serverless_negs module to deploy a Cloud Run service to all locations and then create a global Cloud HTTP Load Balancer with an anycast IP address to route your users to the nearest Cloud Run location.
-
Initialize terraform modules:
terraform init
-
See the execution plan (replace
PROJECT_ID
with yours)terraform plan -var=name=zoneprinter -var=project_id=PROJECT_ID
-
Apply the resources. (replace
PROJECT_ID
with yours)terraform apply -var=name=zoneprinter -var=project_id=PROJECT_ID
-
After deploying, it will print the load balancer’s IP address.
url = http://34.107.196.62
After waiting several minutes, the load balancer configuration will propagate globally and start working.
-
Users around the world will be connected to the nearest Cloud Run location that has the application deployed when they hit the load balancer.
For example, a visitor from USA may see:
Welcome from Google Cloud datacenters at: The Dalles, Oregon, USA ("us-west1").
And a visitor from Thailand might see:
Welcome from Google Cloud datacenters at: Jurong West, Singapore ("asia-southeast1").
-
Clean up after you’re done:
terraform destroy -var=name=zoneprinter -var=project_id=PROJECT_ID
This is not an official project and does not come with maintenance guarantees.