Terraform (2)·续

Configures tunnel with a published application for clientless access.

resource "cloudflare_zero_trust_tunnel_cloudflared_config" "gcp_tunnel_config" { tunnel_id = cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id account_id = var.cloudflare_account_id config { ingress_rule { hostname = "${cloudflare_record.http_app.hostname}" service = "http://httpbin:80" } ingress_rule { service = "http_status:404" } } }

(Optional) Route internal IP of GCP instance through the tunnel for private network access using WARP.

resource "cloudflare_zero_trust_tunnel_route" "example_tunnel_route" { account_id = var.cloudflare_account_id tunnel_id = cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.id network = google_compute_instance.http_server.network_interface.0.network_ip comment = "Example tunnel route" }

Creates an Access application to control who can connect to the public hostname.

resource "cloudflare_zero_trust_access_application" "http_app" { account_id = var.cloudflare_account_id name = "Access application for http_app.${var.cloudflare_zone}" domain = "http_app.${var.cloudflare_zone}" }

Creates a (legacy) Access policy for the Access application.

resource "cloudflare_zero_trust_access_policy" "allow_emails" { application_id = cloudflare_zero_trust_access_application.http_app.id account_id = var.cloudflare_account_id name = "Example policy for http_app.${var.cloudflare_zone}" precedence = "1" decision = "allow" include { email = [var.cloudflare_email] } } ```

To learn more about these resources, refer to the Cloudflare provider documentation ↗︎.

Configure GCP resources

The following configuration defines the specifications for the GCP virtual machine and configures a startup script to run upon boot.

  1. In your configuration directory, create a .tf file:
   touch GCP-config.tf
   ```

2. Add the following content to `GCP-config.tf`:

```tf
   # OS the server will use
   data "google_compute_image" "image" {
    family  = "ubuntu-2204-lts"
    project = "ubuntu-os-cloud"
   }

# GCP Instance resource
   resource "google_compute_instance" "http_server" {
    name         = "test"
    machine_type = var.machine_type
    zone         = var.zone
    tags         = []
    boot_disk {
        initialize_params {
            image = data.google_compute_image.image.self_link
        }
    }

network_interface {
        network = "default"
        access_config {
            //Ephemeral IP
        }
    }
    // Optional config to make instance ephemeral
   /*  scheduling {
        preemptible       = true
        automatic_restart = false
    } */

// Pass the tunnel token to the GCP server so that the server can install and run the tunnel upon startup.
    metadata_startup_script = templatefile("./install-tunnel.tftpl",
        {
            tunnel_token = data.cloudflare_zero_trust_tunnel_cloudflared_token.gcp_tunnel_token.token
        })
   }
   ```

```tf
   # OS the server will use
   data "google_compute_image" "image" {
    family  = "ubuntu-2204-lts"
    project = "ubuntu-os-cloud"
   }

# GCP Instance resource
   resource "google_compute_instance" "http_server" {
    name         = "test"
    machine_type = var.machine_type
    zone         = var.zone
    tags         = []
    boot_disk {
        initialize_params {
            image = data.google_compute_image.image.self_link
        }
    }

network_interface {
        network = "default"
        access_config {
            //Ephemeral IP
        }
    }
    // Optional config to make instance ephemeral
   /*  scheduling {
        preemptible       = true
        automatic_restart = false
    } */

// Pass the tunnel token to the GCP server so that the server can install and run the tunnel upon startup.
    metadata_startup_script = templatefile("./install-tunnel.tftpl",
        {
            tunnel_token = cloudflare_zero_trust_tunnel_cloudflared.gcp_tunnel.tunnel_token
        })
   }
   ```

### Create a startup script

The following script will install `cloudflared` and run the tunnel as a service. This example also installs a lightweight HTTP application that you can use to test connectivity.

1. In your configuration directory, create a Terraform template file:

```sh
   touch install-tunnel.tftpl
   ```

2. Open the file in a text editor and copy and paste the following bash script:

```bash
   # Script to install Cloudflare Tunnel and Docker resources

# Docker configuration
   cd /tmp
   sudo apt-get install software-properties-common
   # Retrieving the docker repository for this OS
   curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
   sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
   # The OS is updated and docker is installed
   sudo apt update -y && sudo apt upgrade -y
   sudo apt install docker docker-compose -y
   # Add the HTTPBin application and run it on localhost:8080.
   cat > /tmp/docker-compose.yml << "EOF"
   version: '3'
   services:
     httpbin:
       image: kennethreitz/httpbin
       restart: always
       container_name: httpbin
       ports:
         - 8080:80

cloudflared:
       image: cloudflare/cloudflared:latest
       restart: always
       container_name: cloudflared
       command: tunnel run --token ${tunnel_token}
   EOF
   cd /tmp
   sudo docker-compose up -d
   ```

## 6. Deploy Terraform

To deploy the configuration files:

1. Initialize your configuration directory:

```sh
   terraform init
   ```

2. Preview everything that will be created:

```sh
   terraform plan
   ```

3. Apply the configuration:

```sh
   terraform apply
   ```

It may take several minutes for the GCP instance and tunnel to come online. You can view your new tunnel in the [Cloudflare dashboard ↗︎](https://dash.cloudflare.com/) under **Networking** > **Tunnels**.

Remove Terraform resources

If you need to roll back the configuration, run `terraform destroy` to delete everything created through Terraform. Both `terraform apply` and `terraform destroy` prompt for user input before applying the changes. To run without requiring user input, you can add the `-auto-approve` flag to the command.

## 7. Test the connection

1. In the [Cloudflare dashboard ↗︎](https://dash.cloudflare.com/), go to **Networking** > **Tunnels** and verify that your tunnel is **Active**.
2. (Optional) If you configured Access, go to **Security** > **Access** > **Applications** and verify that your Cloudflare email is allowed by the Access policy.
3. From any device, open a browser and go to `http_app.<CLOUDFLARE_ZONE>` (for example, `http_app.example.com`).

If you configured Access, you will see the Access login page. Log in with your Cloudflare email.
4. You should see the HTTPBin homepage, confirming that your tunnel is routing traffic correctly.

Was this helpful?

YesNo

## On this page

[![](data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NiIgaGVpZ2h0PSIzMCIgZmlsbD0ibm9uZSIgdmlld0JveD0iMCAwIDY2IDMwIj48ZyBjbGlwLXBhdGg9InVybCgjY2xpcDBfNzQ2XzIpIj48cGF0aCBmaWxsPSIjZmY5OTEwIiBkPSJNNTMuMTIgMTMuMTI1YzYuOTk3IDAgMTIuNjY4IDUuNzA4IDEyLjY2OCAxMi43NSAwIDEuMjU1LS4xOCAyLjQ2OC0uNTE2IDMuNjE0YS43Mi43MiAwIDAgMS0uNjk0LjUxMUg0Ni45NTNhLjM3NS4zNzUgMCAwIDEtLjM0OC0uNTFsLjMzNy0uODgyYzEuMDc2LTIuODMzIDQuMTQ2LTUuMTU0IDYuOTg3LTUuMjlsNS4zODQtLjI3N2EuNTQzLjU0MyAwIDAg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"TechArticle","@id":"https://developers.cloudflare.com/tunnel/guides/terraform/#page","headline":"Terraform","description":"Learn how to deploy a Cloudflare Tunnel using Terraform and our lightweight server-side daemon, cloudflared.","url":"https://developers.cloudflare.com/tunnel/guides/terraform/","inLanguage":"en","image":"https://developers.cloudflare.com/tunnel/guides/terraform/og.png?v=4fad9fd561bcf6e1","dateModified":"2026-09-11","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"},"keywords":["Terraform","Integration"]}


description: Update cloudflared to the latest version. title: Update cloudflared image: https://developers.cloudflare.com/tunnel/guides/update-cloudflared/og.png?v=6bc81ae3c578f131


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/tunnel/llms.txt
Use this file to discover all available pages before exploring further.