現在預設會佈建未連結至 App Engine 的 Datastore 模式資料庫。
如果資料庫已連結至 App Engine,您可以取消連結。
有效 App Engine
可用的 App Engine 應用程式是指同一專案中存在應用程式,且該應用程式未停用。該應用程式不一定要有任何用量。 連結的應用程式和資料庫必須位於相同區域。
如果停用 App Engine 應用程式,也會一併停用連結至該應用程式的 Datastore 模式資料庫。
查看 App Engine 連結狀態
您可以使用 REST API 檢查 App Engine 取消連結狀態:
curl --header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header "Content-type: application/json" \
"https://firestore.googleapis.com/v1/projects/PROJECT_ID/databases/(default)"
在回應中查看 appEngineIntegrationMode
的值。如果值為 DISABLED
,表示資料庫未連結至 App Engine 應用程式。
取消資料庫與 App Engine 的連結
您可以取消 Datastore 模式資料庫與 App Engine 應用程式的連結,移除對可用 App Engine 應用程式的要求。取消連結資料庫後,您就能停用 App Engine,而不影響資料庫存取權。取消連結是永久性作業。
取消連結資料庫後,您就無法在 Datastore Admin 頁面使用「停用寫入」功能。如果寫入功能目前已停用,取消連結資料庫會啟用這項功能。
將資料庫與 App Engine 取消連結前,請務必啟用 Firestore API 並更新 IAM 權限。
如要取消連結資料庫,請前往 Datastore 管理員頁面,然後按一下「Unlink database from app」(取消連結應用程式與資料庫)。取消連結作業最多可能需要五分鐘才會生效。
遷移 Terraform App Engine 資源
如果您先前透過 google_app_engine_application
Terraform 資源管理 Datastore 模式資料庫,可以改用 google_firestore_database
Terraform 資源。
如需透過 Terraform 管理 Datastore 模式資料庫的一般操作說明,請參閱自動建立資料庫。
建立 google_firestore_database 資源
在 Terraform 設定檔中,建立新的 google_firestore_database
資源:
datastore.tf
resource "google_firestore_database" "database" { project = "project" name = "(default)" location_id = "location" type = "DATASTORE_MODE" app_engine_integration_mode = "DISABLED" // Optional, but recommended for safety delete_protection_state = "DELETE_PROTECTION_ENABLED" }
如需可用位置清單,請參閱 Datastore 位置。選擇與現有資料庫對應的位置。
匯入現有的 Datastore 模式資料庫
首先,請確認已啟用 Firestore API。
接下來,請將現有的 Datastore 模式資料庫匯入 Terraform 狀態:
terraform import google_firestore_database.database "(default)"
接著執行:
terraform plan
檢查輸出內容,確認匯入作業已順利完成。如果輸出內容顯示任何欄位發生變化,請確認這些變更是否為有意進行的操作。如果輸出內容包含類似以下的行:
google_firestore_database.database must be replaced
然後檢查 Terraform 設定檔是否有任何錯誤,特別是 project、location 或 name 欄位,接著再次執行 terraform plan
。如果欄位需要 Terraform 替換資料庫,計畫輸出內容中會標上 # forces replacement
。
確認 Terraform 計畫輸出內容符合需求後,請執行下列指令:
terraform apply
移除 google_app_engine_application 資源
如果 Terraform 設定檔中已有 google_app_engine_application
資源,請立即從該檔案中移除。
接著再次執行:
terraform plan
輸出結果應該會類似下列內容:
Terraform will perform the following actions: # google_app_engine_application.app will be destroyed # (because google_app_engine_application.app is not in configuration)
如果對計畫輸出內容感到滿意,請執行
terraform apply
Terraform 目前不支援刪除 App Engine 資源;雖然 Terraform 會顯示資源已刪除,但實際上不會刪除 App Engine 應用程式。不過,Terraform 將不再管理 App Engine 應用程式。
Firestore API 需求
先前,所有 Datastore 模式的 Firestore 資料庫都會連結至 App Engine 應用程式。現在,Datastore 模式資料庫預設會與 App Engine 取消連結。此外,所有資料庫 (包括現有和新建立的資料庫) 現在都必須符合下列規定:
- 如要透過 Google Cloud 控制台和 gcloud CLI 管理資料庫,必須在專案中啟用 Firestore API。無論是原生模式的 Firestore,還是 Datastore 模式的 Firestore 資料庫,都必須啟用這項功能。
從 Google Cloud 控制台或 gcloud CLI 執行下列管理作業時,需要下列 IAM 權限:
- 建立資料庫:
datastore.databases.create
- 查看資料庫中繼資料:
datastore.databases.getMetadata
- 編輯資料庫中繼資料:
datastore.databases.update
- 建立資料庫:
如果您尚未啟用 Firestore API, Google Cloud 控制台的「Datastore」頁面會顯示錯誤,並包含下列通知:
必須啟用 Firestore API 才能管理資料庫。
這則錯誤訊息也會顯示啟用 Firestore API 的按鈕。 按一下按鈕即可啟用 API。
您也應驗證 IAM 權限,確保資料庫存取權不受影響。
確認透過 Google Cloud 控制台 存取資料庫的帳戶具備上述必要權限。
預先定義的角色 (例如「Datastore 使用者」和「Datastore 檢視者」) 包含必要權限。如果您建立任何自訂 IAM 角色,可能需要更新這些角色,加入上述權限。
如果您先前為 Datastore 定義了自訂角色,該角色可能缺少 datastore.databases.getMetadata
權限。如要確保存取權不受影響,請更新自訂角色,加入 datastore.databases.getMetadata
或使用預先定義的角色。
透過 Terraform 啟用 API
您也可以透過 Terraform 啟用 Firestore API:
resource "google_project_service" "firestore" { project = "project" service = "firestore.googleapis.com" }
如果您有 google_firestore_database
資源,可以對 google_project_service
資源新增依附元件,確保 Terraform 嘗試建立資料庫前已啟用 API:
resource "google_firestore_database" "database" { // ... depends_on = [google_project_service.firestore] }