This example deploys a Cloud Run application, creates a Serverless Network Endpoint Group (NEG) and exposes it behind a Cloud HTTPS load balancer with HTTP-to-HTTPS redirection.
You can tweak this example to enable other functionalities such as:
- serving static assets from Cloud CDN
- enabling a security profile via Cloud Armor
- run global endpoints by deploying Cloud Run service to multiple regions.
[[ `basename $PWD` != cloudrun ]] && cd examples/cloudrun
- Install Terraform if it is not already installed (visit terraform.io for other distributions):
- Set the project, replace
YOUR_PROJECT
with your project ID:-
PROJECT=YOUR_PROJECT
gcloud config set project ${PROJECT}
- Configure the environment for Terraform:
[[ $CLOUD_SHELL ]] || gcloud auth application-default login
export GOOGLE_PROJECT=$(gcloud config get-value project)
This option provisions an HTTP forwarding rule (insecure) and is not recommended for production use. It is provided since it provisions faster than the Option 2.
-
Initialize:
terraform init
-
Deploy the load balancer, replace
example.com
with your domain name.terraform apply -var=project_id=$PROJECT \ -var=ssl=false -var=domain=null
-
It may take some time for the load balancer to provision. Visit the output IP address of the load balancer.
This options creates a Google-managed SSL certificate for your domain name, sets it up on HTTPS forwarding rule and creates a HTTP forwarding rule to redirect HTTP traffic to HTTPS.
-
Make sure you have a domain name. This is required since we provision a Google-managed SSL certificate specifically for this domain name.
-
Initialize:
terraform init
-
Deploy the load balancer, replace
example.com
with your domain name.terraform apply -var=project_id=$PROJECT \ -var=domain=example.com
-
After the deployment completes it outputs the IP address of the load balancer. Update DNS records for your domain to point to this IP address.
-
It may take around half an hour for the SSL certificate to be provisioned and the application to start serving traffic.
Name | Description | Type | Default | Required |
---|---|---|---|---|
domain | Domain name to run the load balancer on. Used if ssl is true . |
string |
n/a | yes |
lb-name | Name for load balancer and associated resources | string |
"run-lb" |
no |
project_id | n/a | string |
n/a | yes |
region | Location for load balancer and Cloud Run resources | string |
"us-central1" |
no |
ssl | Run load balancer on HTTPS and provision managed certificate with provided domain . |
bool |
true |
no |
Name | Description |
---|---|
load-balancer-ip | n/a |