Claude Platform Docs
SDKs, CLI, and librariesTerraform provider

Authenticate the Terraform provider

Give the Terraform provider a credential in a pipeline or at a terminal, and use more than one identity.

Before you set up a credential, check Credentials for each resource for the scope that your resources need. Use Workload Identity Federation for pipelines and other automation. Use an ant auth login profile when you run Terraform yourself at a terminal. Provider blocks and credentials on this page are for the Claude API except for those in the Cloud platforms section.

Run Terraform from a pipeline

  1. Set up federation to create a service account, an issuer, and a rule. To create all three with Terraform instead, first sign in with ant auth login --profile admin --scope org:admin. Then apply Example: a team’s workspace and CI identity once at a terminal.
  2. In the job, save the workload’s identity token to a file. The guide for your identity provider shows where the token comes from.
  3. Write a federation profile that names your rule, service account, organization, and token file. If the rule covers all workspaces or more than one, the profile file’s workspace_id setting names the workspace to use. Save it as configs/<name>.json in the configuration directory.
  4. Set ANTHROPIC_PROFILE to <name>.

If your job or shell exports ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN, remove that variable from the job or shell. Unless the provider block sets profile, a variable that’s set wins over a profile, and the provider prints no warning.

Use a profile rather than the federation environment variables, or terraform apply fails on GitHub Actions. terraform apply connects twice, once to plan and once to apply. By default, the API accepts a GitHub Actions identity token only one time (see JWT verification), so the second connection needs the access token that a profile saves.

Run Terraform at a terminal

At a terminal, run ant auth login. Each login makes its profile the active one, so the provider finds it with no variable to set. A variable earlier in the lookup order, such as ANTHROPIC_API_KEY, wins over a login, so unset it.

If you can use neither federation nor a login, see Authentication for other ways to authenticate.

Pin the provider to one credential

For the order in which the provider looks for a credential, see Credential lookup order.

To make a configuration always use one profile, set profile in the provider block. The block then ignores every other credential in the environment.

Don’t write a key or token in the provider block, because anything you write there also ends up in plan files.

Organization credentials

On the Claude API, a Workload Identity Federation profile can manage the organization resources, within the limits in Permissions and constraints. The profile’s federation rule must grant the org:admin scope, and must target a service account whose organization_role is admin. developer, the default, isn’t enough. Create the org:admin rule in the Claude Console, as Bootstrap a workload to manage WIF describes.

At a terminal, an organization admin can sign in with ant auth login --profile admin --scope org:admin (see Admin access). That makes admin the active profile. To switch back, run ant profile activate default.

Don’t keep a long-lived organization admin credential on a shared runner. Provisioners, external data sources, and other providers in the same configuration can read credentials from the environment.

Example: a team’s workspace and CI identity

This example is based on organization/federated-ci in the provider’s repository. It sets up a team: a workspace, a service account with the developer role there, a federation issuer, and a federation rule. The issuer and rule let the team’s GitHub Actions workflows act as the service account, with no stored credential.

Declare and set the variables github_repository_id and github_repository_owner_id as strings that hold the numeric IDs of the team’s GitHub repository and its owner. To find both IDs, run gh api repos/example-org/research-agents --jq '.id, .owner.id', replacing example-org/research-agents with your repository’s full name. The command prints the repository’s ID, then its owner’s.

The example’s resource blocks use the default provider block, so sign in with ant auth login --profile admin --scope org:admin before you apply. You need the aliased block from Manage organization and workspace resources together only if your configuration also manages Managed Agents resources in a workspace that already exists. For a workspace that Terraform creates, see Use a workspace that Terraform creates.

If your organization already has an issuer for GitHub Actions, don’t create one. Bring it under Terraform as Import existing resources describes, and set name to the name that it has. If that issuer backs a rule with the org:admin scope, make any later change to the issuer in the Claude Console.

Add these resource blocks to the configuration from Add the provider block:

resource "anthropic_workspace" "research" {
  name = "research"
}

resource "anthropic_service_account" "ci" {
  name        = "research-ci"
  description = "Deploys the research team's agents from CI."

  # A rename replaces the account, and the API won't archive the old one while
  # the federation rule later in this example still targets it.
  lifecycle {
    create_before_destroy = true
  }
}

resource "anthropic_workspace_service_account" "ci" {
  workspace_id       = anthropic_workspace.research.id
  service_account_id = anthropic_service_account.ci.id
  workspace_role     = "workspace_developer"
}

# GitHub's OIDC issuer. Its signing keys come from OIDC discovery.
resource "anthropic_federation_issuer" "github" {
  name       = "github-actions"
  issuer_url = "https://token.actions.githubusercontent.com"
}

# This rule applies when a push to the repository's main branch starts a
# workflow run. The run exchanges its OIDC token for a short-lived access
# token that acts as the service account in the workspace. The rule matches
# claims, not the token's subject, because GitHub Actions writes the subject
# in more than one format.
resource "anthropic_federation_rule" "deploy" {
  name      = "research-deploy"
  issuer_id = anthropic_federation_issuer.github.id
  match = {
    claims = {
      repository_id       = var.github_repository_id
      repository_owner_id = var.github_repository_owner_id
      ref                 = "refs/heads/main"
      event_name          = "push"
    }
  }
  oauth_scope = "workspace:developer"
  target = {
    service_account = { service_account_id = anthropic_service_account.ci.id }
  }
  workspace_id = anthropic_workspace.research.id
}

# The workflow authenticates with this rule.
output "federation_rule_id" {
  value = anthropic_federation_rule.deploy.id
}

A service account is only an identity. What it can do comes from its workspace memberships and from the federation rules that target it. A token gets only what both the rule’s oauth_scope and the service account’s organization_role allow. organization_role is an optional argument that sets the account’s role in the organization. The example doesn’t set it, so the account gets the default, developer. For details, see Permission boundaries.

To run Terraform from the team’s workflow, follow the steps in Run Terraform from a pipeline. The rule’s scope is workspace:developer, so that workflow can manage resources inside the workspace, but not organization resources such as those in this example.

Manage organization and workspace resources together

Give each provider block only the access it needs. Credentials for each resource shows which credential each resource accepts. To manage organization resources and Managed Agents resources in one configuration, use two provider blocks:

  • The default provider block from Add the provider block has no alias. Every resource without a provider line uses this block. At a terminal, this block uses the active profile, so make that a workspace login. Run ant profile activate default. If you have no profile named default, run ant auth login --profile default instead.
  • A second block has an alias, and its profile argument names the organization-level profile. This block can’t set workspace_id. Each organization resource or data source names this block in its provider line.

This example shows the second block, and a resource that uses it:

# The profile from `ant auth login --profile admin --scope org:admin`.
provider "anthropic" {
  alias           = "admin"
  organization_id = var.organization_id
  profile         = "admin"
}

resource "anthropic_workspace" "research" {
  provider = anthropic.admin
  name     = "research"
}

The default block’s Managed Agents resources go in the workspace its credential acts in, which must exist before you plan. For a workspace that Terraform creates, see Use a workspace that Terraform creates.

Use a workspace that Terraform creates

To manage Managed Agents resources in a workspace that Terraform creates, use two configurations. One terraform apply can’t both create the workspace and manage those resources, because the provider authenticates before it plans, and the credential determines the workspace. The first configuration creates the workspace and a federation rule for it, as Example: a team’s workspace and CI identity does. The second runs with a credential for that workspace.

Set workspace_id in the second configuration’s provider block, so that the provider stops if the credential acts in another workspace.

Multiple identities

To work as more than one identity in a single configuration, give each extra identity its own aliased provider block. A resource then selects a block with provider, and a module with providers:

# Identity 1: whatever the environment provides, such as a federation
# profile in ANTHROPIC_PROFILE or the active `ant auth login` profile.
provider "anthropic" {
  organization_id = var.organization_id
}

# Identity 2: the `ant auth login --profile research` profile and
# nothing else, in a different organization.
provider "anthropic" {
  alias           = "research"
  organization_id = var.research_organization_id
  profile         = "research"
}

module "research" {
  source    = "./modules/research"
  providers = { anthropic = anthropic.research }
}

The managed-agents/multiple-identities example in the provider’s repository shows the full pattern, including a module used twice.

After ant auth login --profile research, the first block also uses the research profile, unless an environment variable comes earlier in the lookup order. That profile belongs to a different organization, so the provider stops with an error before any plan or apply. To switch back, run ant profile activate default.

Pin a federation rule to its workload

  • Match on claims that identify the workload, such as the repository, its owner, the branch or environment, and the event that started the run. Prefer claims that hold IDs over names, because someone can reuse a name after a rename or a deletion.
  • Some issuers serve many customers, such as the issuer of a hosted CI service. There, every rule must name your own organization, project, or repository on that platform. Otherwise, the rule would accept tokens from every customer of that issuer.
  • The API rejects that kind of rule on certain shared issuers, such as GitHub Actions and GitLab.com. Don’t rely on that check for other issuers. On GitHub Actions, use the repository_id claim to identify your repository.
  • Use a subject_prefix if your issuer always writes the subject in the same format. The prefix should name your organization or project, and the branch or environment. Without a trailing *, a subject_prefix must equal the whole subject.
  • Set audience only if your workloads mint tokens with a custom audience.
  • Prefer workspace_id over applies_to_all_workspaces, unless the rule must apply to every workspace in the organization.
  • Let an issuer find its signing keys through OIDC discovery. Avoid setting inline keys in the issuer’s jwks attribute. Inline keys rotate only when you change the configuration.

For how each match field works, see Rule matching semantics.

Through the API, a rule’s oauth_scope can only be workspace:developer or workspace:inference. Manage rules with any other scope, such as org:admin, in the Claude Console. See Permissions and constraints.

Set a rule’s issuer_id and target.service_account.service_account_id by reference, as in issuer_id = anthropic_federation_issuer.github.id, not as literal strings. That way, Terraform destroys the rule before the issuer or the service account. The API can’t archive an issuer or a service account while a rule that isn’t archived refers to it.

Cloud platforms

To manage resources on a cloud platform, set platform in the provider block. The Terraform Registry lists every argument. For each platform’s resources, see Resources on each cloud platform.

Claude Platform on AWS

Create your first workspace in the AWS Console. To find its ID, see Obtain your workspace ID. Set aws_region to the workspace’s AWS region.

All of these arguments are required:

provider "anthropic" {
  platform       = "aws"
  aws_region     = "us-west-2"
  aws_account_id = "123456789012"
  workspace_id   = "wrkspc_01AbCdEf23GhIj"
}

The provider signs requests with AWS credentials from the default AWS credential provider chain, or from the profile in aws_profile. It stops unless they belong to the account in aws_account_id. The credentials also need the permissions in IAM policies.

anthropic_workspace creates a workspace in the block’s aws_region. To use that workspace, apply the resource in one configuration. Then put the workspace’s ID in the provider block of a second configuration.

Claude Models on Google Cloud (Anthropic managed)

Use the Default Workspace, or create another workspace in the Claude Console. To find a workspace’s ID, see Find your workspace ID.

All of these arguments are required:

provider "anthropic" {
  platform             = "google_cloud"
  google_cloud_project = "my-project-id"
  workspace_id         = "wrkspc_01AbCdEf23GhIj"
}

The provider authenticates with Application Default Credentials. The account behind them needs an IAM role on the project. anthropic_external_key needs roles/claude.admin.

Claude Managed Agents on Amazon Bedrock

To set up your AWS account, see Claude Managed Agents for Amazon Bedrock. Set aws_region to one of the AWS regions where Claude Managed Agents is available.

All of these arguments are required except workspace_id:

provider "anthropic" {
  platform       = "bedrock"
  aws_region     = "us-east-1"
  aws_account_id = "123456789012"
  workspace_id   = "proj_01AbCdEf23GhIj"
}

workspace_id names the Amazon Bedrock project: default, or an ID that starts with proj_. It defaults to default, and the provider doesn’t check that the project exists. The provider doesn’t create IAM roles or Amazon Bedrock projects.

The provider signs requests with AWS credentials from the default AWS credential provider chain, or from the profile in aws_profile. It stops unless they belong to the account in aws_account_id. Unset AWS_BEARER_TOKEN_BEDROCK: Claude Managed Agents doesn’t accept an Amazon Bedrock API key, so the provider stops if it’s set.

If your AWS account doesn’t have Claude Managed Agents in aws_region, a run that reads or deletes a resource stops with Cannot tell whether the resource exists. The same happens if the project doesn’t exist. Either way, the resource stays in state. Check aws_region and workspace_id, and run the command again. Run terraform state rm only if the resource is gone.

These arguments aren’t available on this platform:

  • organization_id
  • profile
  • api_key
  • auth_token
  • base_url

Was this page helpful?