Publish applications with Terraform
This guide covers how to use the Cloudflare Terraform provider to quickly publish and secure a private application. In the following example, we will add a new public hostname route to an existing Cloudflare Tunnel, configure how cloudflared
proxies traffic to the application, and secure the application with Cloudflare Access.
- Add your domain to Cloudflare
- Configure an IdP integration
- Create a Cloudflare Tunnel via the Zero Trust dashboard
- Install the Terraform client
- Create an API token (refer to the minimum required permissions)
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:
Create a .tf
file and copy-paste the following example. Fill in your API token, account and zone information, and Tunnel ID.
Find the Tunnel ID
- In Zero Trust, go to Networks > Tunnels.
- Select the tunnel name.
- Copy the Tunnel ID.
Add the following resources to your Terraform configuration.
Using the cloudflare_tunnel_config
resource, create an ingress rule that maps your application to a public DNS record. This example makes localhost:8080
available on app.mycompany.com
, sets the Connect Timeout, and enables Access JWT validation.
Using the cloudflare_access_application
resource, add the application to Cloudflare Access.
Using the cloudflare_access_policy
resource, create a policy to secure the application. The following policy will only allow access to users who authenticate through your identity provider.
To deploy the configuration files:
-
Initialize your configuration directory:
-
Preview everything that will be created:
-
Apply the configuration:
Users can now access the private application by going to the public URL and authenticating with Cloudflare Access. You can view your new tunnel route, Access application, and Access policy in Zero Trust. The new DNS record is shown in the Cloudflare dashboard.