Webhook icon
Schedule icon
WorkingDirectory icon
Commands icon
Docker icon
Script icon
If icon
SlackIncomingWebhook icon
Create icon
Log icon

Scan Changed Terraform and Kubernetes Files Before They Are Applied

Terrascan scans changed Terraform and Kubernetes files from a CI webhook, then escalates every policy violation to Slack and a GitHub issue before apply.

Categories
Infrastructure

How it works

  1. ci_webhook (event-based Webhook, authenticated with the IAC_SCAN_WEBHOOK_KEY secret) receives the post-commit call from CI, and nightly_recheck (a Schedule shipped with disabled: true) offers the same scan as a nightly net once an owner switches it on. The repository and path arrive through the iac_repo and iac_path inputs, so one flow gates any module in the estate.
  2. scan_workspace (core.flow.WorkingDirectory) opens one shared directory for the whole gate. write_fixtures (scripts.shell.Commands running alpine:3.20 through a Docker taskRunner) writes main.tf with a public-read S3 bucket, a wildcard read bucket policy, and an SSH security group open to 0.0.0.0/0, plus deployment.yaml with a privileged, root, privilege-escalating container. Staging fixtures keeps the blueprint runnable without repository credentials, and the path input decides where the manifests land.
  3. scan_iac (scripts.shell.Commands with tenable/terrascan:latest as the Docker taskRunner) runs terrascan scan -i terraform -d <iac_path> --output json > terrascan-report.json || true and then the matching -i k8s scan into terrascan-report-k8s.json. Terrascan exits non-zero when it finds violations, so || true absorbs that code and the Python verdict, not the CLI, is what gates the pipeline.
  4. parse_violations (scripts.python.Script in python:3.12-slim) loads both reports, tolerates log lines wrapped around the JSON, merges every entry under results.violations, buckets severities, and collects distinct rule_id values ordered by severity. It prints a single ::{"outputs": ...}:: payload with verdict, violation_count, severities, policy_ids, severity_summary, policy_summary, and top_policies, and writes the same facts to terrascan-summary.json. A missing or unparseable report contributes an UNPARSEABLE finding and the TERRASCAN-REPORT-UNREADABLE policy id, so a broken scan can never read as clean.
  5. violation_gate (core.flow.If) branches on violation_count. Over zero, alert_slack (notifications.slack.SlackIncomingWebhook) posts the counts and the one-line policy summary, and open_issue (github.issues.Create) files a labeled issue carrying the top policy list, the summary report URI, and the execution id. Zero violations take record_pass, which logs the pass verdict that the outputs publish to the calling job, and the flow-level errors handler repeats any scanner or worker failure to Slack.

What you get

  • A pre-apply verdict for Terraform and Kubernetes changes returned as verdict, violation_count, policy_ids, and report_uri, so a pipeline can branch on it directly.
  • One merged view across both IaC types, with severities bucketed from CRITICAL to LOW and policy ids ordered worst first.
  • Escalation engineers see, a Slack message for the moment and a labeled GitHub issue for the fix, plus an unreadable report that blocks instead of quietly passing.

Who it's for

Platform and DevOps teams who run Terrascan in CI today but want the scan, the verdict, the alert, and the backlog ticket in one replayable execution, and security engineers who must prove a misconfiguration was caught before provisioning rather than after it is live.

Why orchestrate this with Kestra

A CI step can run Terrascan and fail a build, but it cannot keep the report, the alert, and the ticket in one place, retry a flaky scanner, or hand the same verdict to a nightly sweep without duplicating the pipeline. Kestra gives the scan a durable execution history, a shared working directory for the fixtures and both reports, a branch that escalates or stays quiet, and a failure handler that pings the ops channel instead of leaving a silently broken gate.

Prerequisites

  • Docker on the Kestra worker, since the fixtures, the scans, and the parser all run through a Docker taskRunner.
  • Network access to pull tenable/terrascan:latest, alpine:3.20, and python:3.12-slim.
  • A GitHub token with issue-create rights and a CI job that POSTs to the webhook after a commit changes IaC files.

Secrets

  • IAC_SCAN_WEBHOOK_KEY: Shared key CI sends to authenticate the webhook call.
  • SLACK_WEBHOOK_URL: Slack incoming webhook used for the violation alert and the failure escalation.
  • GITHUB_TOKEN: GitHub token with issue-create rights in the configured repository.

Quick start

  1. Store the three secrets, point github_repository at the project that should receive violation issues, and set iac_repo and iac_path to the module CI watches, or keep the defaults for a first dry run.
  2. Run the flow manually. The fixtures stage, both scans write their reports, and violation_gate escalates because the staged manifests are deliberately misconfigured.
  3. Check the Slack message, the labeled issue, and the report_uri output, then switch ci_webhook on in CI so every IaC commit is gated before apply.

How to extend

  • Swap the fixture stage for a real checkout by adding a git.Clone task in scan_workspace ahead of scan_iac, keeping the same path input and reports.
  • Add severity tiers by branching on outputs.parse_violations.vars.severities, so warnings open a ticket while only CRITICAL and HIGH block the apply.
  • Fan out per policy with core.flow.ForEach over policy_ids, and ship terrascan-summary.json to durable storage with aws.s3.Upload.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.