--- name: sg-writing-stackguardian-workflow-terraform description: Use when writing, reviewing or debugging Terraform/OpenTofu HCL for StackGuardian workflows (stackguardian_workflow_git, stackguardian_workflow_from_template, workflow templates/revisions, CUSTOM Ansible/Helm/kubectl/CloudFormation steps) — vcs_config, terraform_config, connectors, secrets in env vars, runners and stackguardian_runner_group, schedules, VCS triggers, notifications, chaining, workflow outputs — or when a workflow applies but every run fails, or when migrating old `stackguardian_workflow` + `data = jsonencode(...)` code. Also covers looking up the org's connectors, secrets, runner groups and templates via the StackGuardian MCP server. --- # Writing StackGuardian workflow Terraform ## Overview Provider `StackGuardian/stackguardian` **1.12.3** (Terraform Plugin Framework). Every nested structure is an **attribute** (`x = {...}` / `x = [{...}]`), never a block. Facts were verified on 1.12.3; on a newer provider re-check rules 11 and 12 first (they are provider bugs). The provider validates almost nothing, so mistakes show up as: a 400 at apply, *"Provider produced inconsistent result after apply"* (the resource is tainted when this happens on create), or a workflow that is created and then fails every run. Everything below was checked against provider, api, core, run-controller and step-template source, and the traps marked **(live)** were reproduced against a real org. Groups: **sg-writing-stackguardian-workflow-group-terraform**. Guardrails/approvals on plans: **sg-writing-stackguardian-policy-terraform**. ## Setup ```terraform terraform { required_providers { stackguardian = { source = "registry.terraform.io/StackGuardian/stackguardian" # full address: the OpenTofu registry has no copy version = "~> 1.12.3" } } } provider "stackguardian" {} # STACKGUARDIAN_API_KEY, STACKGUARDIAN_ORG_NAME, STACKGUARDIAN_API_URI ``` `api_uri` has **no default** (live): set `https://api.app.stackguardian.io` (EU) or `https://api.us.stackguardian.io` (US). A key from the other region returns 401 (per the provider docs). ## Which resource | Want | Resource | |---|---| | Workflow whose IaC is in your git repo | `stackguardian_workflow_git` (`wf_type` TERRAFORM / OPENTOFU / CUSTOM) | | Workflow from a template revision **owned by your org** | `stackguardian_workflow_from_template` (templates of other orgs, incl. the marketplace, fail — rule 11) | | Reusable definition many workflows share | `stackguardian_workflow_template` + `stackguardian_workflow_template_revision` | | Ansible / Helm / kubectl / CloudFormation / Terragrunt | `wf_type = "CUSTOM"` + `wf_steps_config` (there is no ANSIBLE/HELM wf_type) | | Read a workflow's Terraform outputs | `data "stackguardian_workflow_outputs"` | `stackguardian_workflow`, `wfgrp`, `data = jsonencode({...})`, `GitHubComSync`, `is_active`, `cache_config`, `tfvars_file_path` and `enforced_policies` do not exist in this provider. ## Discover dependencies first (StackGuardian MCP) Connectors, secrets, runner groups, groups and other workflows must already exist, and wrong ids only fail at run time. **If the StackGuardian MCP server is connected, look every one up before writing HCL** and offer the user the real options (full mapping: `references/discovery.md`): | Dependency | MCP call | Value to use | |---|---|---| | cloud / VCS connector | `search_orchestrator_resources(resourceTypes=["INTEGRATION.CLOUD"])` / `["INTEGRATION.VCS"]` | `ResourceId` (`/integrations/`) → `integration_id` / `auth`; `ResourceType` minus `INTEGRATION.` → `kind` / `source_config_dest_kind`. Never the `ResourceName` | | secret | `search_orchestrator_resources(resourceTypes=["SECRET"])` | `/secrets/` → `"$${secret::}"`; missing ones must be created first (`sg-manage-secrets` skill) | | private runner group | `search_orchestrator_resources(resourceTypes=["RUNNER"])` | last segment of `/runnergroups/` → `runner_constraints.names` | | workflow group | `search_orchestrator_resources(resourceTypes=["WORKFLOW_GROUP"])` | `SubResourceId` minus `/wfgrps/` → `workflow_group_id` (`ResourceId` is only the leaf) | | other workflows, their outputs | `search_workflows`, `get_workflow_outputs` | `SubResourceId` `/wfgrps//wfs/` → chaining, `${workflow::…}`, import id | | own / step templates | `search_marketplace_templates`, `get_marketplace_template_by_id`, `get_subscription` | `//:`, input schema defaults, draft status | Rows must carry `OrgId = "/orgs/"`; use read tools only (don't create via MCP what Terraform should own); keep the data sources in the HCL as the plan-time check. Without MCP: data sources + ask the user — never invent ids. ## Core template ```terraform data "stackguardian_connector" "github" { id = "github_com" } # GITHUB_COM connector id is always github_com data "stackguardian_connector" "aws" { id = "aws-prod-oidc" } resource "stackguardian_workflow_git" "payments_api" { id = "payments-api" # REQUIRED slug; changing it replaces the workflow (new state!) resource_name = "Payments API" # display name; omit it and the server invents a random one workflow_group_id = stackguardian_workflow_group.prod.id # bare id, "payments/prod" when nested; never change in place wf_type = "TERRAFORM" # TERRAFORM | OPENTOFU | CUSTOM — immutable description = "Payments API infrastructure" # "" fails validate; omit instead vcs_config = { iac_vcs_config = { custom_source = { source_config_dest_kind = "GITHUB_COM" # must equal the auth connector's kind config = { repo = "https://github.com/acme/payments-infra" ref = "main" working_dir = "envs/prod" is_private = true auth = "/integrations/${data.stackguardian_connector.github.id}" } } } iac_input_data = { # becomes a .tfvars.json passed to plan schema_type = "RAW_JSON" data = jsonencode({ region = "eu-central-1", instance_count = 3 }) } } deployment_platform_config = [{ kind = "AWS_OIDC" # = the connector's kind config = { integration_id = "/integrations/${data.stackguardian_connector.aws.id}" } }] environment_variables = [ { kind = "PLAIN_TEXT", config = { var_name = "TF_LOG", text_value = "WARN" } }, { kind = "PLAIN_TEXT", config = { var_name = "DB_PASSWORD", text_value = "$${secret::payments-db-pass}" } }, # $$ = literal ${ ] terraform_config = { terraform_version = "1.5.7" # always set; Terraform <= 1.5.7, OpenTofu e.g. "1.11.x" managed_terraform_state = true # server default is FALSE; true requires NO backend block in the repo approval_pre_apply = true drift_check = true drift_cron = "0 2 * * ? *" # 6-field EventBridge cron, UTC } approvers = ["alice@acme.com", "bob@acme.com", "carol@acme.com"] # lowercase number_of_approvals_required = 2 # 0 = ALL approvers runner_constraints = { type = "shared" } tags = ["payments", "prod"] } ``` ## Rules that bite 1. **Path vs bare ids.** `integration_id`, `auth`: `/integrations/` (or `/secrets/`). `workflow_group_id`, `runner_constraints.names`, `wf_chaining.*`: bare ids. A bare connector id applies fine and every run fails with `Connector does not exist`. Kinds must match the connector: `kind = data.stackguardian_connector.aws.settings.kind` guarantees it. 2. **Secrets:** `PLAIN_TEXT` + `text_value = "$${secret::}"` (the dashboard's form; `VAULT_SECRET` + `secret_id = "/secrets/"` also works at run, although the 1.12.3 docs call `secret_id` "Not used"; `SECRET_VALUE` from older docs is a 400). The whole value must be the reference. Nothing checks the secret at apply — a missing one fails the run. The provider has no secret resource; create secrets out of band. 3. **Terraform > 1.5.7** is rejected on create and update (`Terraform 1.9.0 is above the highest managed version (1.5.7)`, live). OpenTofu has no cap. See `references/execution.md` for escape hatches. 4. **Write every value the server normalizes** or you get *inconsistent result* (live): `terraform_config = {}` (server stamps `1.5.7`), mixed-case approver emails (lowercased). Nested typos (`is_privte`, `terraform_verison`) pass `terraform validate` and are silently dropped (live) — copy attribute names from `references/schema.md`. 5. **Workflows start inactive** and activate on their first run — including a drift or schedule tick (billed). Outputs are unavailable until then. Creating a workflow never runs it. 6. **Schedules auto-apply.** `user_schedules` cannot carry run inputs in this provider, so each tick on a TERRAFORM/OPENTOFU workflow is a full `apply` unless `approval_pre_apply = true`. 7. **Approvals:** `approval_pre_apply` with `approvers = []` lets *anyone* approve; `0` with a list means *everyone*; a plan with no changes completes without asking; any rejection cancels the run. 8. **Destroying the resource does not destroy infrastructure.** It deletes the workflow, its runs, schedules and SG-managed state. Run a destroy run first (see `references/lifecycle.md`). 9. **Clearing values on `workflow_git`:** removing `tags`/`approvers` *keeps* them (`[]` clears); removing `description`, `environment_variables`, `mini_steps`, … clears them. **Never just delete `user_schedules`** — the schedules keep firing: set it to `[]` and apply (server deletes them, provider reports a one-time *inconsistent result*, live), then remove the attribute. 10. **CUSTOM** needs a non-empty `wf_steps_config` (else 201 then every run: `Workflow Steps not set`) and must not have `terraform_config`. 11. **Provider 1.12.3 bug:** `workflow_from_template` with a template owned by *another* org (every `/stackguardian/...` marketplace template) fails at apply with `Could not read template revision … 401 Unauthorized` (live). Use an own-org copy of the template (`examples/own_template.tf`) or `workflow_git` on the template's repo. 12. **Template upgrades take two applies** when the new revision is created in the same config: publish the revision first, then point `iac_template_id` at it (same apply → *inconsistent final plan*, live). ## Quick reference | Topic | Rule / value | |---|---| | Template id | `iac_template_id = "//