Manage Spotlight with Terraform
Once a workspace is under Terraform, its teams, lines, skills, integrations,
hooks, harness profiles, secrets, OIDC trusts and runner configuration change
through terraform apply. You need:
- Terraform 1.11 or later. The module contains write-only values, such as secret values, which older versions cannot send.
- A workspace token for the workspace, to export it. If you don't have one,
issue a workspace token with the scopes
for what the module manages:
configure, plussecretsif it manages secrets,runnersif it manages runner pools or runner trusts, andtrustsif it manages OIDC trusts. - To apply from your machine as yourself, the
spotlightbinary. With it, you can log in from the command line in place of the token. A login acts with your role, so a module that manages secrets, runners or OIDC trusts needs an owner or admin.
Export the workspace
To have the agent write the first Terraform for you, adapted to your code and tools, let the agent write your team's first Terraform instead, then go on to Point the provider at Spotlight.
Otherwise, write the workspace's export
into the folder where the configuration will live, usually a repository of its
own. You need curl and jq. Put the token in SPOTLIGHT_API_TOKEN, replace
<slug> with the workspace's short name from its URL, and run this in that
folder:
export SPOTLIGHT_API_TOKEN=<workspace token>
api=https://app.usespotlight.ai/api/v1/resources/workspaces
id=$(curl -fsS -H "Authorization: Bearer $SPOTLIGHT_API_TOKEN" "$api/import?address=<slug>" | jq -r .id)
curl -fsS -H "Authorization: Bearer $SPOTLIGHT_API_TOKEN" "$api/$id/export" \
| jq -c '.files[]' | while read -r file; do
path=$(jq -r .path <<<"$file"); mkdir -p "$(dirname "$path")"
jq -j .content <<<"$file" > "$path"
doneIt writes:
| File | Contains |
|---|---|
terraform.tf | The required_providers block |
imports.tf | An import block for everything in the workspace. Remove it after the first apply. |
workspace.tf | The workspace and its OIDC trusts |
secrets.tf | The secrets; left out when there are none |
runners.tf | Runner trusts, egresses, runner pools and the workspace's runners; left out when there are none |
team-<team>.tf | One file for each team |
line-<team>-<line>.tf | One file for each line |
integration-<team>-<integration>.tf | One file for each integration |
skills-<team>.tf, hooks-<team>.tf, profiles-<team>.tf | A team's skills, hooks and harness profiles; left out when the team has none |
variables.tf | A sensitive variable for each value Spotlight never returns, such as a secret's value or a runner pool's token; left out when there are none |
prompts/ | Each station's prompt, as prompts/<team>/<line>/<station>.md, read with file() |
skills/ | Each skill's files, as skills/<team>/<skill>/, read with file() |
.gitignore | .terraform/, state files and *.tfvars, so they stay out of version control |
Terraform reads every .tf file at the top of the folder as one module. Keep
the files there, and do not move them into subfolders.
Point the provider at Spotlight
The provider uses a workspace token or an OIDC trust when one is set.
When neither is set, it uses the login that spotlight login keeps. See Provider configuration.
On a self-hosted Spotlight, use your deployment's address in place of app.usespotlight.ai.
Log in once. The provider needs nothing else:
spotlight loginThe provider uses the address of your login. If you logged in to more than one
address, set SPOTLIGHT_URL to the one to use.
Do not set SPOTLIGHT_API_TOKEN or SPOTLIGHT_OIDC_TRUST. If one of them is
set, the provider uses it, not your login.
The export has no provider block. To set the address and token in the configuration instead, add one and keep the token in a variable:
provider "spotlight" {
url = "https://app.usespotlight.ai"
token = var.spotlight_token
}
variable "spotlight_token" {
type = string
sensitive = true
}Keep the token out of version control. With a login, the block needs only
url.
Give the variables their values
Give each variable in variables.tf that has no default the value Spotlight
has for it, for example in a terraform.tfvars file you keep out of version
control:
secret_linear_api_key = "<the secret's value>"A variable for a pull pool's token defaults to null and needs no value to
adopt the pool.
Adopt the workspace
terraform init
terraform applyThe first apply imports every resource through its import block and plans no
change. If the plan shows changes, the workspace changed after you downloaded it:
download it again and repeat.
After the apply you can delete the import blocks, or leave them; Terraform
ignores an import block for a resource already in its state.
Make changes
Edit the configuration and run terraform apply. The plan checks every
expression in an integration or a hook against Spotlight before anything is
written, so a misspelt field fails the plan at the attribute that contains it.
If a plan fails with 403, the diagnostic names the scope the token lacks.
Issue a new token that also has that scope. With a login, your role does not
allow the change; ask an owner or admin.
If the configuration is changed outside Terraform, through the resource API, the next plan changes it back.
Add a workspace to an existing configuration
A token cannot create a workspace. A person creates one by signing up, and
Terraform adopts it with an import block that names its slug:
import {
to = spotlight_workspace.acme
id = "acme"
}
resource "spotlight_workspace" "acme" {
name = "Acme"
slug = "acme"
}Every other resource can be imported by an id of the form
<workspace id>/<address>, as the downloaded module does, or by its identity in
Terraform 1.12 and later:
import {
to = spotlight_line.core_delivery
identity = {
id = "<line id>"
}
}Stop managing a workspace
To stop managing the workspace without deleting it, replace its resource with a
removed block:
removed {
from = spotlight_workspace.acme
lifecycle {
destroy = false
}
}Related
- Provider: every resource, attribute and import id.
- Run Terraform from GitHub Actions without a stored token
- Why configuration lives in Terraform