Cloudflare Pages + Terraformでブログ環境を構築する
はじめに
以前の投稿ではCloudflare Pagesをはじめて触ったときに画面上からクリックしてブログを作成しました。
しかし、ドメイン設定、プロジェクト作成など手順が思いのほか多いです。個人ブログだけなら1つで問題ないのですが、複数サイトの運用を考える場合、単純で退屈な作業です。
今回はTerraformを使って一連の作業を、できるかぎりコードかしてみようと思います。
providerは公式のcloudflare/terraform-provider-cloudflareです。
対象
- とりあえずCloudflare Pages触ってみた人
- Cloudflare PagesをTerraformで管理したい人
Cloudflareのproviderについて
3系
各リソースに定義する形になるそうです。
4系
Provider configuration for account_id is removed
Terraformを実行するためのAPIトークンを作成する
まずはTerraforを実行するためにトークンを発行します。
アカウント | Cloudflare Pages | 編集 | Pagesのプロジェクトを作成するのに必要です |
ユーザー | APIトークン | 編集 | GitHub Actions用のトークンを作成する際に必要です |
ゾーン | ゾーン | 編集 | dnsのzoneを作成する際に必要です |
ゾーン | DNS | 編集 | dnsのcname recordを設定するために必要です |
Cloudflare APIでPermission Group一覧を取得する
CloudflareでZoneを作成する
terraformはterraform.tfvars
を変数として自動的に読み取ってくれるので、これを利用します。
terraform.tfvars
ファイルにシークレット(APIトークン)を定義します。
Zoneを作成するためにcloudflare_zone (Resource) - Terraform registoryを利用します。
コードの準備ができました!さっそくterraform applyしてみましょう!
terraform plan
terraform apply —auto-approve
ゾーンができていることを確認しました!
ネームサーバを登録する
先ほどのterraform applyの出力結果からネームサーバの情報を取得できました。
今回はドメインをGoogle Doaminsで管理しているので、そちらに登録しにいきます。
自分が管理しているドメイン(Google Domains)に移動し、先ほど確認したCloudflareのネームサーバをコピペ & 保存します。
これらの設定に切り替える
ボタンを押したあとこちらのDNS設定はアクティブです
の文言が出ます。
時間が経つとCloudflare側でzoneの設定が完了したことを確認できます。
参考
プロジェクトを作成する
今度はプロジェクトを作成してみます。
先ほどのTerarformのコードからcloudflare_pages_project (Resource) - Terraform registoryを追加します。
terraformコマンドを実行して実作にapplyしてみましょう
terraform plan
terraform apploy —auto-approve
Cloudflare Pagesの画面を見に行くと・・・
いい感じにできていそうです!
カスタムドメインを設定する
カスタムドメインを設定するためにcloudflare_pages_domain (Resource) - Terraform registoryを追加します。
以下のハイライト箇所です。
terraform plan
terraform apply —auto-approve
カスタムドメインの設定はできましたが、CNAMEを登録していないため、検証中
になっています。
次はレコードをDNSに登録します。
DNSにCNAMEを登録する
CNAMEを登録するためにcloudflare_record (Resource) - Terraform registoryを利用します。
以下のハイライト箇所です。
terraform plan
terraform apply —auto-approve
Webサイト
から自分が作成したサイトをクリックすると、DNSを確認できます。
意図した通りにCNAMEが登録されていますね。
Pagesに戻ってカスタムドメインのステータスを確認してみましょう。無事アクティブになっています。問題なさそうですね。
GitHub Actionsでデプロイする用にAPIトークンを発行する
Terraformを実行するためのAPIトークンを作成するで作成したAPIトークンは権限が強いため、新規にGitHub Actions用のトークンを生成します。
そのために以下2つのリソースを使います。
- cloudflare_api_token_permission_groups (Data Source) - Terraform registory
- cloudflare_api_token (Resource) - Terraform registory
以下のハイライトの箇所を追記して、terraform planを実行します
info: Q: data.cloudflare_api_token_permission_groups.all.account[“Pages Write”]とかのキーとなる文字列は何が入るかわからないんだけど・・
A: API token permissions - Cloudflare Docsからでも確認できますが、バージョンによっては微妙にずれているかも知れないので、data resourceを直接outputとしてstdoutに出した方がすぐにわかります。
terraform plan
terraform apply —auto-approve
applyが完了しました!さっそく自分のプロフィールから確認してみましょう
問題なくgh-actions-token
が作成されています。
トークンを取り出す
terraformのstateファイルにトークンの値があるので、それを取り出します。
このトークンをGitHub Actionsのシークレットにセットすれば自動デプロイができます。
GitHub Actionsの設定はGitHub Actionsを使ってCloudflare Pagesにブログを自動デプロイするに書きましたので、よかったら参考にしてください。
参考
Cloudflare Provider
- cloudflare/terraform-provider-cloudflare
- Provider configuration for account_id is removed
- cloudflare_zone (Resource) - Terraform registory
- cloudflare_pages_project (Resource) - Terraform registory
- cloudflare_pages_domain (Resource) - Terraform registory
- cloudflare_record (Resource) - Terraform registory
- cloudflare_api_token_permission_groups (Data Source) - Terraform registory
- cloudflare_api_token (Resource) - Terraform registory
Terraform Language Documentation
その他
おまけ
permission一覧のスコープ
scope: “com.cloudflare.api.account”一覧