Platform · Enterprise

Platform Operator Tooling: Terraform Plan to Markdown and Private AKS Access via Bastion

Two small tools that remove daily friction: a Go utility that renders Terraform plan JSON as reviewable Markdown, and a script that tunnels kubectl to private AKS clusters through Azure Bastion.

  • Go
  • Bash
  • Python
  • Terraform
  • Azure Bastion
  • AKS
  • kubelogin
  • GitHub Actions

// Why this matters

If you want terraform plan json to markdown in your pull requests, you are probably already feeling the review problem: raw plan output is thousands of lines, reviewers stop reading, and the one resource marked "must be replaced" goes unnoticed. A short, structured summary changes what review is able to catch.

The second tool solves a different but equally daily irritation. Private Kubernetes clusters are the right default, but they turn "run a kubectl command" into a multi-step ritual of VPN, jump host, kubeconfig editing and role activation. Done by hand, people take shortcuts: shared kubeconfigs, public endpoints "just for now", tokens in chat.

Both are small enough to build in a day and pay back every day after. They are also a good example of platform work that is not a product but changes how the team behaves.

// The problem

Plan review fails in predictable ways. A reviewer opens a pipeline log and scrolls through attribute-level diffs. Sensitive values are masked, computed values are noisy, and forced replacements are a single line in a very long page. There is no way to ask "how many resources are being destroyed?" without reading everything.

Cluster access fails in a different way. The API server is private, the bastion lives in a hub network, and the operator needs the right role on the bastion, on the cluster and on the namespace. Credentials produced by az aks get-credentials default to a device-code login that prompts constantly. People leave tunnels running in forgotten terminals and later run commands against the wrong cluster.

We want both to be boring: a single command, a deterministic result, and cleanup that does not depend on the operator's memory.

// The solution

For plan rendering, the tool reads the JSON produced by terraform show -json plan.out. Its resource_changes array has, for each resource, an address, a type and a change.actions list such as ["create"], ["update"], ["delete","create"] (a replacement) or ["no-op"]. The renderer filters no-ops, buckets by action, sorts replacements and deletes first, and writes a Markdown summary table followed by collapsible sections per action. It writes to stdout so the CI step decides where it goes.

We chose Go for the renderer because a single static binary is trivial to pull into any pipeline image and the Terraform plan JSON is easy to model with structs. We rejected parsing the human text plan (format changes between versions) and third-party SaaS plan viewers (data leaves the boundary, and some cost money). Attribute-level diffs are included only for updates, with sensitive values already masked by Terraform.

For cluster access, the script performs the same sequence an operator would, but safely. It discovers the private clusters from the subscriptions the operator can see, asks which one, switches to the hub subscription that owns the Bastion, opens az network bastion tunnel to the cluster's API host on a fixed local port, fetches credentials, rewrites the kubeconfig server to the local port, and traps exit to stop the tunnel and restore the backup. Authentication uses kubelogin in azurecli mode so there is no device-code prompt, and authorisation is Azure RBAC for Kubernetes, so access is a role assignment that can be time-boxed through eligible roles.

Both tools slot into the wider platform: the Markdown output feeds the review gates described in From Form to Running Cluster: Self-Service Team Onboarding and One Reusable Plan/Apply Workflow for Cost-Optimised Autoscaling, and the tunnel is how operators perform the checks in Demolition in Dependency Order: Safe, Re-runnable Offboarding.

How it flows flow1. terraform plan → 2. terraform show -json → 3. Render → 4. Comment → 5. Gate: human review → 6. Operator sign-in → 7. Tunnel → 8. Kubeconfig → 9. Cleanup01terraform plan02terraform show -json03Render04Comment05 · GATEGate: human review06Operator sign-in07Tunnel08Kubeconfig09Cleanup
How it flows. End-to-end flow. Dashed pink nodes are gates: points where a human or a check must pass before the next stage runs.
  1. terraform plansaved to a binary plan file
  2. terraform show -jsonstructured plan document
  3. RenderGo tool groups by action and type
  4. CommentMarkdown posted to the pull request
  5. Gate: human review gatereviewer reads the summary, not 4000 lines
  6. Operator sign-inaz login with an eligible role activated
  7. TunnelBastion forwards a local port to the private API
  8. Kubeconfigserver rewritten to the local port, backup kept
  9. CleanupCtrl+C restores the old kubeconfig
OperatorTunnel scriptAzure CLIBastionAKS APIrun, choose clusterswitch to hub subscriptionopen tunnel to API host :443get-credentials, rewrite server to localhostkubectl via localhost portprivate forward
Reaching a private cluster. kubectl talks to a local port; Bastion carries it to the API server.

// Before you start

  • Go 1.22+ to build the renderer, or a prebuilt binary in your runner image.
  • Terraform 1.5+ with a pipeline that can save a plan file.
  • Azure CLI 2.50+ with the bastion and ssh extensions, kubectl and kubelogin.
  • A Bastion host (Standard SKU or higher, with native client support and tunnelling enabled) in the hub network.
  • Azure RBAC for Kubernetes enabled on the clusters, and eligible roles for the operator.
  • Permission to comment on pull requests from CI (a GitHub token with pull-requests: write).

// Step-by-step implementation

  1. 01 Produce a machine-readable plan in CI

    Everything starts with saving the plan file and converting it to JSON. Use -out so what you review is what you apply, and -no-color so any text logs stay clean. The JSON is the contract with the renderer.

    Keep the plan file as a pipeline artifact; the apply job should consume that exact file.

  2. 02 Model the plan JSON in Go

    You only need a few fields, so define small structs and ignore the rest. Decoding into map[string]any for the before/after values lets you diff attributes without modelling every provider schema.

    Classify actions: a change with delete and create is a replacement, which deserves its own bucket because it is the dangerous one.

  3. 03 Render the Markdown summary

    Lead with counts, then list the risky buckets expanded and the harmless ones collapsed using <details>. GitHub renders this natively. For updates, list changed attribute names only; values may be large or sensitive.

    GitHub comments have a size limit (about 65,000 characters), so truncate long lists with a clear "and N more" line.

  4. 04 Post the summary to the pull request

    In CI, run the renderer and upsert a single comment identified by a hidden marker, so each push updates the comment rather than adding a new one. A sticky comment keeps the thread readable.

    Grant only pull-requests: write to the job; the plan job does not need repository write access.

  5. 05 Write the Bastion tunnel script for private AKS

    The script must be safe to interrupt. Back up the kubeconfig before touching it, start the tunnel in the background, and register a trap that stops the tunnel and restores the backup on any exit. Use a fixed local port so the rewritten kubeconfig is predictable; the trade-off is one cluster at a time, which is a feature when you are working on production.

    The Bastion lives in the hub subscription, the cluster in a workload subscription, so the script must switch subscriptions between steps.

  6. 06 Activate roles, then verify and use it

    If your Azure roles are eligible (PIM) rather than permanent, activate them first and wait a minute; a stale session looks exactly like missing permissions. Run the script, leave that terminal open, and use a second terminal for kubectl.

    In production clusters, grant application teams read-only roles; the tunnel changes the path, not the authority.

// How to verify it works

  • jq on plan.json shows resource_changes, and the rendered Markdown summary counts match the counts from terraform plan output.
  • A plan that forces replacement lists the resource under the open "replace" section.
  • The pull request shows exactly one plan comment that updates in place after a second push.
  • lsof -nP -iTCP:9999 -sTCP:LISTEN shows the tunnel while the script runs and nothing after Ctrl+C.
  • kubectl config current-context returns your original context after the script exits, and kubectl get ns works while it runs without a device-code prompt.

// Design decisions and trade-offs

  • Render the plan from the machine-readable JSON, never by parsing the human plan text.
  • Summarise by action and resource type first; details fold away so large plans stay reviewable.
  • Private cluster access goes through a Bastion tunnel and Azure RBAC, so no API server is ever exposed and no kubeconfig certificate is shared.
  • The tunnel script restores the previous kubeconfig on exit; operators never end up pointed at the wrong cluster silently.
  • Tools are small, single-binary or single-file, and live in the repo they serve.

// Pitfalls and lessons learned

  • Plan JSON contains values marked sensitive as unknown/redacted, but before/after can still hold surprising data. Do not print attribute values by default.
  • A plan with -target or -refresh=false is not the full picture. Render the same plan you intend to apply and say so in the comment.
  • Port 9999 already in use (a forgotten tunnel) produces "Tunnel failed to start"; one tunnel at a time is by design, so find and stop the old one.
  • Certificate name mismatch when talking through localhost: set --tls-server-name rather than disabling verification.
  • Without kubelogin convert-kubeconfig -l azurecli, every command asks for a device code and operators will hit the wrong prompt and give up.
  • Bastion needs the Standard SKU with native client support enabled; Basic will not tunnel and the error message does not say why.
  • Never paste kubeconfigs or tokens into tickets or chat; the script keeps them local on purpose.

// Frequently asked questions

How do I convert terraform plan json to markdown?

Run terraform plan -out, then terraform show -json on the plan file, and render resource_changes grouped by change.actions; the Go example above does exactly that.

Why not parse the text output of terraform plan?

The human format is not a stable interface and changes between versions; the JSON representation is documented and versioned.

How do I access a private AKS cluster without a VPN?

Use Azure Bastion native-client tunnelling to forward a local port to the private API server and point kubeconfig at that port.

Does kubelogin work with Azure RBAC for Kubernetes?

Yes. Use the azurecli login mode so kubectl reuses your az session and authorisation comes from role assignments.

Can I run the tunnel for two clusters at once?

You can by using different local ports and contexts, but for production access a single fixed port is a deliberate guard against running commands on the wrong cluster.

// The outcome

Pull requests carry a readable summary of what a Terraform change will do, and operators can reach any private cluster they are entitled to with one command, with access governed by role assignments rather than shared credentials.

// Where to take it next

// Keep reading