このチュートリアルでは、Terraform の始め方を説明します。ドメイン(example.com)を Cloudflare に登録し、すべてを Terraform で管理する想定です。ここでは、www.example.com を Web サーバー 203.0.113.10 に向ける DNS レコードを作成します。
始める前に、次を用意してください。
- Terraform をインストール済み
- このチュートリアルでリソースを編集できる権限を持つ API トークンを作成済み
main.tf というファイルを作成し、API トークン、ゾーン ID、アカウント 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"
}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.設定を適用します。
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.comWeb サーバーの応答を確認します。
curl https://www.example.comHello, this is 203.0.113.10!API 呼び出しの結果をすべて確認するには、次を実行します。
terraform showCloudflare ダッシュボードの DNS > Records ページでも確認できます。
Account home を開く ↗