# A workspace, a service account, and a federation rule that lets GitHub Actions # act as that service account without a stored API key. # # Run with an organization admin's `ant auth login --scope org:admin` profile. # Destroying this configuration can't be undone. To allow it, first apply # `deletion_protection = false` on the workspace, service account, issuer, and # rule. terraform { required_providers { anthropic = { source = "anthropics/anthropic" } } } variable "organization_id" { type = string } # The IDs of the repository and its owner, from # `gh api repos/OWNER/NAME --jq '.id, .owner.id'`. Update the owner's ID if the # repository is transferred. variable "github_repository_id" { type = string } variable "github_repository_owner_id" { type = string } provider "anthropic" { organization_id = var.organization_id } 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 old one can't be archived while the # rule 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" } resource "anthropic_federation_issuer" "github" { name = "github-actions" issuer_url = "https://token.actions.githubusercontent.com" } # Accepts runs started by a push to main. The claims use IDs because someone # can reuse a name after a rename or deletion. resource "anthropic_federation_rule" "deploy" { name = "research-deploy" issuer_id = anthropic_federation_issuer.github.id match = { # Request the token for this audience. audience = "https://api.anthropic.com" claims = { repository_id = var.github_repository_id repository_owner_id = var.github_repository_owner_id ref = "refs/heads/main" # `pull_request_target`, `issue_comment`, and `workflow_run` runs also have # the main ref, but act on someone else's pull request. event_name = "push" } } oauth_scope = "workspace:developer" target = { service_account = { service_account_id = anthropic_service_account.ci.id } } workspace_id = anthropic_workspace.research.id } output "workspace_id" { value = anthropic_workspace.research.id } # To run Terraform in the workflow, use this ID in a federation profile: # https://platform.claude.com/docs/en/cli-sdks-libraries/sdks/terraform/authentication#run-terraform-from-a-pipeline output "federation_rule_id" { value = anthropic_federation_rule.deploy.id }