'Error While creating CNAME at Cloudflare through Terraform

What I did?

  • Created a terraform module with provider as cloudflare
provider "cloudflare" {
}
  • Provided token to the shell environment using variable CLOUDFLARE_API_TOKEN

  • Token have access to the zone say: example.com

  • Creating a CNAME record which is targeting to my S3 bucket using:

resource "cloudflare_record" "cname-bucket" {
  zone_id = var.domain
  name    = var.bucket_name
  value   = "${var.bucket_name}.s3-website.${var.region}.amazonaws.com"
  proxied = true
  type    = "CNAME"
}
  • After applying this module, getting error:
Error: failed to create DNS record: error from makeRequest: HTTP status 400: content "{\"success\":false,\"errors\":[{\"code\":7003,\"message\":\"Could not route to \\/zones\\/example.com\\/dns_records, perhaps your object identifier is invalid?\"},{\"code\":7000,\"message\":\"No route for that URI\"}],\"messages\":[],\"result\":null}"
  • When I tried creating the same using cloudflare with browser, everything works fine but when trying same with terraform, getting the above error.

  • Access my token have: example.com - DNS:Edit

What I need?

  • What I am doing wrong here?
  • How to create this CNAME record using terraform module?


Solution 1:[1]

It looks like the problem is zone_id = var.domain line in your cloudflare_record resource. You are using example.com as the zone_id , but instead you should be using your Cloudflare Zone ID.

You can find you Zone ID in your Cloudflare Dashboard for your domain: check in Overview , on the right column in the API section.

Solution 2:[2]

locals {
  domain = "example.com"
  hostname = "example.com" # TODO: Varies by environment
}

variable "CLOUDFLARE_ACCOUNT_ID" {}
variable "CLOUDFLARE_API_TOKEN" { sensitive = true }

provider "cloudflare" {
  api_token = var.CLOUDFLARE_API_TOKEN
  account_id = var.CLOUDFLARE_ACCOUNT_ID
}

resource "cloudflare_zone" "default" {
  zone = local.domain
  plan = "free"
}

resource "cloudflare_record" "a" {
  zone_id = cloudflare_zone.default.id
  name    = local.hostname
  value   = "192.0.2.1"
  type    = "A"
  ttl     = 1
  proxied = true
}

Source https://github.com/kriasoft/terraform-starter-kit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paolo Tagliaferri
Solution 2 Konstantin Tarkus