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 exampleecomm-prod-db.<step>is the step'spathin the bundle'smassdriver.yaml, for examplesrc.
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​
- OpenTofu
- Terraform
Set the backend configuration:
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:
echo 'terraform {
backend "http" {}
}' > backend.tf
tofu init
Then use the state commands as usual:
# 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.
Set the backend configuration:
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:
echo 'terraform {
backend "http" {}
}' > backend.tf
terraform init
Then use the state commands as usual:
# Download the state
terraform state pull > terraform.tfstate
# Import an existing cloud resource
terraform import aws_instance.example i-1234567890abcdef0
# Remove a resource from state without destroying it
terraform state rm aws_instance.example
terraform state list
After terraform import or terraform state rm, update the bundle's code to match, or the next deployment plans changes for the resource. See terraform import and terraform 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.
- OpenTofu
- Terraform
With the existing backend still configured, pull the state. For Terraform Cloud, run
tofu loginfirst.shelltofu state pull > terraform.tfstateReplace the existing backend block with the HTTP backend, set the backend configuration, and reinitialize:
terraformterraform {
backend "http" {}
}shelltofu init -reconfigurePush the state and verify it:
shelltofu state push terraform.tfstate
tofu state list
With the existing backend still configured, pull the state. For Terraform Cloud, run
terraform loginfirst.shellterraform state pull > terraform.tfstateReplace the existing backend block with the HTTP backend, set the backend configuration, and reinitialize:
terraformterraform {
backend "http" {}
}shellterraform init -reconfigurePush the state and verify it:
shellterraform state push terraform.tfstate
terraform state list