Ignitify.dev
Recipes
Terraform

Recipes

Short, practical setups. Every sample works with Terraform and with OpenTofu 1.11 or later. Replace the names with your own.

Set up a tenant from zero

The tenant already exists, because it comes from sign-up. This recipe fills it with access, a secret, a task, its code and a hook. Use two root modules, so the daily job never holds the owner key.

1 · Access, with the owner key

Apply this once, by hand. It makes a user, a group and a key for CI.

access/main.tf
terraform {
  required_version = ">= 1.11"
  required_providers {
    ignitify = { source = "ignitify-dev/ignitify" }
  }
}

provider "ignitify" {}

resource "ignitify_user" "ci" {
  name = "ci"
}

resource "ignitify_group" "deployers" {
  name = "deployers"
  permissions = [
    "tasks.read", "tasks.write",
    "deployments.read", "deployments.write",
    "secrets.read", "secrets.write",
    "hooks.read", "hooks.write",
    "runs.read",
    # Add "runs.write" when CI also starts runs (ignitify task:run).
    # Add "schedules.read" and "schedules.write" when CI also manages ignitify_schedule
    # (provider 0.5.0 or later).
  ]
  members = [ignitify_user.ci.name]
}

resource "ignitify_api_key" "ci" {
  user  = ignitify_user.ci.name
  label = "ci-pipeline"
}

output "ci_key" {
  value     = ignitify_api_key.ci.raw_key
  sensitive = true
}
terminal
$ IGNITIFY_API_KEY=<owner key> terraform apply

Read the key once with terraform output -raw ci_key and put it in the secret store of your CI system. Do not print it to a log.

2 · The app, with the user key

CI applies this with the user key from step 1.

app/main.tf
terraform {
  required_version = ">= 1.11"
  required_providers {
    ignitify = { source = "ignitify-dev/ignitify" }
  }
}

provider "ignitify" {}

variable "db_password" {
  type      = string
  sensitive = true
  ephemeral = true
}

resource "ignitify_secret" "db_password" {
  name             = "DB_PASSWORD"
  value_wo         = var.db_password
  value_wo_version = 1
}

resource "ignitify_task" "report" {
  slug = "report"
}

resource "ignitify_deployment" "report" {
  task       = ignitify_task.report.slug
  source_dir = "${path.module}/report"
}

resource "ignitify_hook" "report" {
  task = ignitify_task.report.slug
}

output "hook_url" {
  value = ignitify_hook.report.url
}
terminal
$ IGNITIFY_API_KEY=<user key> TF_VAR_db_password=<value> terraform apply

The apply makes the secret, the task, uploads and builds the code, then makes the hook. The report folder holds a task config with slug: report and a task that reads DB_PASSWORD. See Your first task.

Roll a code update

Change a file in the task folder, then plan and apply.

terminal
$ terraform plan  # ignitify_deployment.report will be updated in-place  ~ resource "ignitify_deployment" "report" {      ~ deployment_id = "<old id>" -> (known after apply)      ~ source_hash   = "<old hash>" -> "<new hash>"    }Plan: 0 to add, 1 to change, 0 to destroy.
  • The apply builds the new deployment first. It deletes the old one only when the new one is READY.
  • The hook keeps its URL and its secret.
  • A failed build keeps the old deployment live. Fix the code and apply again.
  • Runs that are still running on the old deployment end, and its run history is deleted. Apply when no run you need is in flight.

Rotate a secret

Terraform cannot see a write-only value, so a new value needs a new version number.

app/main.tf
resource "ignitify_secret" "db_password" {
  name             = "DB_PASSWORD"
  value_wo         = var.db_password
  value_wo_version = 2   # was 1
}
terminal
$ TF_VAR_db_password=<new value> terraform apply

A task reads the new value on its next run. A VM that is already running keeps the value it started with.

Rotate a user key or a hook

Replace the resource. The new one has a new value, and the old one stops working.

terminal
$ terraform apply -replace=ignitify_api_key.ci
terminal
$ terraform apply -replace=ignitify_hook.report

After a key replace, put the new key in your CI store. After a hook replace, give the new URL and secret to the sender.

Bring existing objects under Terraform

You made a task, a secret or a user with the CLI, and now you want Terraform to own it. Write the resource, then import it. The plan must show no change before you apply.

app/main.tf
resource "ignitify_task" "report" {
  slug = "report"
}

resource "ignitify_secret" "db_password" {
  name             = "DB_PASSWORD"
  value_wo         = var.db_password
}
terminal
$ terraform import ignitify_task.report report
terminal
$ terraform import ignitify_secret.db_password DB_PASSWORD
  • Import a task before its deployment and its hook.
  • After an import of a key or a hook, the raw key or secret is empty. Replace the resource to get a new one.
  • A secret with no version in your code is not sent. Set value_wo_version only when you want the first apply to write your value.

Each resource page lists its import id.

Use it from CI

A CI job needs the provider, the user key, the secret values and a state backend.

  1. Key. Store the user key in the secret store of your CI system. Expose it to the job as IGNITIFY_API_KEY.
  2. Values. Expose each secret value as TF_VAR_<name>, for an ephemeral variable.
  3. State. Use an encrypted backend with limited access. The state holds hook secrets and user keys.
  4. Steps. Run init, then plan -out, then apply of that plan file. Apply only from your main branch.
  5. Time. A deployment waits for the build, which can take minutes. Set timeouts on the deployment, and a job timeout above it.
ci.sh
terraform init -input=false
terraform plan -input=false -out=tfplan
terraform apply -input=false tfplan