Terraform ↗ is an infrastructure as code software tool that allows you to deploy services from different providers using a standardized configuration syntax. When creating a Terraform configuration file, you define the final state of the configuration rather than the step-by-step procedure. This allows you to easily deploy, modify, and manage your Tunnels alongside your other infrastructure.
In this guide, you will use Terraform to deploy:
A Google Cloud Project (GCP) virtual machine that runs an HTTP test server
A Cloudflare Tunnel that makes the server available over the Internet
A Cloudflare Access policy that defines who can connect to the server
Prerequisites
To complete the following procedure, you will need:
Create an API token so that Terraform can interact with your Cloudflare account. At minimum, your token should include the following permissions:
Permission type
Permission
Access level
Account
Cloudflare Tunnel
Edit
Account
Access: Apps and Policies
Edit
Zone
DNS
Edit
4. Create a configuration directory
Terraform functions through a working directory that contains configuration files. You can store your configuration in multiple files or just one — Terraform will evaluate all of the configuration files in the directory as if they were in a single document.
Create a folder for your Terraform configuration:
Change into the directory:
5. Create Terraform configuration files
Define input variables
The following variables will be passed into your GCP and Cloudflare configuration.
In your configuration directory, create a .tf file:
Open the file in a text editor and copy and paste the following:
Assign values to the variables
In your configuration directory, create a .tfvars file:
Terraform will automatically use these variables if the file is named terraform.tfvars, otherwise the variable file will need to be manually passed in.
Add the following variables to terraform.tfvars. Be sure to modify the example with your own values.
Configure Terraform providers
You will need to declare the providers ↗ used to provision the infrastructure.
In your configuration directory, create a .tf file:
Add the following providers to providers.tf. The random provider is used to generate a tunnel secret.
Configure Cloudflare resources
The following configuration will modify settings in your Cloudflare account.
In your configuration directory, create a .tf file:
Add the following resources to Cloudflare-config.tf:
The following configuration defines the specifications for the GCP virtual machine and creates a startup script to run upon boot.
In your configuration directory, create a .tf file:
Add the following content to GCP-config.tf:
Create a startup script
The following script will install cloudflared, create a permissions and configuration file for the tunnel, and set up the tunnel to run as a service. This example also installs a lightweight HTTP application that you can use to test connectivity.
In your configuration directory, create a Terraform template file:
Open the file in a text editor and copy and paste the following bash script:
6. Deploy Terraform
To deploy the configuration files:
Initialize your configuration directory:
Preview everything that will be created:
Apply the configuration:
It may take several minutes for the GCP instance and tunnel to come online. You can view your new tunnel, Access application, and Access policy in Zero Trust ↗. The new DNS records are available in the Cloudflare dashboard ↗.
7. Test the connection
In Networks > Tunnels, verify that your tunnel is active.
In Access > Applications, verify that your Cloudflare email is allowed by the Access policy.
From any device, open a browser and go to http_app.<cloudflare_zone> (for example, http_app.example.com).
You will see the Access login page if you have not recently logged in.