{
  "id": "terraform-14",
  "title": "Service account, credential and data-source lookup",
  "description": "Create a Service Account with the all_editor role and a credential for it, keep the credential file as a sensitive output, and read the account back with the indykite_service_accounts data source.",
  "category": "Environment",
  "tags": [
    "Service Account",
    "Credentials",
    "Organization",
    "Data Source"
  ],
  "lastUpdated": "2026-10-12",
  "terraformProviderDocs": [
    "https://registry.terraform.io/providers/indykite/indykite/latest/docs/resources/service_account",
    "https://registry.terraform.io/providers/indykite/indykite/latest/docs/resources/service_account_credential",
    "https://registry.terraform.io/providers/indykite/indykite/latest/docs/data-sources/service_accounts"
  ],
  "relatedGuides": [
    "/guides/guide-credentials",
    "/guides/guide-environment",
    "/guides/guide-terraform"
  ],
  "commonErrors": [
    {
      "code": "ALREADY_EXISTS",
      "message": "Conflict",
      "solution": "name is unique within the organization and immutable. Choose another name, or import the existing account into Terraform state."
    }
  ],
  "summary": "A Service Account is the identity that calls the Config API and runs Terraform; it belongs to the organization, not to a project, and carries a role: all_editor may change configuration, all_viewer may only read.\nThis Terraform configuration:\n1. Looks the organization up by name with the indykite_customer data source.\n2. Creates a Service Account with the all_editor role.\n3. Creates a credential for it with an expire_time within two years, so the returned credential file carries a ready-to-use token, and exposes that file as a sensitive output.\n4. Reads the account back by exact name with the indykite_service_accounts data source, the pattern every list-by-name data source of the provider follows (a parent identifier plus a filter of exact names).",
  "usecase": "Scenario: a CI pipeline needs its own identity to apply the organization's Terraform modules, separate from the people's accounts.\n- The account and its credential are created once by an administrator's Terraform run; the credential file goes to the pipeline's secret store through the sensitive output.\n- The pipeline then authenticates with that file (INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE) and applies the project modules.\n- Rotation is a second indykite_service_account_credential resource added before the first is removed; a read-only auditor account is the same resource with role all_viewer.",
  "requirements": "- ServiceAccount credentials created in the IndyKite Hub for your organization, to run this configuration with.\n- IndyKite Terraform provider 0.49.0 or later, the release the example pins in required_providers; the Terraform guide lists the version each feature first appeared in.\n- The organization's name for the indykite_customer lookup.",
  "steps": "1. Put your organization's name in the indykite_customer data source and apply.\n2. Read the credential file from the sensitive output (terraform output -raw ci_credentials_file) straight into your secret store.\n3. Configure the consumer, here a CI pipeline, with that file and run its own Terraform.\n4. To rotate, add a second credential resource, switch the consumer to it, then remove the first.",
  "code": [
    {
      "description": "A Service Account with a credential, exposed as a sensitive output, and a data-source lookup by name.",
      "filename": "main.tf",
      "code": "\nterraform {\n  required_providers {\n    indykite = {\n      source  = \"indykite/indykite\"\n      version = \">= 0.49.0\" # or a later release\n    }\n  }\n}\n\n# indykite provider integrates IndyKite platform with Terraform scripting.\n# Provider for now does not support any parameters and all is set within service account credential file.\nprovider \"indykite\" {}\n\n# The organization the service account belongs to, looked up by name.\ndata \"indykite_customer\" \"org\" {\n  name = \"your-organization-name\"\n}\n\n# A Service Account authenticates Config API and Terraform calls. role is\n# all_editor (may change configuration) or all_viewer (read only) and cannot be\n# changed after creation.\nresource \"indykite_service_account\" \"ci\" {\n  customer_id  = data.indykite_customer.org.id\n  name         = \"terraform-ci\"\n  display_name = \"CI pipeline\"\n  description  = \"Applies this organization's Terraform modules\"\n  role         = \"all_editor\"\n}\n\n# A credential for it. With an expire_time at most two years ahead the\n# credential file (service_account_config) carries a ready-to-use token.\nresource \"indykite_service_account_credential\" \"ci\" {\n  service_account_id = indykite_service_account.ci.id\n  display_name       = \"CI pipeline credential\"\n  expire_time        = \"2027-10-01T00:00:00Z\"\n}\n\n# The credential file is sensitive: write it to your secret store, never to logs.\noutput \"ci_credentials_file\" {\n  value     = indykite_service_account_credential.ci.service_account_config\n  sensitive = true\n}\n\n# Reading a service account back by exact name with a data source.\ndata \"indykite_service_accounts\" \"ci\" {\n  customer_id = data.indykite_customer.org.id\n  filter      = [indykite_service_account.ci.name]\n  depends_on  = [indykite_service_account.ci]\n}\n\noutput \"ci_service_account_id\" {\n  value = data.indykite_service_accounts.ci.service_accounts[0].id\n}\n"
    }
  ],
  "links": {
    "html": "/terraform/terraform-14",
    "markdown": "/terraform/terraform-14.md",
    "terraformRegistry": "https://registry.terraform.io/providers/indykite/indykite/latest/docs"
  }
}