跳转到内容
搜索文档

1 – 初始化 Terraform

最后更新 查看 MarkdownAgent 设置

本教程演示了如何开始使用 Terraform。您刚刚在 Cloudflare 上注册了域名(example.com)以便在 Terraform 中管理所有内容,现在您将创建一条 DNS 记录,将 www.example.com 指向 203.0.113.10 的 Web 服务器。

在开始之前,确保您已经:

1. 创建您的配置

创建一个名为 main.tf 的文件,填入您自己的 API 令牌区域 ID (zone ID)账户 ID (account ID)域名的值:

terraform {
  required_providers {
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 5"
    }
  }
}

provider "cloudflare" {
  api_token = "<YOUR_API_TOKEN>"
}

variable "zone_id" {
  default = "<YOUR_ZONE_ID>"
}

variable "account_id" {
  default = "<YOUR_ACCOUNT_ID>"
}

variable "domain" {
  default = "<YOUR_DOMAIN>"
}

resource "cloudflare_dns_record" "www" {
  zone_id = "<YOUR_ZONE_ID>"
  name    = "www"
  content = "203.0.113.10"
  type    = "A"
  ttl     = 1
  proxied = true
  comment = "Domain verification record"
}

2. 初始化和计划

初始化 Terraform 以下载 Cloudflare 提供程序:

terraform init

查看将要创建的内容:

terraform plan

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_dns_record.www will be created
  + resource "cloudflare_dns_record" "www" {
      + comment             = "Domain verification record"
      + comment_modified_on = (known after apply)
      + content             = "203.0.113.10"
      + created_on          = (known after apply)
      + id                  = (known after apply)
      + meta                = (known after apply)
      + modified_on         = (known after apply)
      + name                = "www"
      + proxiable           = (known after apply)
      + proxied             = true
      + settings            = (known after apply)
      + tags                = (known after apply)
      + tags_modified_on    = (known after apply)
      + ttl                 = 1
      + type                = "A"
      + zone_id             = "<YOUR_ZONE_ID>"
    }

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

3. 应用并验证

应用您的配置:

terraform apply

在系统提示时输入 yes

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_dns_record.www will be created
  + resource "cloudflare_dns_record" "www" {
      + comment             = "Domain verification record"
      + comment_modified_on = (known after apply)
      + content             = "203.0.113.10"
      + created_on          = (known after apply)
      + id                  = (known after apply)
      + meta                = (known after apply)
      + modified_on         = (known after apply)
      + name                = "www"
      + proxiable           = (known after apply)
      + proxied             = true
      + settings            = (known after apply)
      + tags                = (known after apply)
      + tags_modified_on    = (known after apply)
      + ttl                 = 1
      + type                = "A"
      + zone_id             = "<YOUR_ZONE_ID>"
    }

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

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudflare_dns_record.www: Creating...
cloudflare_dns_record.www: Creation complete after 0s

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

创建后,验证 DNS 记录:

dig www.example.com

测试 Web 服务器响应:

curl https://www.example.com
Hello, this is 203.0.113.10!

要查看 API 调用返回的完整结果:

terraform show

您还可以检查 Cloudflare 仪表板并转到 DNS > Records(记录) 页面。

Go to Account home ↗

这篇文档对您有帮助吗?