Cannot create tunnel with Terraform

Hi,

I try to create a new tunnel with terraform but my apply throw an authentication error, even with my Global API Key:

terraform {
  required_providers {
    random = {
      source = "hashicorp/random"
    }
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "4.19.0"
    }
  }
}

provider "cloudflare" {
  email   = "<email>"
  api_key = "<global api key>"
}

data "cloudflare_accounts" "account" {
  name = "<email>"
}

resource "random_password" "tunnel_secret" {
  length = 64
}

resource "cloudflare_tunnel" "tunnel" {
  account_id = data.cloudflare_accounts.account.id
  name       = "tunnel"
  secret     = base64sha256(random_password.tunnel_secret.result)
}

Here is my terraform run:

Terraform v1.6.4
on linux_amd64
Initializing plugins and modules...
random_password.tunnel_secret: Refreshing state... [id=none]
data.cloudflare_accounts.account: Refreshing...
data.cloudflare_accounts.account: Refresh complete after 0s [id=<redacted>]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cloudflare_tunnel.tunel will be created
  + resource "cloudflare_tunnel" "tunnel" {
      + account_id   = "<redacted>"
      + cname        = (known after apply)
      + id           = (known after apply)
      + name         = "tunnel"
      + secret       = (sensitive value)
      + tunnel_token = (sensitive value)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudflare_tunnel.tunnel: Creating...
╷
│ Error: failed to create Argo Tunnel: Authentication error (10000)
│
│   with cloudflare_tunnel.tunnel,
│   on tunnel.tf line 26, in resource "cloudflare_tunnel" "tunnel":
│    26: resource "cloudflare_tunnel" "tunnel" {
│
╵
Operation failed: failed running terraform apply (exit 1)

Can you try

resource "cloudflare_tunnel" "tunnel" {
  account_id = data.cloudflare_accounts.account.accounts[0].id
  name       = "tunnel"
  secret     = base64sha256(random_password.tunnel_secret.result)
}

https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/data-sources/accounts returns a list of objects.

You can verify it is using the correct account ID by following

I get an Invalid index error but my cloudflare_accounts is wrong.
I’m able to create a tunnel if I hardcode my account id to the resource.

Thanks for the second pair of eyes!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.