Skip to main content

Managing OpenTofu and Terraform State in Massdriver

Massdriver stores OpenTofu and Terraform state for every instance in its HTTP state backend. Each step of a bundle has its own state file at:

https://api.massdriver.cloud/state/<instance-id>/<step>
  • <instance-id> is the instance ID, for example ecomm-prod-db.
  • <step> is the step's path in the bundle's massdriver.yaml, for example src.

The statePaths field of an Instance in the API returns the URL of each step.

The backend uses HTTP Basic auth. The username is your organization ID and the password is an access token: a personal access token or a service account secret. Reading state requires instance:plan on the instance. Writing, locking and unlocking require instance:deploy.

Connecting to the state backend​

Set the backend configuration:

shell
export TF_HTTP_USERNAME="${MASSDRIVER_ORGANIZATION_ID}"
export TF_HTTP_PASSWORD="${MASSDRIVER_API_KEY}"

export MASSDRIVER_INSTANCE_ID="ecomm-prod-db"
export MASSDRIVER_STEP="src"

export TF_HTTP_ADDRESS="https://api.massdriver.cloud/state/${MASSDRIVER_INSTANCE_ID}/${MASSDRIVER_STEP}"
export TF_HTTP_LOCK_ADDRESS="${TF_HTTP_ADDRESS}"
export TF_HTTP_UNLOCK_ADDRESS="${TF_HTTP_ADDRESS}"

Configure the HTTP backend and initialize:

shell
echo 'terraform {
backend "http" {}
}' > backend.tf

tofu init

Then use the state commands as usual:

shell
# Download the state
tofu state pull > terraform.tfstate

# Import an existing cloud resource
tofu import aws_instance.example i-1234567890abcdef0

# Remove a resource from state without destroying it
tofu state rm aws_instance.example

tofu state list

After tofu import or tofu state rm, update the bundle's code to match, or the next deployment plans changes for the resource. See tofu import and tofu state rm.

Migrating state from another backend​

These steps move state from S3 (backend "s3"), Terraform Cloud (backend "remote" or a cloud block), or Azure Blob Storage (backend "azurerm") into Massdriver.

  1. With the existing backend still configured, pull the state. For Terraform Cloud, run tofu login first.

    shell
    tofu state pull > terraform.tfstate
  2. Replace the existing backend block with the HTTP backend, set the backend configuration, and reinitialize:

    terraform
    terraform {
    backend "http" {}
    }
    shell
    tofu init -reconfigure
  3. Push the state and verify it:

    shell
    tofu state push terraform.tfstate
    tofu state list