Claude Platform Docs
SDKs, CLI, and librariesTerraform provider

Operate the Terraform provider

Tasks for after the first apply: import existing resources, keep state and plans safe, and turn on logs.

Pin the provider’s release

terraform init selects the newest release and records it in .terraform.lock.hcl. Commit that file, so that every machine uses the same release until you run terraform init -upgrade.

Use the provider in a module

Any module that uses the provider’s resources or data sources needs source = "anthropics/anthropic" in its own required_providers block. Without it, Terraform searches for hashicorp/anthropic, and terraform init fails.

Destroy or replace a protected resource

To destroy or replace a protected resource, first set deletion_protection = false and apply that change on its own.

Then destroy or replace the resource. After a replacement, set deletion_protection = true again, and apply. Deletion protection explains why turning protection off needs its own apply.

Refactor without archiving

Change a resource’s address with moved blocks, and stop managing one with a removed block with lifecycle { destroy = false }. Add lifecycle { prevent_destroy = true } to anything that a mistaken plan must never archive.

Rename a service account

A rename replaces a service account. To rename one that a federation rule targets:

  1. Add lifecycle { create_before_destroy = true } to the service account.
  2. Turn off deletion protection on the service account, and apply that change on its own.
  3. Change the account’s name, and apply.
  4. Set deletion_protection = true again, and apply.

With create_before_destroy, Terraform creates the new account and updates the rule before it archives the old account.

Attach an encryption key to a workspace

Once you attach a key to a workspace, you can’t detach or replace it.

Grant Anthropic access to the key before you attach it to a workspace. How you attach it depends on the platform that your provider block uses, not on where the key lives:

  • On the Claude API, apply the anthropic_external_key resource on its own and validate the key. Then set external_key_id on the anthropic_workspace.
  • On Claude Platform on AWS, declare the anthropic_external_key resource, and set external_key_id on an anthropic_workspace. Attaching checks the key, as Set up CMEK on Claude Platform on AWS describes.
  • On Claude Models on Google Cloud (Anthropic managed), apply the anthropic_external_key resource. Then verify the key and attach it in the Claude Console, as Set up CMEK with Google Cloud KMS describes.

Once any workspace uses an anthropic_external_key, you can’t change that resource’s provider_config. To use a different key, create a new workspace with it, as Change keys or stop using CMEK describes.

Import existing resources

You can import every resource that How the Terraform provider behaves lists. Each resource’s reference page on the Terraform Registry shows its import ID. Add an import block, and then generate the resource block:

import {
  to = anthropic_workspace.research
  id = "wrkspc_01AbCdEf23GhIj"
}
terraform plan -generate-config-out=generated.tf

The generated configuration is missing some values inside the resources that you import:

  • Secrets, which can’t be imported. They go in the write-only arguments of vault credentials and of a deployment’s GitHub repository. Those arguments’ names end in _wo.
  • Skill files, which can’t be imported. They go in each skill’s file blocks.
  • URLs, which are sensitive, so the generated configuration leaves them null. They go in each agent’s mcp_servers, and in vault credentials’ mcp_server_url and token_endpoint.

Add these values in generated.tf.

An imported anthropic_beta_telemetry_destination keeps the request headers it sends to your observability platform, such as the platform’s API key. The generated configuration doesn’t have headers_wo, because the API never returns the header values. To manage them in Terraform, set headers_wo and a new headers_wo_version in the same apply.

Then run terraform plan to check the result, and run terraform apply to finish the import.

Recover after a failed create

After a server error or a dropped connection, the provider doesn’t retry most creates, to avoid duplicates. After either one, or an interrupted apply, the object might exist but be missing from Terraform state.

Check for the object, and if it exists, import it as Import existing resources describes. If you don’t import it, the next apply can create a duplicate or fail.

Keep state and plans safe

  • Store state in an encrypted, versioned, access-logged, access-controlled remote backend with locking, readable only by the pipeline that applies it. For example, use S3 with a KMS key and least-privilege IAM, GCS with a customer-managed key, or HCP Terraform.
  • Never commit .tfstate files, and treat saved plans, and any state or plan artifacts in CI, as secrets.
  • Declare the variables that supply write-only arguments with ephemeral = true, because a saved plan records the value of every variable that isn’t ephemeral.

HashiCorp’s Manage sensitive data guide covers the rest.

Start a session from Terraform outputs

Sessions aren’t resources. To start sessions on a schedule, use a scheduled deployment instead.

A session needs the agent’s id and the environment_id. It can also name the agent’s version, the vault_ids, and any memory stores to mount as resources. Output those values from your configuration.

The managed-agents/coding-agent example renders the whole request body as a session_request output. Run terraform output -raw session_request to print a body that you can pass to the SDK’s method for creating a session. See Start a session.

Catch changes made outside Terraform

Run terraform plan on a schedule to catch drift. Also run terraform plan -refresh-only on the same schedule. That command shows a federation rule that someone enabled in another workspace, but only if no terraform apply has run since the change.

Turn on logs

To log every request and response, set TF_LOG_PROVIDER=DEBUG:

TF_LOG_PROVIDER=DEBUG terraform apply 2> provider.log

The log redacts API keys, tokens, and every field that the schema marks as sensitive.

Was this page helpful?