コンテンツにスキップ

6 – 設定の元に戻す

時には、設定変更を元に戻す必要があるかもしれません。たとえば、新しい設定でパフォーマンステストを実行したい場合や、IPアドレスを誤って入力してサイト全体をダウンさせてしまった場合などです。

設定を元に戻すには、目的のブランチをチェックアウトし、TerraformにCloudflareの設定を過去に戻すように指示します。もしサイトを誤ってダウンさせてしまった場合は、チュートリアルでの簡潔さのために直接masterにマージするのではなく、プルリクエストのピアレビューのための良い戦略を確立することを検討してください。

1. 設定履歴の確認

どのくらい戻すかを決定する前に、バージョン履歴を確認します:

Terminal window
git log
commit d4fec164581bec44684a4d59bb80aec1f1da5a6e
Author: Me
Date: Wed Apr 18 22:04:52 2018 -0700
ステップ 5 - 2つのページルールを追加。
commit bc9aa9a465a4c8d6deeaa0491814c9f364e9aa8a
Author: Me
Date: Sun Apr 15 23:58:35 2018 -0700
ステップ 4 - ロードバランサー (LB) モニター、LBプール、およびLBを作成。
commit 6761a4f754e77322629ba4e90a90a3defa1fd4b6
Author: Me
Date: Wed Apr 11 11:20:25 2018 -0700
ステップ 4 - アジアデータセンター用の追加の 'www' DNSレコードを追加。
commit d540600b942cbd89d03db52211698d331f7bd6d7
Author: Me
Date: Sun Apr 8 22:21:27 2018 -0700
ステップ 3 - TLS 1.3、常にHTTPSを使用、およびSSL厳格モードを有効にする。
commit 494c6d61b918fce337ca4c0725c9bbc01e00f0b7
Author: Me
Date: Sun Apr 8 19:58:56 2018 -0700
ステップ 2 - terraformプラグインディレクトリと状態ファイルを無視する。
commit 5acea176050463418f6ac1029674c152e3056bc6
Author: Me
Date: Sun Apr 8 19:52:13 2018 -0700
ステップ 1 - ウェブサーバー定義を含む初期コミット。

Cloudflareの設定をGitで保存するもう一つの利点は、誰が変更を行ったかを確認できることです。また、プルリクエストをピアレビューすることで、誰が変更をレビューし承認したかも確認できます。

2. 特定の履歴変更の調査

最後の変更がいつ行われたかを確認します:

Terminal window
git show
commit d4fec164581bec44684a4d59bb80aec1f1da5a6e
Author: Me
Date: Wed Apr 18 22:04:52 2018 -0700
ステップ 5 - 2つのページルールを追加。
diff --git a/cloudflare.tf b/cloudflare.tf
index 0b39450..ef11d8a 100644
--- a/cloudflare.tf
+++ b/cloudflare.tf
@@ -94,3 +94,26 @@ resource "cloudflare_load_balancer" "www-lb" {
description = "example load balancer"
proxied = true
}
+
+resource "cloudflare_page_rule" "increase-security-on-expensive-page" {
+ zone_id = var.zone_id
+ target = "www.${var.domain}/expensive-db-call"
+ priority = 1
+
+ actions {
+ security_level = "under_attack",
+ }
+}
+
+resource "cloudflare_page_rule" "redirect-to-new-db-page" {
+ zone_id = var.zone_id
+ target = "www.${var.domain}/old-location.php"
+ priority = 2
+
+ actions {
+ forwarding_url {
+ url = "https://${var.domain}/expensive-db-call"
+ status_code = 301
+ }
+ }
+}

過去の数回の変更を確認します:

Terminal window
git log -p -3
...
// 上記のページルール設定
...
commit bc9aa9a465a4c8d6deeaa0491814c9f364e9aa8a
Author: Me
Date: Sun Apr 15 23:58:35 2018 -0700
ステップ 4 - ロードバランサー (LB) モニター、LBプール、およびLBを作成。
diff --git a/cloudflare.tf b/cloudflare.tf
index b92cb6f..195b646 100644
--- a/cloudflare.tf
+++ b/cloudflare.tf
@@ -59,3 +59,38 @@ resource "cloudflare_record" "www-asia" {
type = "A"
proxied = true
}
+
+resource "cloudflare_load_balancer_monitor" "get-root-https" {
+ account_id = var.account_id
+ expected_body = "alive"
+ expected_codes = "200"
+ method = "GET"
+ timeout = 5
+ path = "/"
+ interval = 60
+ retries = 2
+ description = "GET / over HTTPS - expect 200"
+}
+
+resource "cloudflare_load_balancer_pool" "www-servers" {
+ account_id = var.account_id
+ name = "www-servers"
+ monitor = cloudflare_load_balancer_monitor.get-root-https.id
+ origins {
+ name = "www-us"
+ address = "203.0.113.10"
+ }
+ origins {
+ name = "www-asia"
+ address = "198.51.100.15"
+ }
+ description = "www origins"
+ enabled = true
+ minimum_origins = 1
+ notification_email = "you@example.com"
+ check_regions = ["WNAM", "ENAM", "WEU", "EEU", "SEAS", "NEAS"]
+}
+resource "cloudflare_load_balancer" "www-lb" {
+ zone_id = var.zone_id
+ name = "www-lb"
+ default_pool_ids = [cloudflare_load_balancer_pool.www-servers.id]
+ fallback_pool_id = cloudflare_load_balancer_pool.www-servers.id
+ description = "example load balancer"
+ proxied = true
+}
commit 6761a4f754e77322629ba4e90a90a3defa1fd4b6
Author: Me
Date: Wed Apr 11 11:20:25 2018 -0700
ステップ 4 - アジアデータセンター用の追加の 'www' DNSレコードを追加。
diff --git a/cloudflare.tf b/cloudflare.tf
index 9f25a0c..b92cb6f 100644
--- a/cloudflare.tf
+++ b/cloudflare.tf
@@ -52,3 +52,10 @@ resource "cloudflare_zone_settings_override" "example-com-settings" {
ssl = "strict"
}
}
+
+resource "cloudflare_record" "www-asia" {
+ zone_id = var.zone_id
+ name = "www"
+ value = "198.51.100.15"
+ type = "A"
+ proxied = true
+}

3. 前の設定を再デプロイ

ページルールで例外を追加するチュートリアルに従ってページルールをデプロイした直後に、そのURLがもはや必要ないと言われ、セキュリティ設定とリダイレクトを削除する必要があると仮定します。

設定ファイルを直接編集してそのエントリを削除することもできますが、Gitを使用してそれを行うこともできます。

i. ブランチを前のコミットに戻す

次のGitコマンドを実行して、履歴を変更せずに最後のコミットを元に戻します:

Terminal window
git revert HEAD~1..HEAD
[master f9a6f7d] "ステップ 6 - バグ修正。"を元に戻す。
1ファイルが変更され、1挿入(+)、1削除(-)
Terminal window
git log -2
commit f9a6f7db72ea1437e146050a5e7556052ecc9a1a
Author: Me
Date: Wed Apr 18 23:28:09 2018 -0700
"ステップ 5 - 2つのページルールを追加。"を元に戻す。
このコミットはd4fec164581bec44684a4d59bb80aec1f1da5a6eを元に戻します。
commit d4fec164581bec44684a4d59bb80aec1f1da5a6e
Author: Me
Date: Wed Apr 18 22:04:52 2018 -0700
ステップ 5 - 2つのページルールを追加。

ii. 変更のプレビュー

terraform planを実行して、実行計画を確認します:

Terminal window
terraform plan
計画の前にメモリ内のTerraform状態を更新しています...
更新された状態はこの計画の計算に使用されますが、ローカルまたはリモートの状態ストレージには保存されません。
cloudflare_page_rule.increase-security-on-expensive-page: 状態を更新中... [id=1c13fdb84710c4cc8b11daf7ffcca449]
cloudflare_page_rule.redirect-to-new-db-page: 状態を更新中... [id=c5c40ff2dc12416b5fe4d0541980c591]
cloudflare_zone_settings_override.example-com-settings: 状態を更新中... [id=e2e6491340be87a3726f91fc4148b126]
cloudflare_record.www: 状態を更新中... [id=c38d3103767284e7cd14d5dad3ab8669]
cloudflare_load_balancer_monitor.get-root-https: 状態を更新中... [id=4238142473fcd48e89ef1964be72e3e0]
cloudflare_record.www-asia: 状態を更新中... [id=fda39d8c9bf909132e82a36bab992864]
cloudflare_load_balancer_pool.www-servers: 状態を更新中... [id=906d2a7521634783f4a96c062eeecc6d]
cloudflare_load_balancer.www-lb: 状態を更新中... [id=cb94f53f150e5c1a65a07e43c5d4cac4]
------------------------------------------------------------------------
実行計画が生成され、以下に示されています。
リソースアクションは次の記号で示されています:
- 削除
Terraformは次のアクションを実行します:
- cloudflare_page_rule.increase-security-on-expensive-page
- cloudflare_page_rule.redirect-to-new-db-page
計画:追加0、変更0、削除2。
------------------------------------------------------------------------
注意:"-out"パラメータを指定しなかったため、Terraformは次に"terraform apply"を実行した場合に正確にこれらのアクションが実行されることを保証できません。

予想通り、Terraformは前のステップで作成された2つのページルールを削除することを示しています。

iii. 変更を適用

変更は良さそうです。Terraformは変更を適用するとCloudflareの設定を元に戻します:

Terminal window
terraform apply --auto-approve
cloudflare_page_rule.redirect-to-new-db-page: 状態を更新中... [id=c5c40ff2dc12416b5fe4d0541980c591]
cloudflare_page_rule.increase-security-on-expensive-page: 状態を更新中... [id=1c13fdb84710c4cc8b11daf7ffcca449]
cloudflare_zone_settings_override.example-com-settings: 状態を更新中... [id=e2e6491340be87a3726f91fc4148b126]
cloudflare_load_balancer_monitor.get-root-https: 状態を更新中... [id=4238142473fcd48e89ef1964be72e3e0]
cloudflare_record.www: 状態を更新中... [id=c38d3103767284e7cd14d5dad3ab8669]
cloudflare_record.www-asia: 状態を更新中... [id=fda39d8c9bf909132e82a36bab992864]
cloudflare_load_balancer_pool.www-servers: 状態を更新中... [id=906d2a7521634783f4a96c062eeecc6d]
cloudflare_load_balancer.www-lb: 状態を更新中... [id=cb94f53f150e5c1a65a07e43c5d4cac4]
cloudflare_page_rule.redirect-to-new-db-page: 削除中... [id=c5c40ff2dc12416b5fe4d0541980c591]
cloudflare_page_rule.increase-security-on-expensive-page: 削除中... [id=1c13fdb84710c4cc8b11daf7ffcca449]
cloudflare_page_rule.increase-security-on-expensive-page: 削除完了 0秒後
cloudflare_page_rule.redirect-to-new-db-page: 削除完了 1秒後
適用完了!リソース:追加0、変更0、削除2。

2つのリソースが削除され、予想通り、前のバージョンに戻りました。