State backend
Spotlight keeps the Terraform state of a workspace's configuration, through
Terraform's built-in http backend. Each workspace has states by name. See
Keep Terraform state in Spotlight.
| Request | Address | Permission |
|---|---|---|
GET | /api/terraform/workspaces/{slug}/states/{name} | plan |
POST | /api/terraform/workspaces/{slug}/states/{name} | configure |
POST | /api/terraform/workspaces/{slug}/states/{name}/lock | plan |
DELETE with lock info | /api/terraform/workspaces/{slug}/states/{name}/lock | plan for the token or person that took the lock, else configure |
DELETE without a body | /api/terraform/workspaces/{slug}/states/{name}/lock | configure |
{slug} is the workspace's slug. {name} is lowercase words joined by
dashes, at most 64 characters.
Example
terraform {
backend "http" {
address = "https://app.usespotlight.ai/api/terraform/workspaces/acme/states/default"
lock_address = "https://app.usespotlight.ai/api/terraform/workspaces/acme/states/default/lock"
unlock_address = "https://app.usespotlight.ai/api/terraform/workspaces/acme/states/default/lock"
lock_method = "POST"
unlock_method = "DELETE"
username = "spotlight"
}
}Sign-in
The backend sends the token as the password of HTTP basic authentication.
Spotlight ignores the username, but Terraform sends the password only when a
username is set. Set the password in TF_HTTP_PASSWORD, not in the
configuration. The routes also take Authorization: Bearer <token>.
| Token | Source |
|---|---|
| A workspace token | SPOTLIGHT_API_TOKEN, or the token itself |
| A token issued through an OIDC trust | spotlight token with SPOTLIGHT_OIDC_TRUST |
| A login | spotlight token after spotlight login |
A person has the permissions of their role: a member has plan, an owner or
admin has plan and configure. A token has the permissions of its
scopes.
Answers
| Request | Status | When |
|---|---|---|
GET | 200 | The latest version of the state, as JSON. |
GET | 204 | No state was written under the name. |
POST | 200 | The state is kept as a new version. |
POST lock | 200 | The lock is taken, or the same lock ID holds it already. |
DELETE lock | 200 | The lock is released, or nothing held it. |
| any | 423 | Another lock ID holds the lock. The body is that lock's info, which Terraform prints. |
POST | 400 | The Content-MD5 header does not match the body. |
| any | 401 | No token, or a token that is unknown, expired or revoked. |
| any | 403 | The token or role lacks the permission. |
| any | 404 | No such workspace, or the token or person is not in it. |
| any | 409 | Another request changed the state at the same time, or a POST names a lock that was released. Run the command again. |
POST | 409 | The state has another lineage than the latest version, a lower serial, or the same serial with other content. The message says which. |
| any | 422 | A state name that is not lowercase words joined by dashes, lock info without an ID, or a body that is not a Terraform state with a serial and a lineage. |
A POST to a locked state must name the lock in ?ID=<lock id>, as Terraform
does. After the first version, a POST must keep the lineage of the latest
version and a serial at least as high. The same serial is taken again only
with the same content, as when Terraform retries a write. A DELETE without a body is what terraform force-unlock sends, and
releases the lock whoever holds it.
Storage
Each POST keeps a new version. Spotlight records the version's number, the
state's serial and lineage, its size and MD5, and who wrote it, and keeps
the body in the deployment's object storage. GET answers the latest version.
Each state keeps its latest 100 versions: after a write, older versions and
their bodies are deleted. Deleting the workspace deletes its states and every
version.
The body is at most 64 MB.
Audit
These are recorded as configuration changes
of the kind terraform-state, keyed by the state name:
| Change | Action | details |
|---|---|---|
| The first version written | created | version, serial, lineage |
| A later version written | updated | version, serial, lineage |
The lock released without lock info, as terraform force-unlock does | updated | released: "forced", lock, heldBy, operation |
| The lock released by a token or person that did not take it | updated | released: "held-by-another", lock, heldBy, operation |
lock is the lock ID, heldBy the token or person that took the lock, as
token:<id> or person:<id>, and operation Terraform's operation, such as
OperationTypeApply. A lock taken and released by the same token or person is
not recorded.