> ## Documentation Index
> Fetch the complete documentation index at: https://docs.useshipd.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Manage projects and environments with Terraform

> Install the shipd Terraform provider from the Terraform Registry, configure credentials, and provision projects and environments.

## Overview

Use the shipd Terraform provider to create and manage projects and environments, detect drift, or look up existing objects. Continue to manage flag definitions with [config as code](https://docs.useshipd.com/config-as-code/define-flags.md).

See the [Terraform provider changelog](https://docs.useshipd.com/changelog/terraform.md) for the resources each release supports.

> [!NOTE]
>
> This guide pins the v0.1 provider for projects and environments. For the
> credential resources available in later builds, see [Manage credentials from CI](https://docs.useshipd.com/reference/terraform.md#manage-credentials-from-ci).

## Prerequisites

* Terraform 1.11 or later. CI covers Terraform 1.11.4 and 1.17.1; OpenTofu compatibility is not yet established.
* A shipd organization and a [service account token](https://docs.useshipd.com/enterprise/service-accounts.md). To create projects, use an organization-wide token with no project or environment binding.

Grant these scopes for the example below. Create the service account and token in the dashboard before running Terraform.

```json theme={null}
{
  "project": ["read", "create", "update", "archive"],
  "environment": ["read", "create", "update", "archive", "set_production"]
}
```

## Configure credentials

Load your service account token from your secret store into the shell where you will run Terraform:

```bash theme={null}
export SHIPD_API_KEY="shipd_sa_REPLACE_WITH_YOUR_TOKEN"
```

## Create a project and environment

Create `main.tf` in your configuration directory. Terraform installs the published [alasti-company/shipd provider](https://registry.terraform.io/providers/alasti-company/shipd/latest/docs) from the Terraform Registry:

```hcl main.tf theme={null}
terraform {
  required_version = ">= 1.11"

  required_providers {
    shipd = {
      source  = "alasti-company/shipd"
      version = "~> 0.1.0"
    }
  }
}

provider "shipd" {}

resource "shipd_project" "app" {
  name = "Web app"
  slug = "web-app"

  lifecycle {
    prevent_destroy = true
  }
}

resource "shipd_environment" "production" {
  project_id    = shipd_project.app.id
  name          = "Production"
  slug          = "production"
  type          = "production"
  is_production = true

  lifecycle {
    prevent_destroy = true
  }
}

output "environment_id" {
  value = shipd_environment.production.id
}
```

Run these commands from the configuration directory, review the plan, and apply it:

```bash theme={null}
terraform init
terraform plan
terraform apply
```

Commit `.terraform.lock.hcl` to keep provider versions consistent across your team and CI. If you previously used a local build, remove its `dev_overrides` entry from your Terraform CLI configuration before initializing so Terraform uses the published provider.

For existing projects or environments, [import their IDs](https://docs.useshipd.com/reference/terraform.md#import-existing-resources) before applying, or use [data sources](https://docs.useshipd.com/reference/terraform.md#data-sources) to read them without managing their lifecycle.

## Report runs from CI

In **Settings → Service accounts**, create a service account token with the `terraform: report` scope. Bind it to the project and environment your CI job targets and save it as `SHIPD_TERRAFORM_REPORT_TOKEN` in CI. Set `SHIPD_PROJECT_ID`, `SHIPD_ENVIRONMENT_ID`, and `CI_RUN_URL` for the job. The project and environment IDs appear in the example configuration under **Project settings → Integrations → Terraform**.

Run this script directly after your CI job finishes a plan or apply. Pass the operation, its exit code, and the ISO 8601 completion timestamp.

```bash report-terraform-run.sh theme={null}
#!/usr/bin/env bash
set -euo pipefail

kind=$1
exit_code=$2
completed_at=$3
status=failed
if [[ "$exit_code" == 0 || ( "$kind" == plan && "$exit_code" == 2 ) ]]; then
  status=succeeded
fi

jq -n \
  --arg projectId "$SHIPD_PROJECT_ID" \
  --arg environmentId "$SHIPD_ENVIRONMENT_ID" \
  --arg kind "$kind" \
  --arg status "$status" \
  --arg completedAt "$completed_at" \
  --arg runUrl "$CI_RUN_URL" \
  --arg exitCode "$exit_code" '
    {$projectId, $environmentId, $kind, $status, $completedAt, $runUrl}
    + (if $kind == "plan" and $status == "succeeded"
       then {hasChanges: ($exitCode == "2")} else {} end)
  ' | curl --fail-with-body --silent --show-error \
    --header "Authorization: Bearer $SHIPD_TERRAFORM_REPORT_TOKEN" \
    --header "Content-Type: application/json" \
    --data-binary @- https://api.useshipd.com/api/v1/rpc/terraform/report
```

Use `terraform plan -detailed-exitcode` when reporting plans. Exit code `0` means no changes, `2` means changes, and `1` means the plan failed. [Terraform plan command reference](https://developer.hashicorp.com/terraform/cli/commands/plan#other-options)

```bash theme={null}
plan_exit=0
terraform plan -input=false -detailed-exitcode || plan_exit=$?
completed_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)
bash report-terraform-run.sh plan "$plan_exit" "$completed_at" || exit $?
if [ "$plan_exit" -ne 2 ]; then
  exit "$plan_exit"
fi
```

After your CI runs apply, call the script with `apply`, the apply exit code, and its completion timestamp. Keep the original `completedAt` if you retry a report. The endpoint returns `data.recorded: false` when it already has a report for that operation with an equal or newer completion timestamp.

The **Terraform runs** panel shows the latest completed plan and apply for the selected environment, each with its CI run link. Results are marked **Stale** after seven days. Apply status describes the reported run at completion.

## Next

* [Terraform provider reference](https://docs.useshipd.com/reference/terraform.md) for configuration, resources, data sources, and lifecycle behavior.
* [Define flags in code](https://docs.useshipd.com/config-as-code/define-flags.md) after provisioning your project and environment.
* [Service account tokens](https://docs.useshipd.com/reference/service-account-tokens.md) for scopes and binding rules.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.