---
title: 操作 Terraform provider
url: https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/operations
description: 首次 apply 之後的工作：匯入既有資源、保護 state 與 plan 的安全，以及開啟日誌。
---

## 固定 provider 的版本

`terraform init` 會選取最新版本，並將其記錄在 `.terraform.lock.hcl` 中。請提交該檔案，讓每台機器都使用相同的版本，直到您執行 `terraform init -upgrade` 為止。

## 在模組中使用 provider

任何使用此 provider 的資源或資料來源的模組，都需要在其自身的 `required_providers` 區塊中加入 `source = "anthropics/anthropic"`。若沒有這一行，Terraform 會搜尋 `hashicorp/anthropic`，導致 `terraform init` 失敗。

## 銷毀或取代受保護的資源

若要銷毀或取代受保護的資源，請先設定 `deletion_protection = false`，並單獨 apply 該變更。

接著再銷毀或取代該資源。取代之後，請再次設定 `deletion_protection = true`，然後 apply。[「Deletion protection」（刪除保護）](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/behavior#deletion-protection)說明了為何關閉保護需要單獨進行一次 apply。

## 重構而不封存

使用 `moved` 區塊變更資源的位址，並使用帶有 `lifecycle { destroy = false }` 的 `removed` 區塊停止管理某個資源。對於任何絕不能因錯誤的 plan 而被封存的項目，請加上 `lifecycle { prevent_destroy = true }`。

## 重新命名服務帳戶

重新命名會取代服務帳戶。若要重新命名某個聯合規則所指向的服務帳戶：

1. 在服務帳戶上加入 `lifecycle { create_before_destroy = true }`。
2. 關閉服務帳戶上的[刪除保護](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/behavior#deletion-protection)，並單獨 apply 該變更。
3. 變更帳戶名稱，然後 apply。
4. 再次設定 `deletion_protection = true`，然後 apply。

有了 `create_before_destroy`，Terraform 會先建立新帳戶並更新規則，然後才封存舊帳戶。

## 將加密金鑰附加至工作區

一旦將金鑰附加至工作區，您就無法將其卸除或取代。

在將金鑰附加至工作區之前，請先[授予 Anthropic 存取該金鑰的權限](https://platform.claude.com/docs/zh-TW/manage-claude/cmek#set-it-up)。附加的方式取決於您的 provider 區塊所使用的平台，而非金鑰所在的位置：

* 在 Claude API 上，單獨 apply `anthropic_external_key` 資源，並[驗證金鑰](https://platform.claude.com/docs/zh-TW/manage-claude/cmek-admin-api#create-validate-attach)。接著在 `anthropic_workspace` 上設定 `external_key_id`。
* 在 Claude Platform on AWS 上，宣告 `anthropic_external_key` 資源，並在 `anthropic_workspace` 上設定 `external_key_id`。附加時會檢查金鑰，如[在 Claude Platform on AWS 上設定 CMEK](https://platform.claude.com/docs/zh-TW/manage-claude/cmek-claude-platform-on-aws#register-the-key-with-anthropic) 所述。
* 在 Claude Models on Google Cloud (Anthropic managed) 上，apply `anthropic_external_key` 資源。接著在 Claude Console 中驗證金鑰並附加它，如[使用 Google Cloud KMS 設定 CMEK](https://platform.claude.com/docs/zh-TW/manage-claude/cmek-google-cloud-kms#register-the-key-with-anthropic) 所述。

一旦有任何工作區使用某個 `anthropic_external_key`，您就無法變更該資源的 `provider_config`。若要使用不同的金鑰，請使用該金鑰建立新的工作區，如[變更金鑰或停止使用 CMEK](https://platform.claude.com/docs/zh-TW/manage-claude/cmek-key-lifecycle#change-keys) 所述。

## 匯入既有資源

您可以匯入[Terraform provider 的行為方式](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/behavior#resources-and-what-terraform-destroy-does)中列出的所有資源。每個資源的參考頁面（位於 [Terraform Registry](https://registry.terraform.io/providers/anthropics/anthropic/latest/docs) 上）會顯示其匯入 ID。加入 `import` 區塊，然後產生資源區塊：

```hcl
import {
  to = anthropic_workspace.research
  id = "wrkspc_01AbCdEf23GhIj"
}
```

```bash
terraform plan -generate-config-out=generated.tf
```

產生的設定會缺少您所匯入資源中的部分值：

* 機密資訊，因為無法匯入。它們應放在 vault 憑證以及部署的 GitHub 儲存庫的唯寫引數中。這些引數的名稱以 `_wo` 結尾。
* Skill 檔案，因為無法匯入。它們應放在每個 skill 的 `file` 區塊中。
* URL，因為屬於敏感資訊，所以產生的設定會將其保留為 `null`。它們應放在每個 agent 的 `mcp_servers` 中，以及 vault 憑證的 `mcp_server_url` 與 `token_endpoint` 中。

請在 `generated.tf` 中加入這些值。

匯入的 `anthropic_beta_telemetry_destination` 會保留它傳送至您的可觀測性平台的請求標頭（例如該平台的 API 金鑰）。產生的設定中沒有 `headers_wo`，因為 API 從不傳回標頭的值。若要在 Terraform 中管理它們，請在同一次 apply 中設定 `headers_wo` 以及新的 `headers_wo_version`。

接著執行 `terraform plan` 檢查結果，並執行 `terraform apply` 完成匯入。

## 建立失敗後的復原

發生伺服器錯誤或連線中斷後，為了避免產生重複項目，provider 不會重試大多數的 create 操作。在上述任一情況或 apply 被中斷後，該物件可能已經存在，但並未記錄在 Terraform 的「state」（狀態）中。

請檢查該物件，若其存在，請依照[匯入既有資源](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/operations#import-existing-resources)所述將其匯入。若您未匯入它，下一次 apply 可能會建立重複項目或失敗。

## 保護 state 與 plan 的安全

* 將 state 儲存在具備加密、版本控制、存取日誌、存取控制及鎖定功能的遠端後端中，且僅允許套用它的管線讀取。例如，使用搭配 KMS 金鑰與最小權限 IAM 的 S3、搭配客戶管理金鑰的 GCS，或 HCP Terraform。
* 切勿提交 `.tfstate` 檔案，並將已儲存的 plan，以及 CI 中任何 state 或 plan 成品視為機密資訊。
* 使用 `ephemeral = true` 宣告提供唯寫引數的變數，因為已儲存的 plan 會記錄每個非 ephemeral 變數的值。

其餘內容請參閱 HashiCorp 的[管理敏感資料](https://developer.hashicorp.com/terraform/language/manage-sensitive-data)指南。

## 從 Terraform 輸出啟動工作階段

工作階段並非資源。若要依排程啟動工作階段，請改用[排程部署](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/behavior#deployments)。

工作階段需要 agent 的 `id` 與 `environment_id`。它也可以指定 agent 的 `version`、`vault_ids`，以及要作為 `resources` 掛載的任何記憶體存放區。請從您的設定中輸出這些值。

[`managed-agents/coding-agent`](https://github.com/anthropics/terraform-provider-anthropic/tree/main/examples/managed-agents/coding-agent) 範例會將整個請求主體轉譯為 `session_request` 輸出。執行 `terraform output -raw session_request` 以印出可傳遞給 SDK 建立工作階段方法的主體。請參閱[啟動工作階段](https://platform.claude.com/docs/zh-TW/managed-agents/sessions)。

## 偵測在 Terraform 之外所做的變更

依排程執行 `terraform plan` 以偵測「drift」（漂移）。同時也依相同排程執行 `terraform plan -refresh-only`。該指令會顯示[有人在其他工作區中啟用的聯合規則](https://platform.claude.com/docs/zh-TW/cli-sdks-libraries/sdks/terraform/behavior#federation-rules)，但前提是自該變更以來尚未執行過任何 `terraform apply`。

## 開啟日誌

若要記錄每個請求與回應，請設定 `TF_LOG_PROVIDER=DEBUG`：

```bash
TF_LOG_PROVIDER=DEBUG terraform apply 2> provider.log
```

日誌會遮蔽 API 金鑰、權杖，以及 schema 標記為敏感的每個欄位。
