<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Posts on 404 Code Not Found</title>
    <link>https://www.404-code-not-found.com/posts/</link>
    <description>Recent content in Posts on 404 Code Not Found</description>
    <generator>Hugo</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 10 Sep 2026 09:00:00 -0600</lastBuildDate>
    <atom:link href="https://www.404-code-not-found.com/posts/index.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>Stop Minting Static Credentials</title>
      <link>https://www.404-code-not-found.com/posts/stop-minting-static-credentials/</link>
      <pubDate>Thu, 10 Sep 2026 09:00:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/stop-minting-static-credentials/</guid>
      <category>terraform</category>
      <category>vault</category>
      <category>security</category>
      <category>oidc</category>
      <category>aws</category>
      <category>azure</category>
      <category>gcp</category>
      <category>postgresql</category>
      <description>&lt;p&gt;&lt;img src=&#34;https://www.404-code-not-found.com/posts/stop-minting-static-credentials/header.png&#34; alt=&#34;Stop Minting Static Credentials&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;Every static credential starts out reasonable. A pipeline needs to SSH into a&#xA;server, so someone generates a key pair and stores the private half somewhere&#xA;the pipeline can read it. A Terraform workspace needs to talk to AWS, so someone&#xA;creates an access key and pastes it into a workspace variable. An application&#xA;needs a database, so someone creates an application user, picks a password, and&#xA;puts it in a config file.&lt;/p&gt;&#xA;&lt;p&gt;Each of those is a credential that nobody asked to expire. It works on day one,&#xA;and it keeps working long after the person who created it has moved on and&#xA;nobody remembers which systems would break if it were rotated. So it doesn&amp;rsquo;t&#xA;get rotated.&lt;/p&gt;&#xA;&lt;p&gt;The fix has the same shape every time. Stop storing a secret that proves who you&#xA;are, and start trusting an identity system to issue a short-lived one on demand.&#xA;This post walks through three of those substitutions, each done for real in my&#xA;lab environment, and the thing that bit on the way in for each one:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;A Vault SSH certificate authority instead of distributing key pairs.&lt;/li&gt;&#xA;&lt;li&gt;OIDC workload identity instead of long-lived cloud access keys, for AWS,&#xA;Azure, and Google Cloud.&lt;/li&gt;&#xA;&lt;li&gt;Vault-managed database credentials instead of a shared application user.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;The Azure and Google Cloud trust configurations are in&#xA;&lt;a href=&#34;https://github.com/404-code-not-found-com/terraform-dynamic-credentials&#34; target=&#34;_blank&#34;&gt;terraform-dynamic-credentials&lt;/a&gt;,&#xA;verified against both HCP Terraform and a self-hosted Terraform Enterprise.&#xA;AWS dynamic credentials worked in my lab too, but an account admin created&#xA;the IAM side, and I don&amp;rsquo;t have admin access to an AWS account to show every&#xA;step here. The AWS module in the repo is written from the HashiCorp docs and&#xA;passes &lt;code&gt;terraform validate&lt;/code&gt;, but I haven&amp;rsquo;t applied it myself. Code excerpts&#xA;elsewhere in the post are trimmed, with environment-specific names generalized.&lt;/p&gt;&#xA;&lt;h2 id=&#34;tldr&#34;&gt;TL;DR&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;SSH:&lt;/strong&gt; Vault&amp;rsquo;s SSH secrets engine signs a certificate per job, good for&#xA;thirty minutes. Hosts trust the CA&amp;rsquo;s public key, baked into the image. There&#xA;is no private key to distribute because there is no long-lived private key&#xA;that grants access.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Cloud:&lt;/strong&gt; HCP Terraform and Terraform Enterprise sign a JWT for every plan&#xA;and apply. AWS, Azure, and Google Cloud trade it for credentials that expire&#xA;with the run. The workspace stores identifiers, not secrets.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Databases:&lt;/strong&gt; a Vault static role owns the application user&amp;rsquo;s password and&#xA;rotates it on a schedule. A dynamic role goes further and creates a new user&#xA;per lease. Both work, and they fail in different ways.&lt;/li&gt;&#xA;&lt;li&gt;The gotchas are almost never in the happy path. They&amp;rsquo;re in the second phase,&#xA;the second week, or the second time you need to change something.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;problem-1-the-ssh-key-pair-everyone-has-a-copy-of&#34;&gt;Problem 1: The SSH Key Pair Everyone Has a Copy Of&lt;/h2&gt;&#xA;&lt;p&gt;My lab&amp;rsquo;s CI pipeline needed to configure a lab server over SSH. The original&#xA;setup was the common one: a private key stored in Vault&amp;rsquo;s key/value engine, read&#xA;by the CI job, written to disk, and used directly.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;secrets:&#xA;  SSH_PRIVATE_KEY:&#xA;    vault:&#xA;      engine:&#xA;        name: kv-v2&#xA;        path: secrets&#xA;      path: lab/lab-key&#xA;      field: private_key&#xA;    file: true&#xA;    token: $VAULT_ID_TOKEN&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Storing the key in Vault beats storing it in the repo, but it doesn&amp;rsquo;t change&#xA;what the key is. It&amp;rsquo;s one private key, valid indefinitely, copied onto every&#xA;runner that executes the job, and rotating it means touching every host that&#xA;trusts it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-sign-a-certificate-per-job&#34;&gt;The Fix: Sign a Certificate per Job&lt;/h3&gt;&#xA;&lt;p&gt;OpenSSH has supported certificates for years. Instead of putting a user&amp;rsquo;s public&#xA;key in &lt;code&gt;authorized_keys&lt;/code&gt; on every host, you put one CA public key in&#xA;&lt;code&gt;sshd_config&lt;/code&gt;, and any user certificate signed by that CA is accepted until it&#xA;expires. Vault&amp;rsquo;s SSH secrets engine is that CA.&lt;/p&gt;&#xA;&lt;p&gt;The Vault side is a mount, a CA, and a role that says what a certificate may&#xA;contain:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;vault_mount&amp;#34; &amp;#34;aap_ssh&amp;#34; {&#xA;  path = &amp;#34;aap-ssh&amp;#34;&#xA;  type = &amp;#34;ssh&amp;#34;&#xA;}&#xA;&#xA;# generate_signing_key keeps the CA private key inside Vault. It is never&#xA;# returned by the API, so it can&amp;#39;t land in Terraform state.&#xA;resource &amp;#34;vault_ssh_secret_backend_ca&amp;#34; &amp;#34;aap_ssh&amp;#34; {&#xA;  backend              = vault_mount.aap_ssh.path&#xA;  generate_signing_key = true&#xA;  key_type             = &amp;#34;ed25519&amp;#34;&#xA;}&#xA;&#xA;resource &amp;#34;vault_ssh_secret_backend_role&amp;#34; &amp;#34;aap_role&amp;#34; {&#xA;  name    = &amp;#34;aap-role&amp;#34;&#xA;  backend = vault_mount.aap_ssh.path&#xA;&#xA;  key_type                = &amp;#34;ca&amp;#34;&#xA;  allow_user_certificates = true&#xA;&#xA;  allowed_users = &amp;#34;ansible&amp;#34;&#xA;  default_user  = &amp;#34;ansible&amp;#34;&#xA;&#xA;  allowed_extensions = &amp;#34;permit-pty,permit-port-forwarding&amp;#34;&#xA;  default_extensions = {&#xA;    &amp;#34;permit-pty&amp;#34; = &amp;#34;&amp;#34;&#xA;  }&#xA;&#xA;  ttl                 = &amp;#34;1800&amp;#34; # 30m&#xA;  max_ttl             = &amp;#34;7200&amp;#34; # 2h&#xA;  not_before_duration = &amp;#34;30&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The host side lives in the image pipeline. CI reads the CA&amp;rsquo;s public key from&#xA;Vault, and Packer drops it into the image along with one line of &lt;code&gt;sshd_config&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;CA_DEST=/etc/ssh/vault-ssh-ca.pub&#xA;cp /tmp/vault-ssh-ca.pub &amp;#34;$CA_DEST&amp;#34;&#xA;chmod 0644 &amp;#34;$CA_DEST&amp;#34;&#xA;&#xA;if grep -q &amp;#39;^TrustedUserCAKeys&amp;#39; /etc/ssh/sshd_config; then&#xA;  sed -i &amp;#34;s|^TrustedUserCAKeys .*|TrustedUserCAKeys $CA_DEST|&amp;#34; /etc/ssh/sshd_config&#xA;else&#xA;  echo &amp;#34;TrustedUserCAKeys $CA_DEST&amp;#34; &amp;gt;&amp;gt; /etc/ssh/sshd_config&#xA;fi&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;On the client side, the lab server is now configured by Ansible Automation&#xA;Platform rather than by the CI job. AAP has a &lt;code&gt;HashiCorp Vault Signed SSH&lt;/code&gt;&#xA;credential type, and linking it to a Machine credential makes AAP request a&#xA;freshly signed certificate every time a job runs:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;- name: Ensure signed-SSH input source on Machine credential&#xA;  ansible.controller.credential_input_source:&#xA;    input_field_name: ssh_public_key_data&#xA;    target_credential: &amp;#34;{{ machine_credential_name }}&amp;#34;&#xA;    source_credential: &amp;#34;{{ signed_ssh_lookup_name }}&amp;#34;&#xA;    metadata:&#xA;      secret_path: aap-ssh&#xA;      role: aap-role&#xA;      auth_path: approle&#xA;      valid_principals: ansible&#xA;      public_key: &amp;#34;{{ ssh_public_key }}&amp;#34;&#xA;    state: present&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The end state: the lab&amp;rsquo;s instances launch with no EC2 key pair at all, port 22&#xA;only accepts connections from AAP&amp;rsquo;s egress range, and the static key is gone&#xA;from the pipeline. The key pair AAP signs still exists, but on&#xA;its own it opens nothing. A certificate is what gets you in, and each one is&#xA;dead within two hours at most.&lt;/p&gt;&#xA;&lt;h3 id=&#34;gotcha-the-ca-is-not-a-normal-resource&#34;&gt;Gotcha: The CA Is Not a Normal Resource&lt;/h3&gt;&#xA;&lt;p&gt;I originally built the CA by hand, as RSA 4096, and brought it under Terraform&#xA;later. That&amp;rsquo;s where the first surprise was. Every argument on&#xA;&lt;code&gt;vault_ssh_secret_backend_ca&lt;/code&gt; forces replacement, because the provider&#xA;implements create, read, and delete but no update. And the provider&amp;rsquo;s read only&#xA;returns &lt;code&gt;public_key&lt;/code&gt; and &lt;code&gt;backend&lt;/code&gt;, so after an &lt;code&gt;import&lt;/code&gt; the other arguments are&#xA;null in state. A configuration that matched the live CA exactly still planned a&#xA;replacement.&lt;/p&gt;&#xA;&lt;p&gt;There was no way to adopt the existing CA without regenerating it. So instead of&#xA;importing it as RSA and rotating to ed25519 later, which would have regenerated&#xA;it twice, I changed the key type in the same apply and regenerated it once.&lt;/p&gt;&#xA;&lt;p&gt;Any change to the CA resource produces a new CA, and every host trusting the old&#xA;public key stops accepting certificates. In a setup where the key is baked into&#xA;images, rotation isn&amp;rsquo;t a Vault operation. It&amp;rsquo;s&#xA;a Vault change followed by rebuilding every image. &lt;code&gt;generate_signing_key = true&lt;/code&gt;&#xA;makes this sharper: the private key never leaves Vault, which is exactly why it&#xA;isn&amp;rsquo;t in state, and also why losing Vault means a new CA and a full image&#xA;rebuild. I think that&amp;rsquo;s the right trade, but it should be a decision, not a&#xA;discovery.&lt;/p&gt;&#xA;&lt;h3 id=&#34;gotcha-write-the-ttls-in-seconds&#34;&gt;Gotcha: Write the TTLs in Seconds&lt;/h3&gt;&#xA;&lt;p&gt;The first version of the role said &lt;code&gt;ttl = &amp;quot;30m&amp;quot;&lt;/code&gt;. It applied cleanly, and then&#xA;every plan after it showed a change. The API returns TTLs as integers in string&#xA;form, the provider doesn&amp;rsquo;t normalize them, and &lt;code&gt;&amp;quot;30m&amp;quot;&lt;/code&gt; is not equal to &lt;code&gt;&amp;quot;1800&amp;quot;&lt;/code&gt;.&#xA;Writing seconds made the plan idempotent.&lt;/p&gt;&#xA;&lt;h3 id=&#34;gotcha-the-public-key-has-a-space-in-it&#34;&gt;Gotcha: The Public Key Has a Space in It&lt;/h3&gt;&#xA;&lt;p&gt;The first image pipeline passed the CA public key to Packer&amp;rsquo;s shell provisioner&#xA;as an environment variable, with this &lt;code&gt;execute_command&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;execute_command  = &amp;#34;sudo sh -c &amp;#39;{{ .Vars }} {{ .Path }}&amp;#39;&amp;#34;&#xA;environment_vars = [&amp;#34;VAULT_SSH_CA_PUBKEY=${var.vault_ssh_ca_pubkey}&amp;#34;]&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;An SSH public key is &lt;code&gt;&amp;lt;type&amp;gt; &amp;lt;key&amp;gt;&lt;/code&gt;, with a space in the middle, and &lt;code&gt;{{ .Vars }}&lt;/code&gt;&#xA;expands inside the single-quoted &lt;code&gt;sh -c&lt;/code&gt; string. The quoting broke.&lt;/p&gt;&#xA;&lt;p&gt;The fix was to never let the key pass through a shell at all. GitLab CI can&#xA;write a Vault secret to a temporary file instead of an environment variable:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;VAULT_SSH_CA_PUBKEY:&#xA;  vault:&#xA;    engine:&#xA;      name: generic&#xA;      path: aap-ssh&#xA;    path: config/ca&#xA;    field: public_key&#xA;  file: true&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Packer then uploads that file with a &lt;code&gt;file&lt;/code&gt; provisioner, and the script reads it&#xA;from disk. The key never goes through an environment variable, a command-line&#xA;argument, or a shell expansion.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-2-cloud-keys-in-workspace-variables&#34;&gt;Problem 2: Cloud Keys in Workspace Variables&lt;/h2&gt;&#xA;&lt;p&gt;The classic Terraform setup for AWS is an IAM user with an access key, pasted&#xA;into the workspace as &lt;code&gt;AWS_ACCESS_KEY_ID&lt;/code&gt; and &lt;code&gt;AWS_SECRET_ACCESS_KEY&lt;/code&gt;. It works,&#xA;it never expires, and it&amp;rsquo;s usually more privileged than anything else in the&#xA;account.&lt;/p&gt;&#xA;&lt;p&gt;My lab had already moved past that, or so I thought. The workspace&#xA;authenticated to Vault with workload identity, then read AWS credentials from&#xA;Vault&amp;rsquo;s AWS secrets engine, which created a fresh IAM user for each run:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;data &amp;#34;vault_aws_access_credentials&amp;#34; &amp;#34;creds&amp;#34; {&#xA;  backend = &amp;#34;aws&amp;#34;&#xA;  role    = &amp;#34;packer&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;On paper, those are dynamic credentials. In practice, the first AWS call failed&#xA;about 23 seconds after the credentials were minted, with&#xA;&lt;code&gt;403 InvalidClientTokenId&lt;/code&gt; from STS. Adding &lt;code&gt;skip_credentials_validation&lt;/code&gt; moved&#xA;the same 403 to &lt;code&gt;iam:ListRoles&lt;/code&gt;. Adding &lt;code&gt;skip_requesting_account_id&lt;/code&gt; and a&#xA;30-second &lt;code&gt;time_sleep&lt;/code&gt; moved it again, this time to a &lt;code&gt;401 AuthFailure&lt;/code&gt; from the&#xA;first EC2 call, 56 seconds after minting. The error kept moving later instead of&#xA;going away. A brand new IAM user takes a while to be recognized everywhere in&#xA;AWS, and there was no sleep long enough to fix that reliably.&lt;/p&gt;&#xA;&lt;p&gt;The HashiCorp docs say not to do exactly this: &amp;ldquo;data sources that&#xA;use secrets engines to generate dynamic secrets must not be used with Vault&#xA;dynamic credentials.&amp;rdquo; The documented route through Vault is Vault-backed dynamic&#xA;credentials, which even has a &lt;code&gt;TFC_VAULT_BACKED_AWS_SLEEP_SECONDS&lt;/code&gt; setting &amp;ldquo;to&#xA;mitigate eventual consistency issues in AWS when using the &lt;code&gt;iam_user&lt;/code&gt; auth&#xA;type.&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;I went the other way and dropped Vault out of the AWS path entirely. With the&#xA;platform&amp;rsquo;s native dynamic credentials, the same apply created a key pair, a&#xA;security group, an EC2 instance, and an Elastic IP with no delay at all.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-let-the-cloud-trust-the-platform&#34;&gt;The Fix: Let the Cloud Trust the Platform&lt;/h3&gt;&#xA;&lt;p&gt;HCP Terraform and Terraform Enterprise act as an OIDC identity provider. For&#xA;every plan and every apply, the platform signs a JWT describing the run. The&#xA;claim that matters is &lt;code&gt;sub&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;organization:my-org:project:my-project:workspace:my-workspace:run_phase:apply&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You configure the cloud to trust tokens from the platform&amp;rsquo;s issuer URL, and to&#xA;accept only tokens whose claims match your workspace. At run time, the provider&#xA;trades the token for short-lived cloud credentials. The workspace holds a role&#xA;ARN or a client ID, which are identifiers rather than secrets.&lt;/p&gt;&#xA;&lt;p&gt;The docs are blunt about what happens if you skip the matching: &amp;ldquo;If you don&amp;rsquo;t&#xA;match against at least the organization name, any organization or workspace on&#xA;HCP Terraform will be able to access your cloud resources!&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;Something still needs admin rights to create the trust in the first place. The&#xA;difference is that it happens once, from a person&amp;rsquo;s own login, rather than being&#xA;stored in every workspace.&lt;/p&gt;&#xA;&lt;h3 id=&#34;aws&#34;&gt;AWS&lt;/h3&gt;&#xA;&lt;p&gt;The trust side is an IAM OIDC provider for the platform&amp;rsquo;s hostname and a role&#xA;whose trust policy checks the audience and the subject:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;aws_iam_role&amp;#34; &amp;#34;this&amp;#34; {&#xA;  name = var.name&#xA;&#xA;  assume_role_policy = jsonencode({&#xA;    Version = &amp;#34;2012-10-17&amp;#34;&#xA;    Statement = [{&#xA;      Effect    = &amp;#34;Allow&amp;#34;&#xA;      Principal = { Federated = local.oidc_provider_arn }&#xA;      Action    = &amp;#34;sts:AssumeRoleWithWebIdentity&amp;#34;&#xA;      Condition = {&#xA;        StringEquals = {&#xA;          &amp;#34;${var.hostname}:aud&amp;#34; = &amp;#34;aws.workload.identity&amp;#34;&#xA;        }&#xA;        StringLike = {&#xA;          &amp;#34;${var.hostname}:sub&amp;#34; = &amp;#34;organization:${var.organization}:project:${var.project}:workspace:${var.workspace}:run_phase:*&amp;#34;&#xA;        }&#xA;      }&#xA;    }]&#xA;  })&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note &lt;code&gt;StringLike&lt;/code&gt; on the subject. The &lt;code&gt;*&lt;/code&gt; after &lt;code&gt;run_phase:&lt;/code&gt; is only a wildcard&#xA;under &lt;code&gt;StringLike&lt;/code&gt;, and the docs call this out explicitly for when plan and&#xA;apply share a role. The workspace then needs two environment variables,&#xA;&lt;code&gt;TFC_AWS_PROVIDER_AUTH=true&lt;/code&gt; and &lt;code&gt;TFC_AWS_RUN_ROLE_ARN&lt;/code&gt;, and the provider block&#xA;should contain nothing but &lt;code&gt;region&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;In my lab, I specified the role and an account admin created it, so I can&amp;rsquo;t&#xA;publish the live trust policy as tested. The module in the repo is written from&#xA;the docs and passes &lt;code&gt;terraform validate&lt;/code&gt;, but I haven&amp;rsquo;t applied it myself.&#xA;Azure and Google Cloud I built and ran end to end.&lt;/p&gt;&#xA;&lt;h3 id=&#34;azure&#34;&gt;Azure&lt;/h3&gt;&#xA;&lt;p&gt;Azure&amp;rsquo;s version is an app registration, its service principal, a role&#xA;assignment, and federated identity credentials that tell Entra ID which tokens&#xA;to accept:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;azuread_application_federated_identity_credential&amp;#34; &amp;#34;this&amp;#34; {&#xA;  for_each = toset([&amp;#34;plan&amp;#34;, &amp;#34;apply&amp;#34;])&#xA;&#xA;  application_id = azuread_application.this.id&#xA;  audiences      = [&amp;#34;api://AzureADTokenExchange&amp;#34;]&#xA;  display_name   = &amp;#34;${var.name}-${each.key}&amp;#34;&#xA;  issuer         = &amp;#34;https://${var.hostname}&amp;#34;&#xA;  subject        = &amp;#34;organization:${var.organization}:project:${var.project}:workspace:${var.workspace}:run_phase:${each.key}&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The workspace gets &lt;code&gt;TFC_AZURE_PROVIDER_AUTH=true&lt;/code&gt;, &lt;code&gt;TFC_AZURE_RUN_CLIENT_ID&lt;/code&gt;,&#xA;&lt;code&gt;ARM_SUBSCRIPTION_ID&lt;/code&gt;, and &lt;code&gt;ARM_TENANT_ID&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The gotcha is the &lt;code&gt;for_each&lt;/code&gt;. A federated credential&amp;rsquo;s subject is an exact&#xA;string match, no wildcards, and the token&amp;rsquo;s subject ends in the run phase. So&#xA;every workspace needs two credentials. I deleted the &lt;code&gt;apply&lt;/code&gt; one to see what&#xA;happens: the plan ran fine, because data sources authenticate during plan, and&#xA;then the apply failed:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;AADSTS700213: No matching federated identity record found for presented&#xA;assertion subject &amp;#39;organization:my-org:project:dynamic-credentials-demo:workspace:dynamic-credentials-demo:run_phase:apply&amp;#39;.&#xA;Check your federated identity credential Subject, Audience and Issuer against&#xA;the presented assertion.&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That&amp;rsquo;s a nasty failure mode, because a green plan is what gets approved. Combine it with Entra ID&amp;rsquo;s limit of 20 federated credentials per app&#xA;registration, and one app registration can serve at most ten workspaces.&lt;/p&gt;&#xA;&lt;p&gt;Microsoft has a preview feature, flexible federated identity credentials, that&#xA;allows wildcard matching on the subject. I tested it both ways. On HCP&#xA;Terraform, a single credential matching&#xA;&lt;code&gt;claims[&#39;sub&#39;] matches &#39;...:run_phase:*&#39;&lt;/code&gt; covered both plan and apply. On&#xA;Terraform Enterprise, Entra ID refused to create it:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;Expression is not supported for applications in this cloud &amp;#39;Public&amp;#39; using&#xA;issuer &amp;#39;https://&amp;lt;tfe-hostname&amp;gt;&amp;#39;.&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Microsoft&amp;rsquo;s docs list only &lt;code&gt;https://app.terraform.io&lt;/code&gt; and its EU equivalent as&#xA;supported Terraform issuers. If you run Terraform Enterprise, two credentials&#xA;per workspace is the design.&lt;/p&gt;&#xA;&lt;h3 id=&#34;google-cloud&#34;&gt;Google Cloud&lt;/h3&gt;&#xA;&lt;p&gt;Google Cloud uses a workload identity pool, an OIDC provider in that pool, and a&#xA;service account the workspace impersonates:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;google_iam_workload_identity_pool_provider&amp;#34; &amp;#34;this&amp;#34; {&#xA;  project                            = var.project_id&#xA;  workload_identity_pool_id          = google_iam_workload_identity_pool.this.workload_identity_pool_id&#xA;  workload_identity_pool_provider_id = var.pool_provider_id&#xA;&#xA;  attribute_mapping = {&#xA;    &amp;#34;google.subject&amp;#34;                        = &amp;#34;assertion.sub&amp;#34;&#xA;    &amp;#34;attribute.terraform_organization_name&amp;#34; = &amp;#34;assertion.terraform_organization_name&amp;#34;&#xA;    &amp;#34;attribute.terraform_project_name&amp;#34;      = &amp;#34;assertion.terraform_project_name&amp;#34;&#xA;    &amp;#34;attribute.terraform_workspace_name&amp;#34;    = &amp;#34;assertion.terraform_workspace_name&amp;#34;&#xA;    &amp;#34;attribute.terraform_run_phase&amp;#34;         = &amp;#34;assertion.terraform_run_phase&amp;#34;&#xA;  }&#xA;&#xA;  attribute_condition = &amp;#34;assertion.sub.startsWith(\&amp;#34;organization:${var.organization}:project:${var.project}:workspace:${var.workspace}:\&amp;#34;)&amp;#34;&#xA;&#xA;  oidc {&#xA;    issuer_uri = &amp;#34;https://${var.hostname}&amp;#34;&#xA;  }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That &lt;code&gt;startsWith&lt;/code&gt; condition covers both run phases, so Google Cloud doesn&amp;rsquo;t have&#xA;Azure&amp;rsquo;s two-credential problem. It carries more weight than it looks like it&#xA;does, though. The service account binding grants &lt;code&gt;roles/iam.workloadIdentityUser&lt;/code&gt;&#xA;to every identity in the pool:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;member = &amp;#34;principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.this.name}/*&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;So the condition is the only thing checking that the token came from your&#xA;workspace and not from someone else&amp;rsquo;s. When I changed it to a different&#xA;organization, the plan failed immediately:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;oauth2/google: status code 400: {&amp;#34;error&amp;#34;:&amp;#34;unauthorized_client&amp;#34;,&#xA;&amp;#34;error_description&amp;#34;:&amp;#34;The given credential is rejected by the attribute condition.&amp;#34;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The workspace gets &lt;code&gt;TFC_GCP_PROVIDER_AUTH=true&lt;/code&gt;,&#xA;&lt;code&gt;TFC_GCP_WORKLOAD_PROVIDER_NAME&lt;/code&gt;, and &lt;code&gt;TFC_GCP_RUN_SERVICE_ACCOUNT_EMAIL&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h3 id=&#34;if-you-run-terraform-enterprise&#34;&gt;If You Run Terraform Enterprise&lt;/h3&gt;&#xA;&lt;p&gt;Everything above works the same on Terraform Enterprise, with the issuer set to&#xA;your own hostname instead of &lt;code&gt;app.terraform.io&lt;/code&gt;. Two things change.&lt;/p&gt;&#xA;&lt;p&gt;First, the cloud has to be able to reach you. To verify a token, the cloud&#xA;fetches &lt;code&gt;/.well-known/openid-configuration&lt;/code&gt; and &lt;code&gt;/.well-known/jwks&lt;/code&gt; from your&#xA;Terraform Enterprise hostname. An instance that&amp;rsquo;s only reachable on a private&#xA;network can&amp;rsquo;t do native dynamic credentials to a public cloud. Vault-backed&#xA;dynamic credentials exist partly for this case.&lt;/p&gt;&#xA;&lt;p&gt;Second, the certificate. HashiCorp&amp;rsquo;s Azure docs say &amp;ldquo;Custom and self-signed&#xA;certificates are not supported due to restrictions in Azure,&amp;rdquo; and the Google&#xA;Cloud docs say dynamic credentials &amp;ldquo;do not work if your Terraform Enterprise&#xA;instance uses a custom or self-signed certificate.&amp;rdquo; My instance uses a publicly&#xA;trusted certificate, so neither applied, but an instance behind an internal CA&#xA;would have hit both.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-3-the-shared-application-user&#34;&gt;Problem 3: The Shared Application User&lt;/h2&gt;&#xA;&lt;p&gt;The database version of a static credential is an application user whose&#xA;password was set once, lives in a config file or a Kubernetes secret, and is&#xA;shared by every instance of the application. Vault&amp;rsquo;s database secrets engine&#xA;offers two ways out, and my lab has both configured against the same Postgres&#xA;database for Terraform Enterprise.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-part-one-a-static-role&#34;&gt;The Fix, Part One: A Static Role&lt;/h3&gt;&#xA;&lt;p&gt;A static role is a one-to-one mapping between a Vault role and an existing&#xA;database user. Vault stores the user&amp;rsquo;s password and rotates it on a schedule.&#xA;Terraform Enterprise itself runs as &lt;code&gt;tfe_app&lt;/code&gt;, whose password Vault rotates&#xA;weekly:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;vault_database_secret_backend_static_role&amp;#34; &amp;#34;tfe_static&amp;#34; {&#xA;  backend         = vault_mount.tfe_psql.path&#xA;  name            = &amp;#34;tfe-static&amp;#34;&#xA;  db_name         = &amp;#34;tfe-psql&amp;#34;&#xA;  username        = &amp;#34;tfe_app&amp;#34;&#xA;  rotation_period = 604800 # 7 days&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Two things to know before you create one. Vault rotates the password the&#xA;moment the role is created, so the password you created the user with stops&#xA;working immediately. And the Vault docs are explicit that the user Vault itself&#xA;connects as should never be a static role: &amp;ldquo;Do not manage the same root database&#xA;credentials that you provide to Vault in config/ with static roles.&amp;rdquo; In my lab,&#xA;Vault connects as &lt;code&gt;tfe&lt;/code&gt;, and the application runs as &lt;code&gt;tfe_app&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;Terraform Enterprise runs on Kubernetes, so the Vault Secrets Operator (VSO)&#xA;delivers the password as a Kubernetes secret and restarts the deployment when it&#xA;changes:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;apiVersion: secrets.hashicorp.com/v1beta1&#xA;kind: VaultDynamicSecret&#xA;metadata:&#xA;  name: tfe-database&#xA;spec:&#xA;  vaultAuthRef: tfe-auth&#xA;  mount: tfe-psql&#xA;  path: static-creds/tfe-static&#xA;  allowStaticCreds: true&#xA;  refreshAfter: 24h&#xA;  destination:&#xA;    name: tfe-database&#xA;    create: true&#xA;  rolloutRestartTargets:&#xA;    - kind: Deployment&#xA;      name: terraform-enterprise&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;gotcha-the-first-rotation-is-the-real-test&#34;&gt;Gotcha: The First Rotation Is the Real Test&lt;/h3&gt;&#xA;&lt;p&gt;The first version of that manifest didn&amp;rsquo;t have &lt;code&gt;allowStaticCreds&lt;/code&gt;. It deployed&#xA;fine and Terraform Enterprise ran fine, for a week. Then Vault rotated the&#xA;password on schedule, the Kubernetes secret still held the old one, and Terraform&#xA;Enterprise started logging:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;failed SASL auth: FATAL: password authentication failed for user &amp;#34;tfe_app&amp;#34; (SQLSTATE 28P01)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Without &lt;code&gt;allowStaticCreds&lt;/code&gt;, VSO 1.4.0 treated the static credential response as&#xA;a non-renewable lease, concluded there was nothing left to refresh, and never&#xA;read it again. Its log said as much: &amp;ldquo;Vault secret does not support periodic&#xA;renewal/refresh via reconciliation.&amp;rdquo; The VSO API reference describes the flag as&#xA;something that &amp;ldquo;should be set when syncing credentials that are periodically&#xA;rotated by the Vault server, rather than created upon request.&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;A rotation setup that hasn&amp;rsquo;t survived its first rotation isn&amp;rsquo;t tested yet. If&#xA;your rotation period is a week, you find out a week later, which is usually&#xA;long after the change that caused it has been merged and forgotten.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-part-two-a-dynamic-role&#34;&gt;The Fix, Part Two: A Dynamic Role&lt;/h3&gt;&#xA;&lt;p&gt;A dynamic role goes further. Instead of rotating one user&amp;rsquo;s password, Vault&#xA;creates a brand new database user for each lease and drops it when the lease&#xA;ends. Every instance of the application gets its own username, which the Vault&#xA;docs point out makes auditing easier, because you can trace a query to a&#xA;specific instance.&lt;/p&gt;&#xA;&lt;p&gt;My lab has a dynamic role on the same database:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;resource &amp;#34;vault_database_secret_backend_role&amp;#34; &amp;#34;tfe_psql&amp;#34; {&#xA;  backend = vault_mount.tfe_psql.path&#xA;  name    = &amp;#34;tfe-psql-role&amp;#34;&#xA;  db_name = &amp;#34;tfe-psql&amp;#34;&#xA;&#xA;  creation_statements = [&#xA;    &amp;#34;CREATE ROLE \&amp;#34;{{name}}\&amp;#34; WITH LOGIN PASSWORD &amp;#39;{{password}}&amp;#39; VALID UNTIL &amp;#39;{{expiration}}&amp;#39;;\nGRANT ALL PRIVILEGES ON DATABASE tfe TO \&amp;#34;{{name}}\&amp;#34;;&amp;#34;&#xA;  ]&#xA;&#xA;  default_ttl = 3600  # 1 hour&#xA;  max_ttl     = 86400 # 24 hours&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Nothing in the lab actually consumes it. It has only ever been read by hand. So&#xA;before writing about it, I reproduced it against a throwaway Vault and Postgres&#xA;17, with the same creation statement and a table owned by a separate user, and&#xA;tried to use it the way an application would.&lt;/p&gt;&#xA;&lt;h3 id=&#34;gotcha-grant-all-on-a-database-doesnt-reach-the-tables&#34;&gt;Gotcha: GRANT ALL on a Database Doesn&amp;rsquo;t Reach the Tables&lt;/h3&gt;&#xA;&lt;p&gt;The generated user could log in, and that was all:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;ERROR:  permission denied for table widgets&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In Postgres, the privileges on a database are about connecting to it and&#xA;creating things in it, not about the tables inside it. &lt;code&gt;GRANT ALL PRIVILEGES ON DATABASE&lt;/code&gt; reads like it grants everything, and it grants none of the access an&#xA;application needs. The example role in Vault&amp;rsquo;s own Postgres docs uses&#xA;&lt;code&gt;GRANT SELECT ON ALL TABLES IN SCHEMA public&lt;/code&gt; instead.&lt;/p&gt;&#xA;&lt;h3 id=&#34;gotcha-a-user-that-owns-something-cant-be-dropped&#34;&gt;Gotcha: A User That Owns Something Can&amp;rsquo;t Be Dropped&lt;/h3&gt;&#xA;&lt;p&gt;The second problem is worse, because it shows up when the lease ends. I granted&#xA;the dynamic user permission to create tables, had it create one, and revoked the&#xA;lease. Vault queued the revocation, and the user was still there. The Vault log&#xA;explained why:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;[ERROR] expiration: failed to revoke lease: lease_id=db/creds/dyn/h3AscHe4Ki5S9HC096qXdMsL&#xA;error=&amp;#34;failed to revoke entry: resp: (*logical.Response)(nil) err: ERROR: role&#xA;\&amp;#34;v-token-dyn-hOoD3GJA9yCYjC7PX4uC-1789066250\&amp;#34; cannot be dropped because some&#xA;objects depend on it (SQLSTATE 2BP01)&amp;#34; attempts=1 next_attempt=21.012025154s&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Vault retries, and it keeps failing. Any application that runs its own schema&#xA;migrations will create objects owned by whichever user ran them, which is a&#xA;user that&amp;rsquo;s supposed to disappear.&lt;/p&gt;&#xA;&lt;p&gt;The fix that worked was to give ownership to a role that never logs in, and&#xA;make every dynamic user act as that role:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;CREATE ROLE app_schema NOLOGIN;&#xA;ALTER TABLE widgets OWNER TO app_schema;&#xA;GRANT CREATE ON SCHEMA public TO app_schema;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then the creation statement makes each generated user a member of &lt;code&gt;app_schema&lt;/code&gt;&#xA;and sets it as their default role:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-sql&#34; data-lang=&#34;sql&#34;&gt;CREATE ROLE &amp;#34;{{name}}&amp;#34; WITH LOGIN PASSWORD &amp;#39;{{password}}&amp;#39;&#xA;  VALID UNTIL &amp;#39;{{expiration}}&amp;#39; IN ROLE app_schema;&#xA;ALTER ROLE &amp;#34;{{name}}&amp;#34; SET ROLE app_schema;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With that in place, the generated user could read the table, a table it created&#xA;was owned by &lt;code&gt;app_schema&lt;/code&gt;, and revoking the lease dropped the user cleanly.&lt;/p&gt;&#xA;&lt;h3 id=&#34;static-or-dynamic&#34;&gt;Static or Dynamic?&lt;/h3&gt;&#xA;&lt;p&gt;They fail differently, and that&amp;rsquo;s the useful way to choose. A static role keeps&#xA;one username and changes its password, so the risk is a consumer that doesn&amp;rsquo;t&#xA;pick up the new one. A dynamic role changes the username every lease, so the&#xA;risk is anything tied to the old username: permissions granted to it, and&#xA;objects it owns.&lt;/p&gt;&#xA;&lt;p&gt;Terraform Enterprise in my lab uses the static role. &lt;code&gt;tfe_app&lt;/code&gt; owns the &lt;code&gt;tfe&lt;/code&gt;&#xA;database and its &lt;code&gt;public&lt;/code&gt; schema, so the ownership problem never comes up, and&#xA;the only thing that has to be right is the rotation. For an application that&#xA;doesn&amp;rsquo;t own its schema, or where each instance should have its own identity,&#xA;a dynamic role is the stronger option, as long as its creation statement grants&#xA;access to the tables and not just the database.&lt;/p&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;Every substitution here follows the same pattern: remove the stored secret,&#xA;trust an identity system instead, and make whatever it issues expire.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;SSH certificates instead of key pairs&lt;/strong&gt; - Vault signs a certificate per job&#xA;with a thirty-minute TTL, and hosts trust the CA through &lt;code&gt;TrustedUserCAKeys&lt;/code&gt;.&#xA;Treat the CA resource as immutable: changing it means rebuilding every image&#xA;that trusts it.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Workload identity instead of access keys&lt;/strong&gt; - the platform signs a token per&#xA;run phase, and the cloud trades it for credentials that expire with the run.&#xA;On AWS, use &lt;code&gt;StringLike&lt;/code&gt; for the wildcard. On Azure, create a federated&#xA;credential for plan &lt;em&gt;and&lt;/em&gt; apply. On Google Cloud, the attribute condition is&#xA;the only check that the token came from your workspace.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Terraform Enterprise changes two things&lt;/strong&gt; - the cloud has to reach its OIDC&#xA;endpoints, and Azure and Google Cloud won&amp;rsquo;t accept a custom or self-signed&#xA;certificate.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Vault-owned database credentials instead of a shared user&lt;/strong&gt; - static roles&#xA;rotate one user&amp;rsquo;s password, and nothing proves the delivery works until the&#xA;first rotation. Dynamic roles create a user per lease, and need a creation&#xA;statement that grants table access and keeps ownership off the ephemeral user.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Test past the first run&lt;/strong&gt; - none of these failures showed up there. The&#xA;Azure apply failed after a clean plan, the VSO outage waited a week for the&#xA;first rotation, and the stuck revocation only appeared when the lease ended.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The Azure and Google Cloud configurations, with a workload you can run against&#xA;your own HCP Terraform or Terraform Enterprise workspace, are in&#xA;&lt;a href=&#34;https://github.com/404-code-not-found-com/terraform-dynamic-credentials&#34; target=&#34;_blank&#34;&gt;terraform-dynamic-credentials&lt;/a&gt;.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>How I Structure a Terraform Module</title>
      <link>https://www.404-code-not-found.com/posts/terraform-module-structure/</link>
      <pubDate>Fri, 21 Aug 2026 09:00:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/terraform-module-structure/</guid>
      <category>terraform</category>
      <category>iac</category>
      <category>pre-commit</category>
      <category>terraform-docs</category>
      <description>&lt;p&gt;&lt;img src=&#34;https://www.404-code-not-found.com/posts/terraform-module-structure/header.png&#34; alt=&#34;How I Structure a Terraform Module&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;Open a Terraform repository you have never seen before. There is a good chance&#xA;it looks like this: one &lt;code&gt;main.tf&lt;/code&gt; with eight hundred lines in it, a&#xA;&lt;code&gt;variables.tf&lt;/code&gt; where required and optional inputs are shuffled together in&#xA;alphabetical order, a provider block with no version constraint at all, and a&#xA;&lt;code&gt;README.md&lt;/code&gt; that documented the inputs accurately about four months ago.&lt;/p&gt;&#xA;&lt;p&gt;None of that is anybody&amp;rsquo;s fault. Terraform doesn&amp;rsquo;t care how you arrange your&#xA;files. Every file ending in &lt;code&gt;.tf&lt;/code&gt; in a directory gets concatenated before&#xA;anything is evaluated, so the language gives you no reason to prefer one layout&#xA;over another. The tutorials all put everything in &lt;code&gt;main.tf&lt;/code&gt; because they&amp;rsquo;re&#xA;teaching one resource at a time, and then that becomes the shape of the repo&#xA;forever.&lt;/p&gt;&#xA;&lt;p&gt;I&amp;rsquo;m not a developer by trade. I don&amp;rsquo;t have the muscle memory that lets someone&#xA;navigate an unfamiliar codebase by feel. What I have instead is a layout I use&#xA;every single time, so that six months from now, when I come back to a module I&#xA;half remember, I know where things are before I open anything.&lt;/p&gt;&#xA;&lt;p&gt;This post is that layout, and the reasoning behind each piece of it. Every&#xA;convention here is a response to a specific way I have watched a module go bad.&#xA;It&amp;rsquo;s all based on the&#xA;&lt;a href=&#34;https://developer.hashicorp.com/terraform/language/style&#34; target=&#34;_blank&#34;&gt;HashiCorp style guide&lt;/a&gt;,&#xA;with opinions layered on where the style guide leaves room.&lt;/p&gt;&#xA;&lt;p&gt;Everything below is scaffolded in&#xA;&lt;a href=&#34;https://github.com/404-code-not-found-com/terraform-module-template&#34; target=&#34;_blank&#34;&gt;terraform-module-template&lt;/a&gt;,&#xA;which I use for modules and root modules alike.&lt;/p&gt;&#xA;&lt;h2 id=&#34;tldr&#34;&gt;TL;DR&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;data.tf&lt;/code&gt;, &lt;code&gt;locals.tf&lt;/code&gt;, &lt;code&gt;providers.tf&lt;/code&gt;, &lt;code&gt;terraform.tf&lt;/code&gt;, &lt;code&gt;backend.tf&lt;/code&gt;,&#xA;&lt;code&gt;variables.tf&lt;/code&gt;, and &lt;code&gt;outputs.tf&lt;/code&gt; always exist, even when empty.&lt;/li&gt;&#xA;&lt;li&gt;Resources live in &lt;code&gt;main.tf&lt;/code&gt; until there are roughly twenty of them, then split&#xA;by service or function.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;variables.tf&lt;/code&gt; is two sections, required before optional, alphabetized inside&#xA;each. &lt;code&gt;outputs.tf&lt;/code&gt; is one alphabetized list.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform.tf&lt;/code&gt; holds &lt;code&gt;required_version&lt;/code&gt; and nothing else. &lt;code&gt;required_providers&lt;/code&gt;&#xA;belongs in &lt;code&gt;providers.tf&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Pin everything pinnable with &lt;code&gt;~&amp;gt; Maj.Min&lt;/code&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Generate the README with &lt;code&gt;terraform-docs&lt;/code&gt;, do not write it.&lt;/li&gt;&#xA;&lt;li&gt;Run &lt;code&gt;fmt&lt;/code&gt;, &lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;docs&lt;/code&gt;, &lt;code&gt;tflint&lt;/code&gt;, and &lt;code&gt;trivy&lt;/code&gt; in pre-commit, so you find&#xA;problems before a plan does.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;problem-1-everything-ends-up-in-maintf&#34;&gt;Problem 1: Everything Ends Up in main.tf&lt;/h2&gt;&#xA;&lt;p&gt;A module starts with three resources, so of course they go in &lt;code&gt;main.tf&lt;/code&gt;. Then it&#xA;grows. Nobody ever decides to put eight hundred lines in one file. It happens&#xA;because the alternative requires a decision, and there&amp;rsquo;s never a good moment to&#xA;make it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-files-by-role-first-size-second&#34;&gt;The Fix: Files by Role First, Size Second&lt;/h3&gt;&#xA;&lt;p&gt;Some file names are reserved for a role, and those files always exist:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-text&#34; data-lang=&#34;text&#34;&gt;backend.tf      state backend configuration&#xA;data.tf         data sources&#xA;locals.tf       local values&#xA;main.tf         resources&#xA;outputs.tf      module outputs&#xA;providers.tf    required_providers, plus provider configuration&#xA;terraform.tf    required_version&#xA;variables.tf    input variables&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Resources stay in &lt;code&gt;main.tf&lt;/code&gt; until there are around twenty of them. Past that,&#xA;&lt;code&gt;main.tf&lt;/code&gt; splits by service or function: &lt;code&gt;network.tf&lt;/code&gt;, &lt;code&gt;iam.tf&lt;/code&gt;, &lt;code&gt;database.tf&lt;/code&gt;.&#xA;The number isn&amp;rsquo;t sacred. The point is that the split is triggered by size, and&#xA;only resources ever get split. Data sources, locals, and provider configuration&#xA;stay in their own files no matter how few or how many there are.&lt;/p&gt;&#xA;&lt;p&gt;That asymmetry is deliberate. Resource count varies enormously between modules,&#xA;so where resources live has to scale. The other categories are small and&#xA;bounded, and their value comes from being findable at a fixed address rather&#xA;than from being organized well.&lt;/p&gt;&#xA;&lt;h3 id=&#34;why-empty-files-stay&#34;&gt;Why Empty Files Stay&lt;/h3&gt;&#xA;&lt;p&gt;In a fresh module, &lt;code&gt;data.tf&lt;/code&gt; and &lt;code&gt;locals.tf&lt;/code&gt; are empty except for a header&#xA;comment:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;###############################################################################&#xA;# data.tf&#xA;#&#xA;# Contains any data sources&#xA;###############################################################################&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This is the convention people push back on most, and I&amp;rsquo;ll keep defending it. An&#xA;absent file carries no information. You can&amp;rsquo;t tell whether this module has no&#xA;data sources or whether someone put them in &lt;code&gt;main.tf&lt;/code&gt; without looking. A&#xA;present, empty file answers both questions at once: there are no data sources&#xA;yet, and here is where the next one goes.&lt;/p&gt;&#xA;&lt;p&gt;Most structural decay in a repo doesn&amp;rsquo;t come from someone disagreeing with the&#xA;layout. It comes from someone adding a data source at 5pm, not seeing an obvious&#xA;home for it, and dropping it at the bottom of &lt;code&gt;main.tf&lt;/code&gt;. An empty &lt;code&gt;data.tf&lt;/code&gt;&#xA;removes the decision, which is the only reliable way to make a convention&#xA;survive contact with a deadline.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-2-you-cant-tell-required-from-optional&#34;&gt;Problem 2: You Can&amp;rsquo;t Tell Required From Optional&lt;/h2&gt;&#xA;&lt;p&gt;Alphabetical ordering in &lt;code&gt;variables.tf&lt;/code&gt; sounds obviously correct, and it&amp;rsquo;s the&#xA;default advice. It also means that the first thing you want to know about a&#xA;module - what do I have to give it? - is the one thing the file won&amp;rsquo;t tell you.&#xA;You have to read every block and check each one for a &lt;code&gt;default&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-two-sections-alphabetized-inside-each&#34;&gt;The Fix: Two Sections, Alphabetized Inside Each&lt;/h3&gt;&#xA;&lt;p&gt;&lt;code&gt;variables.tf&lt;/code&gt; is split into required variables, meaning no &lt;code&gt;default&lt;/code&gt;, followed&#xA;by optional variables, meaning there is one. Each section is alphabetized&#xA;independently:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;###############################################################################&#xA;# Required Variables (no default values)&#xA;###############################################################################&#xA;variable &amp;#34;image_id&amp;#34; {&#xA;  type        = string&#xA;  description = &amp;#34;The id of the machine image (AMI) to use for the server.&amp;#34;&#xA;&#xA;  validation {&#xA;    condition     = length(var.image_id) &amp;gt; 4 &amp;amp;&amp;amp; substr(var.image_id, 0, 4) == &amp;#34;ami-&amp;#34;&#xA;    error_message = &amp;#34;The image_id value must be a valid AMI id, starting with \&amp;#34;ami-\&amp;#34;.&amp;#34;&#xA;  }&#xA;}&#xA;&#xA;###############################################################################&#xA;# Optional Variables (has a default value)&#xA;###############################################################################&#xA;variable &amp;#34;instance_type&amp;#34; {&#xA;  type        = string&#xA;  description = &amp;#34;Instance Type&amp;#34;&#xA;  default     = &amp;#34;t3.micro&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now the top of the file is the module&amp;rsquo;s contract. Everything above the second&#xA;banner is what a caller must supply. Everything below it is a knob they may&#xA;choose to turn. Sorting alphabetically inside each section keeps lookup fast&#xA;without giving up that distinction.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;outputs.tf&lt;/code&gt; gets plain alphabetical ordering, because outputs have no&#xA;equivalent of the required/optional split. There is only one axis to sort on, so&#xA;sort on it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;make-the-generated-docs-agree&#34;&gt;Make the Generated Docs Agree&lt;/h3&gt;&#xA;&lt;p&gt;A convention that only lives in the source file will drift the moment something&#xA;else renders the same information in a different order. &lt;code&gt;terraform-docs&lt;/code&gt;&#xA;produces the inputs table in the README, and by default it sorts by name, which&#xA;would immediately contradict &lt;code&gt;variables.tf&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;So &lt;code&gt;.terraform-docs.yml&lt;/code&gt; is configured to sort the same way:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;sort:&#xA;  enabled: true&#xA;  by: required&#xA;&#xA;settings:&#xA;  anchor: false&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Required inputs come first in the generated table, exactly as they do in the&#xA;file that generated it. It&amp;rsquo;s a small thing, but it is the difference between a&#xA;convention that is asserted once in a style guide and one that is enforced in&#xA;two places and therefore holds.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-3-one-file-for-every-version-constraint&#34;&gt;Problem 3: One File for Every Version Constraint&lt;/h2&gt;&#xA;&lt;p&gt;The common layout is a single &lt;code&gt;versions.tf&lt;/code&gt; holding both &lt;code&gt;required_version&lt;/code&gt; and&#xA;&lt;code&gt;required_providers&lt;/code&gt;, or a &lt;code&gt;terraform&lt;/code&gt; block sitting at the top of &lt;code&gt;main.tf&lt;/code&gt;&#xA;with everything in it. Both work. Both bury two unrelated decisions in the same&#xA;place.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-three-files-three-concerns&#34;&gt;The Fix: Three Files, Three Concerns&lt;/h3&gt;&#xA;&lt;p&gt;The version of Terraform itself, alone in its own file:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;# terraform.tf&#xA;terraform {&#xA;  required_version = &amp;#34;~&amp;gt; 1.0&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The providers, their versions, and their configuration, together in another:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;# providers.tf&#xA;terraform {&#xA;  required_providers {&#xA;    google = {&#xA;      source  = &amp;#34;hashicorp/google&amp;#34;&#xA;      version = &amp;#34;~&amp;gt; 6.28&amp;#34;&#xA;    }&#xA;  }&#xA;}&#xA;&#xA;provider &amp;#34;google&amp;#34; {&#xA;  # Configuration options&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And where state lives, in a third:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;# backend.tf&#xA;terraform {&#xA;  cloud {&#xA;    organization = &amp;#34;example_corp&amp;#34;&#xA;    hostname     = &amp;#34;app.terraform.io&amp;#34;&#xA;&#xA;    workspaces {&#xA;      tags = [&amp;#34;app&amp;#34;]&#xA;    }&#xA;  }&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Yes, that&amp;rsquo;s three separate &lt;code&gt;terraform&lt;/code&gt; blocks across three files. Terraform&#xA;merges them, so this costs nothing at evaluation time and buys a clean&#xA;separation:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform.tf&lt;/code&gt;&lt;/strong&gt; is about the tool. It changes when I decide to adopt a new&#xA;Terraform version, which is a deliberate, infrequent, repo-wide decision.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;providers.tf&lt;/code&gt;&lt;/strong&gt; is about dependencies. It changes when a provider ships&#xA;something I need, which happens on somebody else&amp;rsquo;s schedule and much more&#xA;often. It also holds provider configuration, which is what I&amp;rsquo;m editing most of&#xA;the time.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;backend.tf&lt;/code&gt;&lt;/strong&gt; is about where state lives. It&amp;rsquo;s the one file most likely to&#xA;differ between environments or to be supplied at init time, and the one most&#xA;likely to be the reason a &lt;code&gt;terraform init&lt;/code&gt; behaves differently on someone&#xA;else&amp;rsquo;s machine.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Keeping them apart means a diff tells you which kind of change you are looking&#xA;at before you read a single line of it.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-4-pins-that-are-too-loose-or-too-tight&#34;&gt;Problem 4: Pins That Are Too Loose or Too Tight&lt;/h2&gt;&#xA;&lt;p&gt;There are two failure modes here and they look nothing alike.&lt;/p&gt;&#xA;&lt;p&gt;Too loose, usually meaning no constraint at all, gives you a plan that worked&#xA;yesterday and doesn&amp;rsquo;t work today, because a provider released a major version&#xA;overnight and &lt;code&gt;terraform init&lt;/code&gt; on a clean checkout happily took it.&lt;/p&gt;&#xA;&lt;p&gt;Too tight, meaning an exact pin like &lt;code&gt;version = &amp;quot;6.28.0&amp;quot;&lt;/code&gt;, gives you a module&#xA;that never picks up a bug fix unless someone edits it, and a fleet of repos&#xA;pinned to a scatter of slightly different patch releases.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-pessimistic-constraints-on-everything-pinnable&#34;&gt;The Fix: Pessimistic Constraints on Everything Pinnable&lt;/h3&gt;&#xA;&lt;p&gt;Everything that can be pinned gets pinned with the pessimistic operator at the&#xA;major-minor level: Terraform core, every provider, every module call.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;version = &amp;#34;~&amp;gt; 6.28&amp;#34;    # &amp;gt;= 6.28.0, &amp;lt; 7.0.0&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Minor and patch releases flow in. A major version, which is where the breaking&#xA;changes live by convention, does not. That&amp;rsquo;s the tradeoff I want almost&#xA;everywhere: I get fixes without being asked, and I get to schedule the upgrades&#xA;that cost me something.&lt;/p&gt;&#xA;&lt;p&gt;One edge case. For a provider still on &lt;code&gt;0.x&lt;/code&gt;, &lt;code&gt;~&amp;gt; 0.104&lt;/code&gt; expands to&#xA;&lt;code&gt;&amp;gt;= 0.104.0, &amp;lt; 1.0.0&lt;/code&gt;, and pre-1.0 providers routinely make breaking changes on&#xA;a minor bump. If a dependency matters and is still pre-1.0, tighten it to&#xA;&lt;code&gt;~&amp;gt; 0.104.0&lt;/code&gt; and accept the manual bumps.&lt;/p&gt;&#xA;&lt;h3 id=&#34;commit-the-lock-file&#34;&gt;Commit the Lock File&lt;/h3&gt;&#xA;&lt;p&gt;&lt;code&gt;~&amp;gt; 6.28&lt;/code&gt; is a statement about what you will tolerate. &lt;code&gt;.terraform.lock.hcl&lt;/code&gt; is&#xA;a record of what you actually got, and it belongs in version control.&lt;/p&gt;&#xA;&lt;p&gt;The lock file holds on to a provider entry as long as state still references&#xA;that provider, even after you&amp;rsquo;ve deleted every last resource for it from your&#xA;configuration. The instinct is to open the lock file and delete the stanza.&#xA;Don&amp;rsquo;t. Apply first so the resources leave state, and the next &lt;code&gt;terraform init&lt;/code&gt;&#xA;will prune the entry on its own. Hand-editing a lock file to fix a problem that&#xA;an apply would have fixed tends to produce a second, stranger problem.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-5-the-readme-drifts&#34;&gt;Problem 5: The README Drifts&lt;/h2&gt;&#xA;&lt;p&gt;Every Terraform README starts with an accurate inputs table. Then someone adds a&#xA;variable in a hurry. The code is right and the documentation is wrong, and now&#xA;the README is worse than no README, because people trust it.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-generate-it&#34;&gt;The Fix: Generate It&lt;/h3&gt;&#xA;&lt;p&gt;&lt;code&gt;terraform-docs&lt;/code&gt; reads the actual variable and output blocks and writes the&#xA;tables into the README between marker comments. It runs as a pre-commit hook, so&#xA;it is not something anyone has to remember:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;- id: terraform_docs&#xA;  args:&#xA;    - --args=--lockfile=false&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The one thing that catches everybody the first time: the hook rewrites&#xA;&lt;code&gt;README.md&lt;/code&gt; during the commit, which means the commit fails, because the file it&#xA;just changed was not part of what you staged. This is correct behavior and it&#xA;looks like a broken hook. Re-stage and commit again and it passes. It only&#xA;happens when the generated content actually changed, so after the first time it&#xA;mostly disappears.&lt;/p&gt;&#xA;&lt;h2 id=&#34;problem-6-you-find-the-error-at-apply-time&#34;&gt;Problem 6: You Find the Error at Apply Time&lt;/h2&gt;&#xA;&lt;p&gt;The expensive version of every mistake in this post is the one you discover&#xA;after &lt;code&gt;terraform apply&lt;/code&gt; has already created four of seven resources.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-fix-a-pre-commit-chain-in-order&#34;&gt;The Fix: A Pre-Commit Chain, in Order&lt;/h3&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;- repo: https://github.com/antonbabenko/pre-commit-terraform&#xA;  rev: v1.108.1&#xA;  hooks:&#xA;    - id: terraform_fmt&#xA;    - id: terraform_validate&#xA;      args:&#xA;        - --hook-config=--retry-once-with-cleanup=true&#xA;    - id: terraform_docs&#xA;      args:&#xA;        - --args=--lockfile=false&#xA;    - id: terraform_tflint&#xA;      args:&#xA;        - --args=--config=__GIT_WORKING_DIR__/.tflint.hcl&#xA;    - id: terraform_trivy&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The order is cheapest and most mechanical first, so that later hooks are reading&#xA;code that is already well formed:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform_fmt&lt;/code&gt;&lt;/strong&gt; normalizes whitespace and alignment. Running it first&#xA;means no other hook, and no future diff, is ever about formatting.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform_validate&lt;/code&gt;&lt;/strong&gt; catches syntax and internal consistency errors.&#xA;&lt;code&gt;--retry-once-with-cleanup=true&lt;/code&gt; handles the common case where validation&#xA;fails only because of a stale &lt;code&gt;.terraform&lt;/code&gt; directory: it clears it and tries&#xA;once more instead of reporting a problem you don&amp;rsquo;t have.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform_docs&lt;/code&gt;&lt;/strong&gt; regenerates the README, now that the code it is&#xA;documenting is known to be valid.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform_tflint&lt;/code&gt;&lt;/strong&gt; catches the things &lt;code&gt;validate&lt;/code&gt; structurally cannot -&#xA;deprecated syntax, unused declarations, provider-specific mistakes such as an&#xA;instance type that doesn&amp;rsquo;t exist.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;terraform_trivy&lt;/code&gt;&lt;/strong&gt; scans for security misconfigurations. It goes last&#xA;because it is the slowest and the most likely to need a human judgment call&#xA;about whether a finding applies.&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;Alongside those, the standard hygiene hooks run on everything, and one of them&#xA;earns a special mention:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;- id: no-commit-to-branch&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;That blocks commits directly to &lt;code&gt;main&lt;/code&gt;. It&amp;rsquo;s saved me from myself more than any&#xA;of the Terraform-specific hooks.&lt;/p&gt;&#xA;&lt;h2 id=&#34;the-directories-you-dont-need-yet&#34;&gt;The Directories You Don&amp;rsquo;t Need Yet&lt;/h2&gt;&#xA;&lt;p&gt;The template ships five directories that are empty apart from a &lt;code&gt;README.md&lt;/code&gt;:&#xA;&lt;code&gt;examples/&lt;/code&gt;, &lt;code&gt;modules/&lt;/code&gt;, &lt;code&gt;tests/&lt;/code&gt;, &lt;code&gt;files/&lt;/code&gt;, and &lt;code&gt;templates/&lt;/code&gt;.&lt;/p&gt;&#xA;&lt;p&gt;The placeholder README exists because git won&amp;rsquo;t track an empty directory, so&#xA;without a file in it the directory would not survive a clone. But the reason to&#xA;scaffold them at all is the same reason the empty &lt;code&gt;data.tf&lt;/code&gt; earns its place.&#xA;When it&amp;rsquo;s time to write the first test, there is no decision to make about where&#xA;tests go. When someone needs a usage example, &lt;code&gt;examples/&lt;/code&gt; is already there with&#xA;a note explaining what belongs in it.&lt;/p&gt;&#xA;&lt;p&gt;Scaffolding an empty directory costs nothing today. Adding one to a repo that&#xA;has already grown around its absence means moving files and updating every path&#xA;that pointed at them.&lt;/p&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;None of this is clever, and that&amp;rsquo;s the point. It&amp;rsquo;s a set of decisions made once&#xA;so that they do not have to be made again while tired:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Reserved file names, always present&lt;/strong&gt; - &lt;code&gt;data.tf&lt;/code&gt;, &lt;code&gt;locals.tf&lt;/code&gt;,&#xA;&lt;code&gt;providers.tf&lt;/code&gt;, &lt;code&gt;terraform.tf&lt;/code&gt;, &lt;code&gt;backend.tf&lt;/code&gt;, &lt;code&gt;variables.tf&lt;/code&gt;, &lt;code&gt;outputs.tf&lt;/code&gt;.&#xA;Empty is fine, and an empty file still tells you something.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Only resources scale&lt;/strong&gt; - &lt;code&gt;main.tf&lt;/code&gt; splits by service past roughly twenty&#xA;resources. Nothing else splits, ever.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Required before optional&lt;/strong&gt; in &lt;code&gt;variables.tf&lt;/code&gt;, alphabetized within each&#xA;section, with &lt;code&gt;terraform-docs&lt;/code&gt; configured to sort the same way so the&#xA;generated table cannot contradict the source.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Three files for three kinds of version decision&lt;/strong&gt; - the tool, its&#xA;dependencies, and where state lives.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;~&amp;gt; Maj.Min&lt;/code&gt; on everything pinnable&lt;/strong&gt;, with the pre-1.0 caveat, and the lock&#xA;file committed as the record of what you actually resolved.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Generate the README&lt;/strong&gt;, never hand-maintain it.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;fmt&lt;/code&gt;, &lt;code&gt;validate&lt;/code&gt;, &lt;code&gt;docs&lt;/code&gt;, &lt;code&gt;tflint&lt;/code&gt;, &lt;code&gt;trivy&lt;/code&gt;&lt;/strong&gt; in that order in pre-commit,&#xA;because the cheapest place to find any of these problems is before the commit,&#xA;and the most expensive is halfway through an apply.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;The whole thing is in&#xA;&lt;a href=&#34;https://github.com/404-code-not-found-com/terraform-module-template&#34; target=&#34;_blank&#34;&gt;terraform-module-template&lt;/a&gt;&#xA;if you would rather clone it than assemble it.&lt;/p&gt;&#xA;&lt;p&gt;If you disagree with a specific convention here, that&amp;rsquo;s fine and probably&#xA;healthy. What matters far more than which layout you pick is that you pick one&#xA;and it&amp;rsquo;s the same in every repo. Decide once, and you stop spending attention on&#xA;the question.&lt;/p&gt;&#xA;&lt;h2 id=&#34;up-next-terraform-in-practice&#34;&gt;Up Next: Terraform in Practice&lt;/h2&gt;&#xA;&lt;p&gt;This kicks off a series on the practices that sit around the Terraform code&#xA;rather than in it. Next up: getting static credentials out of your&#xA;infrastructure entirely - a Vault SSH certificate authority instead of&#xA;distributing key pairs, OIDC instead of long-lived AWS access keys, and dynamic&#xA;database credentials instead of a shared application user. Three substitutions,&#xA;and the gotcha that bites on each one.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>Running Terraform Enterprise Active-Active on OpenShift: Lessons Learned</title>
      <link>https://www.404-code-not-found.com/posts/tfe-on-openshift-active-active/</link>
      <pubDate>Thu, 02 Jul 2026 08:00:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/tfe-on-openshift-active-active/</guid>
      <category>terraform-enterprise</category>
      <category>openshift</category>
      <category>kubernetes</category>
      <category>postgresql</category>
      <category>vault</category>
      <description>&lt;p&gt;&lt;img src=&#34;https://www.404-code-not-found.com/posts/tfe-on-openshift-active-active/article.png&#34; alt=&#34;Data center corridor with server racks lit by purple and orange accent lighting&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;In my &lt;a href=&#34;https://www.404-code-not-found.com/posts/vault-on-openshift-hcp-autounseal&#34;&gt;last post&lt;/a&gt;, I covered the four&#xA;challenges I hit running Vault Enterprise on OpenShift. That same lab cluster&#xA;also runs Terraform Enterprise (TFE) in active-active mode, using that Vault&#xA;cluster as its secrets backend. TFE brought its own set of OpenShift-specific&#xA;problems — some in TFE itself, some in the supporting PostgreSQL and object&#xA;storage layers, and one that turned out to be the same VSO bug from the Vault&#xA;post showing up again in a different place.&lt;/p&gt;&#xA;&lt;p&gt;The deployment follows the same GitOps pattern as Vault: ArgoCD, a three-source&#xA;Helm setup (upstream TFE chart, a values file from the repo, and raw manifests&#xA;for VSO custom resources, the Route, and the ImageStream/BuildConfig that&#xA;supporting pieces need). TFE runs with 2 replicas against a CloudNativePG-based&#xA;PostgreSQL cluster, a Redis deployment for active-active coordination, and a&#xA;NooBaa S3 bucket (via OpenShift Data Foundation) for object storage. Every&#xA;secret TFE needs — license, encryption password, database credentials, Redis&#xA;password, S3 keys, registry pull secret, TLS certificate — comes from the local&#xA;Vault cluster through Vault Secrets Operator (VSO).&lt;/p&gt;&#xA;&lt;p&gt;This post covers five things that caught me off guard: TFE&amp;rsquo;s hard refusal to&#xA;run mixed versions during a rolling update, why one Postgres cluster ended up&#xA;with two different Vault-managed role designs, a missing trust anchor for the&#xA;in-cluster S3 endpoint, the VSO stuck-reconciliation bug recurring across two&#xA;more secrets, and what it takes to get TFE&amp;rsquo;s job agent running under&#xA;OpenShift&amp;rsquo;s restricted security model.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-1-active-active-means-lockstep-versions&#34;&gt;Challenge 1: Active-Active Means Lockstep Versions&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-problem&#34;&gt;The Problem&lt;/h3&gt;&#xA;&lt;p&gt;TFE&amp;rsquo;s active-active mode replicates its Rails processes across multiple pods&#xA;that all have to agree on the running version. I found this out during what&#xA;should have been a routine chart and image bump. The new pod came up as&#xA;&lt;code&gt;CrashLoopBackOff&lt;/code&gt; while the two existing pods stayed healthy:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;startup check failed: check=upgrade ... TFE version (2.0.4) is not&#xA;inter-compatible with system version (2.0.2), to upgrade to this version&#xA;active TFE versions must be stopped&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The root cause is the Helm chart&amp;rsquo;s default &lt;code&gt;RollingUpdate&lt;/code&gt; strategy with&#xA;&lt;code&gt;maxSurge: 25%&lt;/code&gt; — it starts the new-version pod &lt;em&gt;alongside&lt;/em&gt; the two&#xA;still-running old-version pods to keep capacity up during the rollout. TFE&amp;rsquo;s&#xA;own startup check treats that mixed-version state as invalid and refuses to&#xA;come up, so the new pod crash-loops forever while the old pods sit there&#xA;looking healthy.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-solution&#34;&gt;The Solution&lt;/h3&gt;&#xA;&lt;p&gt;The fix is to pin the Deployment to &lt;code&gt;Recreate&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;# values.yaml&#xA;strategy:&#xA;  type: Recreate&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;Recreate&lt;/code&gt; tears down all pods before bringing any of them back up on the new&#xA;image, so there&amp;rsquo;s never a moment where two versions coexist. The trade-off is&#xA;real and worth stating plainly: &lt;strong&gt;every deploy that touches the pod template —&#xA;image bump or not — causes a brief full TFE outage while pods restart&lt;/strong&gt;&#xA;(around 3 minutes, observed). For an active-active system, that&amp;rsquo;s a step back&#xA;from zero-downtime rollouts, but it&amp;rsquo;s the only option that matches TFE&amp;rsquo;s&#xA;lockstep version requirement.&lt;/p&gt;&#xA;&lt;p&gt;If you ever hit the crash-loop mid-upgrade (say, the strategy setting got&#xA;reverted by mistake), the recovery is a coordinated restart:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;oc scale deployment terraform-enterprise -n tfe --replicas=0&#xA;oc scale deployment terraform-enterprise -n tfe --replicas=2&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;One subtlety if you&amp;rsquo;re running ArgoCD with &lt;code&gt;selfHeal: true&lt;/code&gt;: it will fight the&#xA;scale-to-0 and start restoring replicas almost immediately. That&amp;rsquo;s fine here —&#xA;the new ReplicaSet on the new image brings both pods up together, which is&#xA;exactly what you want. Both pods reached &lt;code&gt;1/1 Running&lt;/code&gt; within a few minutes.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-2-one-postgres-cluster-two-vault-role-designs&#34;&gt;Challenge 2: One Postgres Cluster, Two Vault Role Designs&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-problem-1&#34;&gt;The Problem&lt;/h3&gt;&#xA;&lt;p&gt;Vault&amp;rsquo;s database secrets engine has an obvious, idiomatic pattern: a dynamic&#xA;role that creates a brand-new Postgres user on every lease, with a short TTL.&#xA;That&amp;rsquo;s what I set up first:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;vault write tfe-psql/roles/tfe-psql-role \&#xA;  db_name=tfe \&#xA;  creation_statements=&amp;#34;&#xA;    CREATE ROLE \&amp;#34;{{name}}\&amp;#34; WITH LOGIN PASSWORD &amp;#39;{{password}}&amp;#39; VALID UNTIL &amp;#39;{{expiration}}&amp;#39;;&#xA;    GRANT ALL PRIVILEGES ON DATABASE tfe TO \&amp;#34;{{name}}\&amp;#34;;&#xA;  &amp;#34; \&#xA;  default_ttl=&amp;#34;1h&amp;#34; \&#xA;  max_ttl=&amp;#34;24h&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;It works — Vault happily mints a fresh, uniquely-named Postgres role every&#xA;hour. The problem is that TFE doesn&amp;rsquo;t want that. It expects to connect as one&#xA;stable database user across restarts, and its migrations need that user to&#xA;&lt;em&gt;own&lt;/em&gt; the database and the &lt;code&gt;public&lt;/code&gt; schema — not just have grants on them.&#xA;Dynamic per-lease roles with randomized names don&amp;rsquo;t fit that model at all.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-solution-1&#34;&gt;The Solution&lt;/h3&gt;&#xA;&lt;p&gt;The fix was to layer Vault&amp;rsquo;s &lt;strong&gt;static role&lt;/strong&gt; feature on top of the same&#xA;database connection, rather than replacing the dynamic role. A static role&#xA;manages the password for an existing, stable-named Postgres user on a fixed&#xA;rotation schedule instead of creating new ones:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Allow both role types on the same connection&#xA;vault write tfe-psql/config/tfe-psql \&#xA;  allowed_roles=&amp;#34;tfe-psql-role,tfe-static&amp;#34;&#xA;&#xA;# Register the static role&#xA;vault write tfe-psql/static-roles/tfe-static \&#xA;  db_name=tfe-psql \&#xA;  username=tfe_app \&#xA;  rotation_period=604800&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But the &lt;code&gt;tfe_app&lt;/code&gt; user has to exist and own the right objects before Vault can&#xA;manage its password. That&amp;rsquo;s a one-time manual bootstrap: create the role, then&#xA;hand over ownership as the Postgres superuser:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Create the role using the cluster&amp;#39;s admin user&#xA;psql &amp;#34;host=tfe-psql-primary.tfe.svc.cluster.local user=tfe dbname=tfe sslmode=require&amp;#34; \&#xA;  -c &amp;#34;CREATE ROLE tfe_app WITH LOGIN PASSWORD &amp;#39;${INIT_PW}&amp;#39;;&amp;#34;&#xA;&#xA;# As the postgres superuser, transfer ownership&#xA;psql -U postgres -d tfe -c &amp;#34;&#xA;  ALTER DATABASE tfe OWNER TO tfe_app;&#xA;  GRANT ALL ON SCHEMA public TO tfe_app;&#xA;  ALTER SCHEMA public OWNER TO tfe_app;&#xA;&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;After that, Vault rotates &lt;code&gt;tfe_app&lt;/code&gt;&amp;rsquo;s password every 7 days without ever&#xA;changing the username, which is exactly what TFE needs. The dynamic role&#xA;stays registered too — it&amp;rsquo;s a fine pattern for ad hoc admin access, just not&#xA;for the application&amp;rsquo;s own connection.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-3-noobaa-s3-and-a-missing-trust-anchor&#34;&gt;Challenge 3: NooBaa S3 and a Missing Trust Anchor&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-problem-2&#34;&gt;The Problem&lt;/h3&gt;&#xA;&lt;p&gt;TFE uses S3-compatible object storage for run logs and state, and this&#xA;cluster provides that in-cluster via NooBaa (part of OpenShift Data&#xA;Foundation) rather than a cloud S3 bucket. Pointing TFE at NooBaa&amp;rsquo;s internal&#xA;endpoint seemed straightforward — until TFE crashed on startup with:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;x509: certificate signed by unknown authority&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The NooBaa S3 endpoint&amp;rsquo;s certificate is signed by OpenShift&amp;rsquo;s own internal&#xA;service-CA, and TFE&amp;rsquo;s container doesn&amp;rsquo;t trust that CA by default. Nothing&#xA;about the bucket configuration was wrong; TFE just couldn&amp;rsquo;t verify the TLS&#xA;certificate on the connection.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-solution-2&#34;&gt;The Solution&lt;/h3&gt;&#xA;&lt;p&gt;Every OpenShift namespace gets an auto-populated ConfigMap&#xA;(&lt;code&gt;openshift-service-ca.crt&lt;/code&gt;) containing exactly that CA certificate. The fix&#xA;is to mount it into the path TFE&amp;rsquo;s chart already exposes for a custom CA&#xA;bundle:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;# values.yaml&#xA;tls:&#xA;  caCertBaseDir: /etc/ssl/certs&#xA;  caCertFileName: custom_ca_certs.pem&#xA;&#xA;extraVolumes:&#xA;  - name: openshift-service-ca&#xA;    configMap:&#xA;      name: openshift-service-ca.crt&#xA;      items:&#xA;        - key: service-ca.crt&#xA;          path: custom_ca_certs.pem&#xA;&#xA;extraVolumeMounts:&#xA;  - name: openshift-service-ca&#xA;    mountPath: /etc/ssl/certs/custom_ca_certs.pem&#xA;    subPath: custom_ca_certs.pem&#xA;    readOnly: true&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once that ConfigMap is mounted and referenced by &lt;code&gt;caCertBaseDir&lt;/code&gt;/&#xA;&lt;code&gt;caCertFileName&lt;/code&gt;, TFE trusts the NooBaa endpoint&amp;rsquo;s certificate and connects&#xA;cleanly.&lt;/p&gt;&#xA;&lt;p&gt;Worth noting on the bucket side: the &lt;code&gt;ObjectBucketClaim&lt;/code&gt; that provisions the&#xA;bucket also produces a Secret with the S3 access keys, but TFE never reads&#xA;that Secret directly. Those keys get copied into Vault, and VSO syncs them&#xA;back out into the Secret TFE actually consumes — keeping VSO/Vault as the&#xA;single source of truth for every credential, not just the ones that&#xA;originate there.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-4-the-vso-stuck-reconciliation-bug-again&#34;&gt;Challenge 4: The VSO Stuck-Reconciliation Bug, Again&lt;/h2&gt;&#xA;&lt;p&gt;If you read the Vault post, you&amp;rsquo;ll remember VSO 1.4.0&amp;rsquo;s bug where a&#xA;&lt;code&gt;VaultStaticSecret&lt;/code&gt; can stop reconciling after a transient Vault HA error —&#xA;and doesn&amp;rsquo;t recover on its own even if you restart the VSO controller. That&#xA;bug wasn&amp;rsquo;t a one-off. It hit twice more here, on two of TFE&amp;rsquo;s secrets, each&#xA;time triggered by an unrelated Vault rollout causing a brief Raft leader&#xA;election:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;local node not active but active cluster node not found&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Once on the &lt;code&gt;VaultStaticSecret&lt;/code&gt; backing TFE&amp;rsquo;s S3 credentials, and once on the&#xA;one backing its Redis password — both during Vault maintenance that had&#xA;nothing to do with TFE. Same symptom both times: &lt;code&gt;SecretSynced=False&lt;/code&gt;, a&#xA;&lt;code&gt;lastTransitionTime&lt;/code&gt; stuck days in the past, and zero new controller log&#xA;lines for the resource.&lt;/p&gt;&#xA;&lt;p&gt;The fix is the same spec-patch workaround from the Vault post — touch the&#xA;resource to force VSO to requeue it, then revert:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;oc -n tfe patch vaultstaticsecret tfe-s3 \&#xA;  --type=merge -p &amp;#39;{&amp;#34;spec&amp;#34;:{&amp;#34;refreshAfter&amp;#34;:&amp;#34;6m&amp;#34;}}&amp;#39;&#xA;&#xA;# confirm SecretSynced=True and a fresh lastTransitionTime, then revert&#xA;oc -n tfe patch vaultstaticsecret tfe-s3 \&#xA;  --type=merge -p &amp;#39;{&amp;#34;spec&amp;#34;:{&amp;#34;refreshAfter&amp;#34;:&amp;#34;5m&amp;#34;}}&amp;#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There&amp;rsquo;s a related but distinct issue on TFE&amp;rsquo;s database credentials, which&#xA;come from a &lt;code&gt;VaultDynamicSecret&lt;/code&gt; rather than a &lt;code&gt;VaultStaticSecret&lt;/code&gt;. VSO 1.4.0&#xA;doesn&amp;rsquo;t treat a static-role database response as renewable unless told&#xA;otherwise, so without one extra field it syncs once and never refetches —&#xA;meaning the next scheduled password rotation leaves TFE with a stale&#xA;password and failing Postgres logins:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;apiVersion: secrets.hashicorp.com/v1beta1&#xA;kind: VaultDynamicSecret&#xA;metadata:&#xA;  name: tfe-database&#xA;  namespace: tfe&#xA;spec:&#xA;  vaultAuthRef: tfe-auth&#xA;  mount: tfe-psql&#xA;  path: static-creds/tfe-static&#xA;  allowStaticCreds: true&#xA;  refreshAfter: 24h&#xA;  destination:&#xA;    name: tfe-database&#xA;    create: true&#xA;  rolloutRestartTargets:&#xA;    - kind: Deployment&#xA;      name: terraform-enterprise&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Two different VSO resource types, two different failure modes, two different&#xA;fixes — but both are now a standard part of the runbook rather than a&#xA;surprise, since between the Vault deployment and this one, the same bug&#xA;family has now hit three separate secrets.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Update (2026):&lt;/strong&gt; Digging in later reframed this. The stuck-reconciliation&#xA;part is a &lt;em&gt;low-probability race&lt;/em&gt; — it needs a VSO reconcile in flight during&#xA;the brief leader-election window — not a frequent recurrence, and it isn&amp;rsquo;t&#xA;fixed by upgrading VSO (the release after 1.4.0 is dependency/build-only, and&#xA;1.4.0 already ships the relevant &lt;code&gt;VaultStaticSecret&lt;/code&gt; client-callback handler).&#xA;I couldn&amp;rsquo;t reproduce it in six back-to-back forced elections. Full write-up in&#xA;the &lt;a href=&#34;https://www.404-code-not-found.com/posts/vault-on-openshift-hcp-autounseal/#bug-2-vso-stops-reconciling-after-a-transient-vault-ha-event&#34;&gt;Vault post&amp;rsquo;s Bug 2 update&lt;/a&gt;.&#xA;The spec-patch recovery above still applies.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-5-a-custom-job-agent-image-for-openshift&#34;&gt;Challenge 5: A Custom Job-Agent Image for OpenShift&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-problem-3&#34;&gt;The Problem&lt;/h3&gt;&#xA;&lt;p&gt;TFE runs each Terraform run in a short-lived &amp;ldquo;agent&amp;rdquo; container. The&#xA;stock &lt;code&gt;hashicorp/tfc-agent&lt;/code&gt; image fails admission under OpenShift&amp;rsquo;s&#xA;restricted security model — it expects to write to a working directory owned&#xA;by a fixed UID, which doesn&amp;rsquo;t hold up once OpenShift assigns the container an&#xA;arbitrary UID from the namespace&amp;rsquo;s allowed range.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-solution-3&#34;&gt;The Solution&lt;/h3&gt;&#xA;&lt;p&gt;HashiCorp&amp;rsquo;s own OpenShift deployment guide covers this: build a thin wrapper&#xA;image that opens up the permissions the arbitrary UID needs. I wired it up as&#xA;an OpenShift &lt;code&gt;BuildConfig&lt;/code&gt; that layers a couple of &lt;code&gt;chmod&lt;/code&gt; calls onto the&#xA;upstream image:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;apiVersion: build.openshift.io/v1&#xA;kind: BuildConfig&#xA;metadata:&#xA;  name: tfc-agent&#xA;  namespace: tfe-agents&#xA;spec:&#xA;  output:&#xA;    to:&#xA;      kind: ImageStreamTag&#xA;      name: tfc-agent:latest&#xA;  source:&#xA;    type: Dockerfile&#xA;    dockerfile: |&#xA;      FROM docker.io/hashicorp/tfc-agent:latest&#xA;      USER root&#xA;      RUN mkdir /.tfc-agent &amp;amp;&amp;amp; \&#xA;          chmod og+rw /.tfc-agent &amp;amp;&amp;amp; \&#xA;          chmod o+rx /home/tfc-agent&#xA;      USER tfc-agent&#xA;  strategy:&#xA;    type: Docker&#xA;    dockerStrategy:&#xA;      from:&#xA;        kind: DockerImage&#xA;        name: docker.io/hashicorp/tfc-agent:latest&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;TFE points at the built image via &lt;code&gt;TFE_RUN_PIPELINE_IMAGE&lt;/code&gt;, referencing the&#xA;in-cluster image registry rather than Docker Hub directly. Re-running&#xA;&lt;code&gt;oc start-build tfc-agent&lt;/code&gt; picks up new upstream releases of &lt;code&gt;tfc-agent&lt;/code&gt;&#xA;without needing to restart TFE itself — the new image is used the next time&#xA;an agent pod spawns.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;a-few-smaller-landmines&#34;&gt;A Few Smaller Landmines&lt;/h2&gt;&#xA;&lt;p&gt;Two more things that didn&amp;rsquo;t warrant a full section but are worth knowing&#xA;before you deploy TFE on OpenShift yourself:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;The chart hard-codes its own resource names.&lt;/strong&gt; The Deployment and Service&#xA;are both named the literal string &lt;code&gt;terraform-enterprise&lt;/code&gt;, regardless of&#xA;your Helm release name — &lt;code&gt;nameOverride&lt;/code&gt;/&lt;code&gt;fullnameOverride&lt;/code&gt; exist in the&#xA;chart&amp;rsquo;s helpers but the main templates never use them. Anything that needs&#xA;to reference TFE by name (VSO&amp;rsquo;s &lt;code&gt;rolloutRestartTargets&lt;/code&gt;, a Route&amp;rsquo;s&#xA;&lt;code&gt;to.name&lt;/code&gt;) has to use &lt;code&gt;terraform-enterprise&lt;/code&gt;, not your release name. The&#xA;Service&amp;rsquo;s port is also named &lt;code&gt;https-port&lt;/code&gt;, not the more conventional&#xA;&lt;code&gt;https&lt;/code&gt; — a Route with the wrong &lt;code&gt;targetPort&lt;/code&gt; gives a silent 503 rather&#xA;than an obvious error.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The chart defaults to a &lt;code&gt;LoadBalancer&lt;/code&gt; Service.&lt;/strong&gt; On a bare-metal cluster&#xA;with no LoadBalancer provider, that sits at &lt;code&gt;EXTERNAL-IP: &amp;lt;pending&amp;gt;&lt;/code&gt;&#xA;forever and keeps ArgoCD&amp;rsquo;s health check stuck on &lt;code&gt;Progressing&lt;/code&gt;. Overriding&#xA;to &lt;code&gt;service.type: ClusterIP&lt;/code&gt; and fronting it with an OpenShift Route&#xA;clears this up immediately.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;Running TFE active-active on OpenShift is straightforward once you&amp;rsquo;ve hit&#xA;each of these once, but the documentation doesn&amp;rsquo;t cover any of them:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Active-active means lockstep versions&lt;/strong&gt; — &lt;code&gt;RollingUpdate&lt;/code&gt; will crash-loop&#xA;a mixed-version rollout every time; &lt;code&gt;strategy: Recreate&lt;/code&gt; is the only&#xA;correct answer, at the cost of a brief outage on every deploy&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Dynamic Vault roles don&amp;rsquo;t fit every app&lt;/strong&gt; — TFE&amp;rsquo;s stable-username,&#xA;owns-the-schema requirement needed a static role layered onto the same&#xA;database connection, with a one-time manual ownership handoff&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;In-cluster S3 needs in-cluster trust&lt;/strong&gt; — mount OpenShift&amp;rsquo;s own&#xA;&lt;code&gt;openshift-service-ca.crt&lt;/code&gt; ConfigMap if you&amp;rsquo;re pointing TFE at NooBaa&#xA;instead of a cloud S3 endpoint&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The VSO stuck-reconciliation bug isn&amp;rsquo;t Vault-specific&lt;/strong&gt; — it hit TFE&amp;rsquo;s S3&#xA;and Redis secrets independently, both times triggered by unrelated Vault&#xA;HA events; the spec-patch workaround is now a standing runbook step&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;OpenShift&amp;rsquo;s restricted SCC reaches into the job agent, too&lt;/strong&gt; — not just&#xA;the TFE pods themselves; budget time for the custom &lt;code&gt;tfc-agent&lt;/code&gt; image build&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Between this post and the Vault one, that&amp;rsquo;s the full HashiCorp-on-OpenShift&#xA;stack this lab runs — Vault as the secrets backend, VSO as the sync layer,&#xA;and TFE consuming both. Every failure mode above came from the same root&#xA;cause in different clothes: OpenShift&amp;rsquo;s stricter defaults (SCCs, arbitrary&#xA;UIDs, in-cluster CAs) and TFE&amp;rsquo;s own strict operational assumptions (lockstep&#xA;versions, stable database identity) both push back against defaults that&#xA;work fine on vanilla Kubernetes.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>Running Vault on OpenShift with HCP Vault Auto-Unseal: Lessons Learned</title>
      <link>https://www.404-code-not-found.com/posts/vault-on-openshift-hcp-autounseal/</link>
      <pubDate>Thu, 11 Jun 2026 08:00:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/vault-on-openshift-hcp-autounseal/</guid>
      <category>vault</category>
      <category>openshift</category>
      <category>kubernetes</category>
      <category>hcp</category>
      <category>terraform</category>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;I recently set up a three-node Vault Enterprise HA cluster on OpenShift, using&#xA;HCP Vault as the auto-unseal provider via the transit secrets engine. On paper&#xA;this is a straightforward combination of well-documented features. In practice,&#xA;it was a series of traps — some subtle, some spectacular — that took multiple&#xA;sessions to fully work through.&lt;/p&gt;&#xA;&lt;p&gt;This post covers the four main challenge areas I hit: getting &lt;code&gt;IPC_LOCK&lt;/code&gt; right&#xA;on OpenShift, wiring up the auto-unseal token flow securely, managing Raft&#xA;quorum safely during rolling updates, and working around a reconciliation bug in&#xA;Vault Secrets Operator. I&amp;rsquo;ll focus on what caught me off guard and what the&#xA;correct solution looks like.&lt;/p&gt;&#xA;&lt;p&gt;The deployment is GitOps-managed via ArgoCD using a three-source Helm pattern:&#xA;the upstream Vault chart, a values file from the repo, and raw manifests for&#xA;cluster-level resources (SCCs, Routes, ConfigMaps) that Helm can&amp;rsquo;t cleanly&#xA;own.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-1-ipc_lock-on-openshift&#34;&gt;Challenge 1: IPC_LOCK on OpenShift&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-problem&#34;&gt;The Problem&lt;/h3&gt;&#xA;&lt;p&gt;Vault requires the &lt;code&gt;IPC_LOCK&lt;/code&gt; Linux capability so it can call &lt;code&gt;mlockall()&lt;/code&gt; to&#xA;prevent secrets from being swapped to disk. This is non-negotiable — if the&#xA;capability is missing, Vault exits at startup. On OpenShift this runs into the&#xA;default &lt;code&gt;restricted-v2&lt;/code&gt; Security Context Constraint (SCC), which doesn&amp;rsquo;t&#xA;include &lt;code&gt;IPC_LOCK&lt;/code&gt; in its allowed capabilities.&lt;/p&gt;&#xA;&lt;p&gt;I discovered this the hard way when all three pods went into &lt;code&gt;CrashLoopBackOff&lt;/code&gt;&#xA;immediately after deploying. What made it more confusing was that the error&#xA;message differed depending on the image:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Upstream image&lt;/strong&gt; (&lt;code&gt;docker.io/hashicorp/vault-enterprise&lt;/code&gt;): Vault starts,&#xA;calls &lt;code&gt;mlockall()&lt;/code&gt;, gets &lt;code&gt;EPERM&lt;/code&gt;, and exits with:&#xA;&lt;pre&gt;&lt;code&gt;Vault requires the IPC_LOCK capability&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Red Hat partner image&lt;/strong&gt; (&lt;code&gt;registry.connect.redhat.com/hashicorp/vault-enterprise&lt;/code&gt;):&#xA;The binary ships with &lt;code&gt;cap_ipc_lock+ep&lt;/code&gt; set via &lt;code&gt;setcap&lt;/code&gt;. When the kernel&#xA;drops that from the bounding set at exec time (because the SCC doesn&amp;rsquo;t allow&#xA;it), the &lt;code&gt;exec()&lt;/code&gt; itself fails before Vault even starts:&#xA;&lt;pre&gt;&lt;code&gt;exec: /usr/bin/vault: operation not permitted&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;Both root causes are the same — no &lt;code&gt;IPC_LOCK&lt;/code&gt; in the SCC — but the symptoms&#xA;look completely different, which costs time when debugging.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-solution-custom-scc&#34;&gt;The Solution: Custom SCC&lt;/h3&gt;&#xA;&lt;p&gt;The fix is a custom SCC that adds &lt;code&gt;IPC_LOCK&lt;/code&gt; as both an allowed and default&#xA;capability:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;apiVersion: security.openshift.io/v1&#xA;kind: SecurityContextConstraints&#xA;metadata:&#xA;  name: vault-ipc-lock&#xA;  annotations:&#xA;    kubernetes.io/description: &amp;gt;&#xA;      Extends restricted-v2 to allow IPC_LOCK for Vault mlock support.&#xA;allowPrivilegeEscalation: false&#xA;allowPrivilegedContainer: false&#xA;allowedCapabilities:&#xA;  - IPC_LOCK&#xA;defaultAddCapabilities:&#xA;  - IPC_LOCK&#xA;fsGroup:&#xA;  type: MustRunAs&#xA;readOnlyRootFilesystem: false&#xA;runAsUser:&#xA;  type: MustRunAsRange&#xA;seLinuxContext:&#xA;  type: MustRunAs&#xA;seccompProfiles:&#xA;  - runtime/default&#xA;supplementalGroups:&#xA;  type: RunAsAny&#xA;volumes:&#xA;  - configMap&#xA;  - downwardAPI&#xA;  - emptyDir&#xA;  - ephemeral&#xA;  - persistentVolumeClaim&#xA;  - projected&#xA;  - secret&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Custom SCCs don&amp;rsquo;t automatically get a &lt;code&gt;ClusterRole&lt;/code&gt; the way built-in ones do,&#xA;so you also need to create one manually and bind it to the Vault service account:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;---&#xA;apiVersion: rbac.authorization.k8s.io/v1&#xA;kind: ClusterRole&#xA;metadata:&#xA;  name: system:openshift:scc:vault-ipc-lock&#xA;rules:&#xA;  - apiGroups: [&amp;#34;security.openshift.io&amp;#34;]&#xA;    resources: [&amp;#34;securitycontextconstraints&amp;#34;]&#xA;    resourceNames: [&amp;#34;vault-ipc-lock&amp;#34;]&#xA;    verbs: [&amp;#34;use&amp;#34;]&#xA;---&#xA;apiVersion: rbac.authorization.k8s.io/v1&#xA;kind: RoleBinding&#xA;metadata:&#xA;  name: vault-ipc-lock&#xA;  namespace: vault&#xA;roleRef:&#xA;  apiGroup: rbac.authorization.k8s.io&#xA;  kind: ClusterRole&#xA;  name: system:openshift:scc:vault-ipc-lock&#xA;subjects:&#xA;  - kind: ServiceAccount&#xA;    name: vault&#xA;    namespace: vault&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id=&#34;the-scc-priority-gotcha&#34;&gt;The SCC Priority Gotcha&lt;/h3&gt;&#xA;&lt;p&gt;Here&amp;rsquo;s the non-obvious part. When multiple SCCs have the same priority,&#xA;OpenShift&amp;rsquo;s SCC resolver picks one based on a deterministic but non-obvious&#xA;ordering algorithm. In my cluster, a pre-existing &lt;code&gt;odf-blackbox-scc&lt;/code&gt; had the&#xA;same default priority of 10. Without an explicit hint in the pod spec, the&#xA;init container landed on &lt;code&gt;odf-blackbox-scc&lt;/code&gt; and failed — despite the&#xA;&lt;code&gt;vault&lt;/code&gt; service account being bound to my new SCC.&lt;/p&gt;&#xA;&lt;p&gt;The fix is to explicitly declare &lt;code&gt;IPC_LOCK&lt;/code&gt; in the container&amp;rsquo;s &lt;code&gt;securityContext&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;# In the Vault Helm values.yaml&#xA;server:&#xA;  extraContainers: []&#xA;  statefulSet:&#xA;    securityContext:&#xA;      container:&#xA;        capabilities:&#xA;          add:&#xA;            - IPC_LOCK&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This hint steers the SCC resolver to select &lt;code&gt;vault-ipc-lock&lt;/code&gt; over any&#xA;same-priority SCC that doesn&amp;rsquo;t advertise &lt;code&gt;IPC_LOCK&lt;/code&gt; support. The hint is&#xA;required even when &lt;code&gt;defaultAddCapabilities&lt;/code&gt; is set — without it, the resolver&#xA;may never reach your SCC.&lt;/p&gt;&#xA;&lt;h3 id=&#34;image-choice&#34;&gt;Image Choice&lt;/h3&gt;&#xA;&lt;p&gt;I also switched from the Red Hat partner image to the upstream&#xA;&lt;code&gt;docker.io/hashicorp/vault-enterprise:2.0.1-ent&lt;/code&gt;. Without the &lt;code&gt;docker.io/&lt;/code&gt;&#xA;prefix, RHCOS short-name rewriting rewrites unqualified image tags to&#xA;&lt;code&gt;registry.connect.redhat.com/...&lt;/code&gt; at pull time. That&amp;rsquo;s fine in theory, but in&#xA;practice the partner mirror had divergent cached bytes across nodes (different&#xA;&lt;code&gt;sha256&lt;/code&gt; digests for the same tag) and was using a floating &lt;code&gt;2.0-ent&lt;/code&gt; tag&#xA;rather than a pinned version. Once the SCC was in place, the upstream image&#xA;with an explicit registry and pinned tag worked cleanly.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-2-auto-unseal-token-flow&#34;&gt;Challenge 2: Auto-Unseal Token Flow&lt;/h2&gt;&#xA;&lt;h3 id=&#34;why-hcp-vault-dedicated&#34;&gt;Why HCP Vault Dedicated?&lt;/h3&gt;&#xA;&lt;p&gt;Before getting into the mechanics, it&amp;rsquo;s worth explaining the choice of HCP&#xA;Vault as the transit unseal provider rather than a self-hosted instance.&lt;/p&gt;&#xA;&lt;p&gt;The transit auto-unseal mechanism doesn&amp;rsquo;t require HCP Vault — any Vault cluster&#xA;(including Community Edition) can host a transit secrets engine and serve as the&#xA;unseal provider. The problem is the bootstrap paradox: if your transit unseal&#xA;cluster is also self-hosted, you need a way to unseal &lt;em&gt;it&lt;/em&gt; before it can unseal&#xA;anything else. You&amp;rsquo;ve just moved the problem one level up.&lt;/p&gt;&#xA;&lt;p&gt;HCP Vault Dedicated sidesteps this entirely. HashiCorp operates the cluster,&#xA;handles HA failover, applies upgrades, and manages the underlying infrastructure.&#xA;Critically, it uses cloud KMS for its own unsealing, so there&amp;rsquo;s no chicken-and-egg&#xA;problem — it&amp;rsquo;s always available when your OpenShift pods start up.&lt;/p&gt;&#xA;&lt;p&gt;For a use case like this — a single transit key used only at pod startup — the&#xA;&lt;a href=&#34;https://developer.hashicorp.com/hcp/docs/vault/get-started/deployment-considerations/tiers-and-features&#34; target=&#34;_blank&#34;&gt;Development tier&lt;/a&gt;&#xA;is sufficient. It&amp;rsquo;s a single-node cluster (no SLA, fine for a non-critical&#xA;dependency like this one), capped at 25 clients, and costs &lt;strong&gt;$0.03/hour&lt;/strong&gt; —&#xA;roughly &lt;strong&gt;$21.60/month&lt;/strong&gt;. That&amp;rsquo;s a low price for eliminating the unseal&#xA;bootstrapping problem entirely and offloading upgrades and operations to&#xA;HashiCorp.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-architecture&#34;&gt;The Architecture&lt;/h3&gt;&#xA;&lt;p&gt;Vault&amp;rsquo;s transit auto-unseal requires a token with encrypt/decrypt access to an&#xA;HCP Vault transit key. On every pod startup, Vault needs that token to be&#xA;available before it can unseal. The token can&amp;rsquo;t live in a static Kubernetes&#xA;Secret (that would be a long-lived credential), so it needs to be minted fresh&#xA;at startup using the pod&amp;rsquo;s service account JWT.&lt;/p&gt;&#xA;&lt;p&gt;The flow is:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;OpenShift projects each pod&amp;rsquo;s service account token to&#xA;&lt;code&gt;/var/run/secrets/kubernetes.io/serviceaccount/token&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;An init container uses that JWT to authenticate to HCP Vault&amp;rsquo;s JWT auth&#xA;method (&lt;code&gt;POST /v1/auth/jwt/login&lt;/code&gt;)&lt;/li&gt;&#xA;&lt;li&gt;On successful auth, it mints a short-lived child token (24h TTL, &lt;code&gt;no_parent&lt;/code&gt;&#xA;so it&amp;rsquo;s an orphan, &lt;code&gt;autounseal&lt;/code&gt; policy)&lt;/li&gt;&#xA;&lt;li&gt;It renders the transit seal stanza with that token to a shared &lt;code&gt;emptyDir&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;The main Vault container reads that stanza via &lt;code&gt;vault server -config=...&lt;/code&gt;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;HCP Vault&amp;rsquo;s JWT auth method is configured with:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;bound_audiences: &amp;quot;https://kubernetes.default.svc&amp;quot;&lt;/code&gt; — matches the audience&#xA;in the SA token&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;bound_subject: &amp;quot;system:serviceaccount:vault:vault&amp;quot;&lt;/code&gt; — locks it to&#xA;exactly this service account&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;jwks_url&lt;/code&gt; pointing to OpenShift&amp;rsquo;s OIDC JWKS endpoint&lt;/li&gt;&#xA;&lt;li&gt;The cluster CA cert for TLS verification&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;This gives each pod a fresh, per-pod-bound token that&amp;rsquo;s useless to any other&#xA;workload. It&amp;rsquo;s about as tight as you can make a startup credential.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-evolution-curl-to-vault-agent&#34;&gt;The Evolution: curl to Vault Agent&lt;/h3&gt;&#xA;&lt;p&gt;My first implementation used a curl+bash init container — about 50 lines of&#xA;shell inline in &lt;code&gt;values.yaml&lt;/code&gt;. It worked but had several problems:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;JSON parsing with &lt;code&gt;jq&lt;/code&gt; and &lt;code&gt;grep&lt;/code&gt; with fragile exit code handling&lt;/li&gt;&#xA;&lt;li&gt;The rendered &lt;code&gt;seal.hcl&lt;/code&gt; had the token in plaintext on disk (acceptable but&#xA;not great)&lt;/li&gt;&#xA;&lt;li&gt;Silent failure modes: if the token-create call failed, the shell would&#xA;continue and render an empty &lt;code&gt;seal.hcl&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Hard to maintain and test&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;I replaced it with a declarative Vault Agent running in one-shot mode&#xA;(&lt;code&gt;exit_after_auth = true&lt;/code&gt;). The agent config lives in a ConfigMap:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-hcl&#34; data-lang=&#34;hcl&#34;&gt;# vault-agent-config.yaml&#xA;vault {&#xA;  address = &amp;#34;https://vault.hashicorp.cloud:8200&amp;#34;&#xA;&#xA;  tls {&#xA;    ca_cert = &amp;#34;/vault/userconfig/hcp-ca/ca.crt&amp;#34;&#xA;  }&#xA;}&#xA;&#xA;auto_auth {&#xA;  method &amp;#34;jwt&amp;#34; {&#xA;    namespace = &amp;#34;admin/my-org&amp;#34;&#xA;    mount_path = &amp;#34;auth/jwt&amp;#34;&#xA;    config {&#xA;      path       = &amp;#34;/var/run/secrets/kubernetes.io/serviceaccount/token&amp;#34;&#xA;      role       = &amp;#34;autounseal-token&amp;#34;&#xA;    }&#xA;  }&#xA;  sink &amp;#34;file&amp;#34; {&#xA;    config {&#xA;      path = &amp;#34;/tmp/vault-agent-token&amp;#34;&#xA;    }&#xA;  }&#xA;}&#xA;&#xA;template {&#xA;  contents = &amp;lt;&amp;lt;EOT&#xA;{{- with secret &amp;#34;auth/token/create&amp;#34; &amp;#34;policies=autounseal&amp;#34; &amp;#34;period=24h&amp;#34; &amp;#34;no_parent=true&amp;#34; &amp;#34;orphan=true&amp;#34; -}}&#xA;seal &amp;#34;transit&amp;#34; {&#xA;  address     = &amp;#34;https://vault.hashicorp.cloud:8200&amp;#34;&#xA;  namespace   = &amp;#34;admin/my-org&amp;#34;&#xA;  token       = &amp;#34;{{ .Auth.ClientToken }}&amp;#34;&#xA;  key_name    = &amp;#34;autounseal&amp;#34;&#xA;  mount_path  = &amp;#34;transit&amp;#34;&#xA;  tls_ca_cert = &amp;#34;/vault/userconfig/hcp-ca/ca.crt&amp;#34;&#xA;}&#xA;{{- end -}}&#xA;EOT&#xA;  destination = &amp;#34;/vault/userconfig/vault-config/seal.hcl&amp;#34;&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The agent logs clearly report whether JWT auth succeeded and whether the&#xA;template rendered — a significant improvement over silent shell failures. The&#xA;&lt;code&gt;exit_after_auth = true&lt;/code&gt; flag makes it act as a one-shot runner rather than a&#xA;persistent sidecar.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-command-override-gotcha&#34;&gt;The &lt;code&gt;command:&lt;/code&gt; Override Gotcha&lt;/h3&gt;&#xA;&lt;p&gt;One subtle issue: if you override &lt;code&gt;command:&lt;/code&gt; in the init container spec, you&#xA;bypass the image&amp;rsquo;s &lt;code&gt;docker-entrypoint.sh&lt;/code&gt;. That entrypoint runs a&#xA;&lt;code&gt;setcap -r /usr/bin/vault&lt;/code&gt; cap-clearing step before execing the binary —&#xA;important because the partner image ships with &lt;code&gt;cap_ipc_lock+ep&lt;/code&gt; baked into the&#xA;binary&amp;rsquo;s extended attributes.&lt;/p&gt;&#xA;&lt;p&gt;The fix is to only set &lt;code&gt;args:&lt;/code&gt; (e.g., &lt;code&gt;[&amp;quot;agent&amp;quot;, &amp;quot;-config=/path/to/agent.hcl&amp;quot;]&lt;/code&gt;)&#xA;and leave &lt;code&gt;command:&lt;/code&gt; unset. The entrypoint handles the rest.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-3-raft-quorum-during-rolling-updates&#34;&gt;Challenge 3: Raft Quorum During Rolling Updates&lt;/h2&gt;&#xA;&lt;h3 id=&#34;the-ondelete-trap&#34;&gt;The OnDelete Trap&lt;/h3&gt;&#xA;&lt;p&gt;The Vault Helm chart sets &lt;code&gt;updateStrategy: OnDelete&lt;/code&gt; on the StatefulSet by&#xA;default. This means ArgoCD applying a new chart revision stages the change in a&#xA;new &lt;code&gt;controllerRevision&lt;/code&gt; but does &lt;strong&gt;not&lt;/strong&gt; restart any pods. You have to manually&#xA;delete each pod to trigger the rollout.&lt;/p&gt;&#xA;&lt;p&gt;The danger is that multiple staged-but-unrolled revisions can accumulate. If&#xA;you pushed three separate changes without rolling, deleting a pod applies all&#xA;three at once. Always check &lt;code&gt;oc get statefulset vault -o json | jq &#39;.status | {currentRevision, updateRevision}&#39;&lt;/code&gt; before starting a rollout.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-voter-vs-follower-distinction&#34;&gt;The Voter vs. Follower Distinction&lt;/h3&gt;&#xA;&lt;p&gt;When a Vault pod restarts and rejoins the Raft cluster, it goes through a&#xA;stabilization window (default 10 seconds) before Raft&amp;rsquo;s autopilot promotes it&#xA;to a full voter. During this window, the pod is a non-voter — it&amp;rsquo;s replicating&#xA;and appears healthy, but it doesn&amp;rsquo;t count toward quorum.&lt;/p&gt;&#xA;&lt;p&gt;I learned this the hard way. A 3-node cluster needs 2 voters for quorum. I&#xA;bounced pod 0, saw it come up &lt;code&gt;1/1 Ready&lt;/code&gt; with&#xA;&lt;code&gt;replication.standby: stream started&lt;/code&gt;, assumed it was healthy, and immediately&#xA;deleted pod 1. Pod 1 went down before pod 0 achieved voter status, dropping the&#xA;active voter count to 1 — below quorum — causing a brief write outage until&#xA;things stabilized.&lt;/p&gt;&#xA;&lt;p&gt;&lt;code&gt;1/1 Ready&lt;/code&gt; does not mean voter. Always verify:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Check who the current leader is (unauthenticated)&#xA;oc -n vault exec vault-0 -- \&#xA;  wget -qO- http://127.0.0.1:8200/v1/sys/leader | jq .&#xA;&#xA;# Verify the just-rolled pod is a voter before touching the next&#xA;VAULT_TOKEN=&amp;lt;root-token&amp;gt; vault operator raft list-peers&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Look for &lt;code&gt;Voter: true&lt;/code&gt; on the pod you just rolled before deleting the next one.&lt;/p&gt;&#xA;&lt;h3 id=&#34;the-correct-rollout-order&#34;&gt;The Correct Rollout Order&lt;/h3&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Identify the current leader at rollout time (don&amp;rsquo;t assume it&amp;rsquo;s pod 0 — it&#xA;could have changed since the last restart)&lt;/li&gt;&#xA;&lt;li&gt;Delete a follower and wait for &lt;code&gt;1/1 Ready&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; &lt;code&gt;Voter: true&lt;/code&gt; in&#xA;&lt;code&gt;raft list-peers&lt;/code&gt;&lt;/li&gt;&#xA;&lt;li&gt;Repeat for the remaining follower&lt;/li&gt;&#xA;&lt;li&gt;Delete the leader last — it triggers re-election to one of the now-healthy&#xA;standbys before the leader pod restarts&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;p&gt;This sequence ensures quorum is never threatened.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;challenge-4-vso-reconciliation-bugs&#34;&gt;Challenge 4: VSO Reconciliation Bugs&lt;/h2&gt;&#xA;&lt;p&gt;I&amp;rsquo;m using Vault Secrets Operator (VSO) 1.4.0 to sync Vault secrets into&#xA;Kubernetes Secrets. It hit two distinct bugs during this deployment.&lt;/p&gt;&#xA;&lt;h3 id=&#34;bug-1-static-database-credentials-not-refreshing&#34;&gt;Bug 1: Static Database Credentials Not Refreshing&lt;/h3&gt;&#xA;&lt;p&gt;Vault was rotating the Terraform Enterprise database password weekly via the&#xA;database secrets engine&amp;rsquo;s static role. VSO was supposed to detect the rotation&#xA;and update the Kubernetes Secret — but it wasn&amp;rsquo;t. TFE pods started failing with&#xA;&lt;code&gt;SQLSTATE 28P01&lt;/code&gt; (authentication failure) after each rotation.&lt;/p&gt;&#xA;&lt;p&gt;VSO&amp;rsquo;s logs showed the reason:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;Vault secret does not support periodic renewal/refresh via reconciliation&#xA;horizon: 0&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;VSO 1.4.0 doesn&amp;rsquo;t treat static-creds responses as renewable by default. The&#xA;fix is one field on the &lt;code&gt;VaultDynamicSecret&lt;/code&gt;:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;spec:&#xA;  allowStaticCreds: true&#xA;  refreshAfter: 24h&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;allowStaticCreds: true&lt;/code&gt; tells VSO to use the static-creds rotation tracker and&#xA;honor the &lt;code&gt;rotation_period&lt;/code&gt; configured in Vault. &lt;code&gt;refreshAfter&lt;/code&gt; is a safety net&#xA;in case the rotation tracker misses an event.&lt;/p&gt;&#xA;&lt;h3 id=&#34;bug-2-vso-stops-reconciling-after-a-transient-vault-ha-event&#34;&gt;Bug 2: VSO Stops Reconciling After a Transient Vault HA Event&lt;/h3&gt;&#xA;&lt;p&gt;After Vault HA events (leader re-election, pod restarts), VSO occasionally&#xA;emits 500 errors like:&lt;/p&gt;&#xA;&lt;pre&gt;&lt;code&gt;local node not active but active cluster node not found&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This is transient and expected — it resolves once the new leader is fully&#xA;established. The problem is that VSO 1.4.0 records the failure and then stops&#xA;requeuing the affected &lt;code&gt;VaultStaticSecret&lt;/code&gt;. Days later, the secret is stale and&#xA;VSO&amp;rsquo;s controller shows zero log entries for it since the failure event.&lt;/p&gt;&#xA;&lt;p&gt;Restarting the VSO controller pod alone is not sufficient — it re-initializes&#xA;but doesn&amp;rsquo;t automatically re-pick stuck resources.&lt;/p&gt;&#xA;&lt;p&gt;The fix is to touch the VSS spec to force a requeue:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Temporarily change refreshAfter to trigger the watcher&#xA;oc -n &amp;lt;namespace&amp;gt; patch vaultstaticsecret &amp;lt;name&amp;gt; \&#xA;  --type=merge -p &amp;#39;{&amp;#34;spec&amp;#34;:{&amp;#34;refreshAfter&amp;#34;:&amp;#34;6m&amp;#34;}}&amp;#39;&#xA;&#xA;# Verify reconciliation fires, then revert to original value&#xA;oc -n &amp;lt;namespace&amp;gt; patch vaultstaticsecret &amp;lt;name&amp;gt; \&#xA;  --type=merge -p &amp;#39;{&amp;#34;spec&amp;#34;:{&amp;#34;refreshAfter&amp;#34;:&amp;#34;5m&amp;#34;}}&amp;#39;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note: the &lt;code&gt;vso.secrets.hashicorp.com/refresh&lt;/code&gt; annotation documented for&#xA;triggering immediate reconciliation works for &lt;code&gt;VaultDynamicSecret&lt;/code&gt; but &lt;strong&gt;not&lt;/strong&gt;&#xA;&lt;code&gt;VaultStaticSecret&lt;/code&gt;. Only the spec-patch method reliably works for the latter.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Update (2026):&lt;/strong&gt; After some follow-up questions about whether this was still a&#xA;problem, I dug in further — and the picture is more nuanced than &amp;ldquo;VSO gives up&#xA;too early.&amp;rdquo;&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;It&amp;rsquo;s a low-probability race, not a timeout.&lt;/strong&gt; The stuck state only happens&#xA;when a reconcile is in flight &lt;em&gt;during&lt;/em&gt; the few-second leader-election window and&#xA;lands on a node still returning the 500. With a multi-minute &lt;code&gt;refreshAfter&lt;/code&gt;,&#xA;that overlap is rare — which is exactly why it&amp;rsquo;s hard to reproduce on demand. I&#xA;only caught it originally because it happened opportunistically across weeks of&#xA;real rollouts.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;Upgrading does not fix it.&lt;/strong&gt; I assumed a later VSO release must have addressed&#xA;this, but the release after 1.4.0 is dependency/build-only with no reconciler&#xA;changes, and 1.4.0 already ships the &lt;code&gt;VaultStaticSecret&lt;/code&gt; client-callback handler&#xA;(&lt;a href=&#34;https://github.com/hashicorp/vault-secrets-operator/pull/867&#34; target=&#34;_blank&#34;&gt;GH-867&lt;/a&gt;). The&#xA;code path that sticks is present in both.&lt;/p&gt;&#xA;&lt;p&gt;&lt;strong&gt;I tried hard to reproduce it and couldn&amp;rsquo;t.&lt;/strong&gt; I stood up a throwaway&#xA;&lt;code&gt;VaultStaticSecret&lt;/code&gt; with &lt;code&gt;refreshAfter: 5s&lt;/code&gt; (so a reconcile is almost always in&#xA;flight) and forced six real leader elections back-to-back. It self-healed every&#xA;time — the 500 never even surfaced. Reliably reproducing it seems to need many&#xA;more election/reconcile overlaps, not a handful.&lt;/p&gt;&#xA;&lt;p&gt;The open question: does GH-867&amp;rsquo;s client-callback handler cover the &amp;ldquo;local node&#xA;not active&amp;rdquo; case &lt;em&gt;during&lt;/em&gt; an election, or only clean reconnect events? What I&#xA;saw suggests it doesn&amp;rsquo;t fully cover the election race. The spec-patch recovery&#xA;above still applies.&lt;/p&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;Running Vault on OpenShift is entirely doable and production-viable, but the&#xA;combination of OpenShift&amp;rsquo;s SCC model, Vault&amp;rsquo;s Raft consensus requirements, and&#xA;VSO&amp;rsquo;s edge cases means there are more failure modes than the documentation&#xA;covers. The main takeaways:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;IPC_LOCK needs an explicit SCC&lt;/strong&gt; — and the &lt;code&gt;securityContext.capabilities.add&lt;/code&gt;&#xA;hint is required to steer SCC selection when multiple SCCs have equal priority&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Qualify your image references&lt;/strong&gt; — &lt;code&gt;docker.io/hashicorp/vault-enterprise:2.0.1-ent&lt;/code&gt;&#xA;is unambiguous; bare &lt;code&gt;hashicorp/vault-enterprise&lt;/code&gt; is not&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Vault Agent beats curl+bash&lt;/strong&gt; — one-shot mode gives you declarative auth,&#xA;structured failure logging, and no shell fragility&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;&lt;code&gt;1/1 Ready&lt;/code&gt; is not &lt;code&gt;Voter: true&lt;/code&gt;&lt;/strong&gt; — wait for &lt;code&gt;raft list-peers&lt;/code&gt; before&#xA;rolling the next pod&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;VSO 1.4.0 has a stuck-VSS bug&lt;/strong&gt; — know the spec-patch workaround before&#xA;you need it in production&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;up-next-terraform-enterprise-on-openshift&#34;&gt;Up Next: Terraform Enterprise on OpenShift&lt;/h2&gt;&#xA;&lt;p&gt;The same lab cluster runs Terraform Enterprise in active-active mode alongside&#xA;Vault, and it has its own set of OpenShift-specific challenges worth a dedicated&#xA;post. The short preview:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;PostgreSQL via CloudNativePG&lt;/strong&gt; — TFE needs a PostgreSQL cluster with a&#xA;specific schema owner and connection string format. Getting the VSO-managed&#xA;credentials to rotate cleanly with TFE&amp;rsquo;s connection pool is where the&#xA;&lt;code&gt;allowStaticCreds&lt;/code&gt; fix from Challenge 4 above actually first surfaced.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;NooBaa S3 for object storage&lt;/strong&gt; — TFE uses S3-compatible storage for run&#xA;logs and state. OpenShift ships with NooBaa (via ODF) as an in-cluster S3&#xA;provider, which avoids a cloud dependency but requires matching TFE&amp;rsquo;s expected&#xA;bucket configuration and path-style addressing.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Redis for active-active coordination&lt;/strong&gt; — TFE&amp;rsquo;s active-active mode uses&#xA;Redis as its coordination layer. Running Redis on OpenShift under&#xA;&lt;code&gt;restricted-v2&lt;/code&gt; has its own SCC story.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;The same VSO stuck-reconciliation bug&lt;/strong&gt; — hits TFE&amp;rsquo;s Redis and S3 secrets&#xA;independently after any Vault HA event, so the spec-patch workaround becomes&#xA;a standard part of the Vault rollout runbook.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;If you&amp;rsquo;re planning a full HashiCorp stack on OpenShift, the Vault work above is&#xA;the prerequisite — get Vault stable and auto-unsealing before bringing TFE up,&#xA;since TFE uses Vault as its secrets backend from day one.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>Workstation Setup for Terraform</title>
      <link>https://www.404-code-not-found.com/posts/workstation-setup-for-terraform/</link>
      <pubDate>Fri, 31 Jan 2025 17:00:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/workstation-setup-for-terraform/</guid>
      <description>&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;In my last post, I covered how I set up&#xA;&lt;a href=&#34;https://www.404-code-not-found.com/posts/vscode-setup-for-terraform&#34;&gt;VSCode for Terraform&lt;/a&gt;. The second most&#xA;popular question is how I set up my workstation for Terraform development.&lt;/p&gt;&#xA;&lt;p&gt;As with most things in the tech world, there are many ways to do things. The&#xA;following is the way that works for me. I am always looking for ways to improve&#xA;my workflow, so if you have suggestions, please let me know.&lt;/p&gt;&#xA;&lt;p&gt;Additionally, I am a Mac user, so some of the tools I use are harder to use on&#xA;Windows. However, the tools can be used in WSL or DevContainers if you use&#xA;either of those tools.&lt;/p&gt;&#xA;&lt;p&gt;On a Mac, I use &lt;a href=&#34;https://brew.sh/&#34; target=&#34;_blank&#34;&gt;Homebrew&lt;/a&gt; to install my tools. Also, Since we&#xA;have to store all our code in a version control system, I installed the latest&#xA;version of &lt;code&gt;git&lt;/code&gt; with Homebrew.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Install Homebrew&#xA;/bin/bash -c &amp;#34;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)&amp;#34;&#xA;&#xA;# Install Git&#xA;brew install git&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;installing-terraform&#34;&gt;Installing Terraform&lt;/h2&gt;&#xA;&lt;p&gt;I have to use multiple versions of Terraform for different projects, so I use&#xA;&lt;a href=&#34;https://tfswitch.warrensbox.com/&#34; target=&#34;_blank&#34;&gt;TFSwitch&lt;/a&gt; to switch the version of Terraform&#xA;installed based on the directory I am in and the version constraint in my&#xA;Terraform configuration.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install warrensbox/tap/tfswitch&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I have configured &lt;code&gt;tfswitch&lt;/code&gt; to link the correct version of Terraform to&#xA;&lt;code&gt;~/bin/terraform&lt;/code&gt; as the &lt;code&gt;~/bin&lt;/code&gt; directory is in my &lt;code&gt;$PATH&lt;/code&gt;. This is done by&#xA;creating a &lt;code&gt;~/.tfswitch.toml&lt;/code&gt; file with the following contents:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-toml&#34; data-lang=&#34;toml&#34;&gt;bin=&amp;#34;~/bin/terraform&amp;#34;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I use the following &lt;code&gt;zsh&lt;/code&gt; function to load &lt;code&gt;tfswitch&lt;/code&gt; in a directory with&#xA;Terraform configuration files.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-zsh&#34; data-lang=&#34;zsh&#34;&gt;load-tfswitch() {&#xA;  local tfswitchrc_path=&amp;#34;${HOME}/.tfswitch.toml&amp;#34;&#xA;&#xA;  # if [[ -f &amp;#34;$tfswitchrc_path&amp;#34; ]] &amp;amp;&amp;amp; [[ -f &amp;#34;terraform.tf&amp;#34; ]]; then&#xA;  if [[ -f &amp;#34;$tfswitchrc_path&amp;#34; ]]; then&#xA;    if [[ $(ls -l *.tf | wc -l ) -gt 0 ]] 2&amp;gt; /dev/null; then&#xA;      tfswitch&#xA;    fi&#xA;  fi&#xA;}&#xA;&#xA;add-zsh-hook chpwd load-tfswitch&#xA;load-tfswitch&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now, if you don&amp;rsquo;t need to support multiple versions of Terraform, you can&#xA;install it with Homebrew from HashiCorp&amp;rsquo;s tap.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;  brew install hashicorp/tap/terraform&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Otherwise, you can download it directly from the&#xA;&lt;a href=&#34;https://developer.hashicorp.com/terraform/install&#34; target=&#34;_blank&#34;&gt;Terraform Install page&lt;/a&gt;.&lt;/p&gt;&#xA;&lt;h2 id=&#34;git-tools&#34;&gt;Git Tools&lt;/h2&gt;&#xA;&lt;p&gt;Storing your Terraform configuration in a version control system is a must. I&#xA;don&amp;rsquo;t do development daily, so I forget to format my code and check for secrets&#xA;or other vulnerabilities. One of the worst is that I forgot to update the&#xA;README.md with any variable or output changes. The most annoying thing is that I&#xA;forgot to create a new branch for my changes, as most of the git repos I work in&#xA;have the default branch protected, so I can&amp;rsquo;t push directly to it.&lt;/p&gt;&#xA;&lt;p&gt;To help me with all of these problems, I use&#xA;&lt;a href=&#34;https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks&#34; target=&#34;_blank&#34;&gt;Git Hooks&lt;/a&gt;,&#xA;specifically the &lt;code&gt;pre-commit&lt;/code&gt; hook to check for all of these things before I&#xA;commit my changes. If you are unfamiliar with Git Hooks, they are scripts that&#xA;run automatically when specific actions occur in a Git repository. You can use&#xA;these scripts to enforce coding standards, check for vulnerabilities, or even&#xA;run tests before you commit your changes.&lt;/p&gt;&#xA;&lt;p&gt;Traditionally, you would have to write these scripts yourself, but there is a&#xA;tool called &lt;a href=&#34;https://pre-commit.com/&#34; target=&#34;_blank&#34;&gt;pre-commit&lt;/a&gt; that makes it easy to install&#xA;and manage these hooks.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install pre-commit&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Once you have &lt;code&gt;pre-commit&lt;/code&gt; installed, you can create a &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt;&#xA;file in the root of your repository with the following contents:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;# yaml-language-server: $schema=https://json.schemastore.org/pre-commit-config.json&#xA;&#xA;repos:&#xA;  - repo: local&#xA;    hooks:&#xA;      - id: trufflehog&#xA;        name: TruffleHog&#xA;        description: Detect secrets in your data.&#xA;        entry: trufflehog git file://. --since-commit HEAD --fail&#xA;        language: golang&#xA;        pass_filenames: false&#xA;        stages: [&amp;#34;pre-commit&amp;#34;, &amp;#34;pre-push&amp;#34;]&#xA;  - repo: https://github.com/pre-commit/pre-commit-hooks&#xA;    rev: v5.0.0&#xA;    hooks:&#xA;      - id: check-merge-conflict&#xA;      - id: end-of-file-fixer&#xA;      - id: no-commit-to-branch&#xA;  - repo: https://github.com/antonbabenko/pre-commit-terraform&#xA;    rev: v1.97.0&#xA;    hooks:&#xA;      - id: terraform_fmt&#xA;      - id: terraform_validate&#xA;        args:&#xA;          - --hook-config=--retry-once-with-cleanup=true&#xA;      - id: terraform_docs&#xA;        args:&#xA;          - --args=--lockfile=false&#xA;      - id: terraform_tflint&#xA;        args:&#xA;          - --args=--config=__GIT_WORKING_DIR__/.tflint.hcl&#xA;      - id: terraform_checkov&#xA;      - id: infracost_breakdown&#xA;        args:&#xA;          - --args=--path=.&#xA;          - --args=--terraform-var-file=&amp;#34;terraform.tfvars&amp;#34;&#xA;        # verbose: true&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Every time you commit your changes, the &lt;code&gt;pre-commit&lt;/code&gt; tool runs and checks each&#xA;hook you have defined. If any hook fails, the commit will abort, and you must&#xA;fix the issues before committing your changes.&lt;/p&gt;&#xA;&lt;p&gt;We haven&amp;rsquo;t covered all of the hooks in the &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file, but&#xA;I&amp;rsquo;ll list the ones I use here:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;trufflehog&lt;/code&gt;: Scans your git repo for committed secrets 😱.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;check-merge-conflict&lt;/code&gt;: Checks for files that contain merge conflict strings.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;end-of-file-fixer&lt;/code&gt;: Ensures that files end with a newline.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;no-commit-to-branch&lt;/code&gt;: Prevents commits directly to a branch (default branch&#xA;in our case).&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform_fmt&lt;/code&gt;: Formats your Terraform code.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform_validate&lt;/code&gt;: Validates your Terraform code.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform_docs&lt;/code&gt;: Dynamically updates your README.md with information on your&#xA;module&amp;rsquo;s inputs, outputs, and requirements.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform_tflint&lt;/code&gt;: A Terraform linter that checks for best practices and&#xA;errors in your Terraform code.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform_checkov&lt;/code&gt;: A tool that checks your Terraform code for security&#xA;vulnerabilities.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;infracost_breakdown&lt;/code&gt;: Gives you a cost estimate for the cloud resources your&#xA;module would deploy.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;tools-needed-for-the-pre-commit-hooks-that-i-use&#34;&gt;Tools needed for the Pre-commit hooks that I use&lt;/h3&gt;&#xA;&lt;p&gt;You will need to install a few of these tools to use all of the &lt;code&gt;pre-commit&lt;/code&gt;&#xA;hooks that I have listed above.&lt;/p&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/trufflesecurity/trufflehog&#34; target=&#34;_blank&#34;&gt;Trufflehog&lt;/a&gt; scans your git repo&#xA;for secrets 😱. Doing this as a pre-commit hook lets you catch secrets before&#xA;committing them. This way, they don&amp;rsquo;t end up in your git history, keeping your&#xA;security team happy.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install trufflesecurity/trufflehog/trufflehog&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/terraform-docs/terraform-docs&#34; target=&#34;_blank&#34;&gt;Terraform-docs&lt;/a&gt; dynamically&#xA;updates your README.md with information on your module&amp;rsquo;s inputs, outputs, and&#xA;requirements.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install terraform-docs&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In your README.md file, you can add the following comments to have&#xA;&lt;code&gt;terraform-docs&lt;/code&gt; update the file.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-markdown&#34; data-lang=&#34;markdown&#34;&gt;&amp;lt;!-- BEGIN_TF_DOCS --&amp;gt;&#xA;&amp;lt;!-- END_TF_DOCS --&amp;gt;&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/infracost/infracost&#34; target=&#34;_blank&#34;&gt;Infracost&lt;/a&gt; provides a cost estimate for&#xA;the cloud resources on which your configuration will be deployed.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install infracost&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/stedolan/jq&#34; target=&#34;_blank&#34;&gt;Jq&lt;/a&gt; is a lightweight and flexible command-line&#xA;JSON processor. required for &lt;code&gt;terraform_validate&lt;/code&gt; with&#xA;&lt;code&gt;--retry-once-with-cleanup&lt;/code&gt; flag, and for &lt;code&gt;infracost_breakdown&lt;/code&gt; hook.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install jq&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/terraform-linters/tflint&#34; target=&#34;_blank&#34;&gt;TFLint&lt;/a&gt; is a Terraform linter that&#xA;checks for best practices and errors in your Terraform code.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install tflint&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;a href=&#34;https://github.com/bridgecrewio/checkov&#34; target=&#34;_blank&#34;&gt;Checkov&lt;/a&gt; is a static code analysis&#xA;tool for infrastructure as code (IaC).&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install checkov&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id=&#34;convenience-tools&#34;&gt;Convenience Tools&lt;/h2&gt;&#xA;&lt;p&gt;With Terraform installed via &lt;code&gt;tfswitch&lt;/code&gt; and the &lt;code&gt;pre-commit&lt;/code&gt; hooks setup, I have&#xA;a solid foundation for my Terraform development workflow. But I use a few more&#xA;tools to make my life easier.&lt;/p&gt;&#xA;&lt;p&gt;First, I use &lt;a href=&#34;https://1password.com/&#34; target=&#34;_blank&#34;&gt;1Password&lt;/a&gt; to store my secrets, API keys,&#xA;and passwords. I also use the&#xA;&lt;a href=&#34;https://1password.com/downloads/command-line/&#34; target=&#34;_blank&#34;&gt;1Password CLI&lt;/a&gt; to access my&#xA;secrets via environment variables and shell scripts.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install 1password 1password-cli&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;With 1Password and the 1Password CLI installed, I can access my secrets via the&#xA;&lt;code&gt;op&lt;/code&gt; command.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;export TFE_TOKEN=$(op read --cache &amp;#34;op://Vault/Item/Key&amp;#34;)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This allows me to easily set environment variables for my secrets in my shell.&#xA;However, I need to remember to unset these variables when I&amp;rsquo;m done with them. To&#xA;help with this, I use &lt;a href=&#34;https://direnv.net/&#34; target=&#34;_blank&#34;&gt;Direnv&lt;/a&gt; to set and unset variables&#xA;based on my directory.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;brew install direnv&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I use the following &lt;code&gt;.envrc&lt;/code&gt; file in the root of my Terraform configuration to&#xA;set my secrets.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;# Exports&#xA;export TFE_TOKEN=$(op read --cache &amp;#34;op://Vault/Item/Key&amp;#34;)&#xA;&#xA;# Terraform Variable exports&#xA;export TF_VAR_okta_token=$(op read --cache &amp;#34;op://Vault/Item/Section/Key&amp;#34;)&#xA;export TF_VAR_okta_client_id=$(op read --cache &amp;#34;op://Vault/Item/Section/Key&amp;#34;)&#xA;export TF_VAR_okta_client_secret=$(op read --cache &amp;#34;op://Vault/Item/Section/Key&amp;#34;)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above example exports the &lt;code&gt;TFE_TOKEN&lt;/code&gt; and a few Terraform variables. When I&#xA;change into or out of the directory with the &lt;code&gt;.envrc&lt;/code&gt; file, &lt;code&gt;direnv&lt;/code&gt; will set&#xA;and unset these variables for me.&lt;/p&gt;&#xA;&lt;p&gt;With so many tools in use, remembering all the commands to run can be a pain.&#xA;Yes, I could create aliases for them, but I like to keep my shell clean. So, I&#xA;use &lt;a href=&#34;https://taskfile.dev/&#34; target=&#34;_blank&#34;&gt;Task&lt;/a&gt; to create a &lt;code&gt;Taskfile.yml&lt;/code&gt; with all the&#xA;commands I use.&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-yaml&#34; data-lang=&#34;yaml&#34;&gt;# yaml-language-server: $schema=https://taskfile.dev/schema.json&#xA;# https://taskfile.dev&#xA;&#xA;version: &amp;#34;3&amp;#34;&#xA;&#xA;dotenv: [&amp;#34;.envrc&amp;#34;]&#xA;&#xA;vars:&#xA;  CURRENT_DATE:&#xA;    sh: date +&amp;#34;%Y-%m-%dT%H:%M:%S%Z&amp;#34;&#xA;&#xA;tasks:&#xA;  default:&#xA;    cmds:&#xA;      - task: pre&#xA;&#xA;  hog:&#xA;    cmds:&#xA;      - trufflehog git file://. --since-commit HEAD --only-verified --fail&#xA;&#xA;  pre:&#xA;    cmds:&#xA;      - pre-commit autoupdate&#xA;      - pre-commit run -a&#xA;&#xA;  push:&#xA;    cmds:&#xA;      - git add .&#xA;      - git commit -m &amp;#34;{{.CURRENT_DATE}}&amp;#34;&#xA;      - git push&#xA;    silent: true&#xA;&#xA;  tag:&#xA;    cmds:&#xA;      - git push&#xA;      - git tag -s {{.CLI_ARGS}} -m &amp;#34;{{.CLI_ARGS}}&amp;#34;&#xA;      - git push --tags&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now I can run &lt;code&gt;task hog&lt;/code&gt; to scan my git repo for secrets, &lt;code&gt;task pre&lt;/code&gt; to run all&#xA;of my &lt;code&gt;pre-commit&lt;/code&gt; hooks, &lt;code&gt;task push&lt;/code&gt; to commit my changes and push them to the&#xA;remote, and &lt;code&gt;task tag -- v1.0.0&lt;/code&gt; to tag my release.&lt;/p&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;This is how I set up my workstation for Terraform development. I hope you found&#xA;it helpful. If you have any suggestions or tools that you use, please let me&#xA;know. I am always looking for ways to improve my workflow.&lt;/p&gt;&#xA;</description>
    </item>
    <item>
      <title>VS Code Setup for Terraform</title>
      <link>https://www.404-code-not-found.com/posts/vscode-setup-for-terraform/</link>
      <pubDate>Thu, 10 Oct 2024 10:30:00 -0600</pubDate>
      <guid>https://www.404-code-not-found.com/posts/vscode-setup-for-terraform/</guid>
      <description>&lt;p&gt;&lt;img src=&#34;https://www.404-code-not-found.com/posts/vscode-setup-for-terraform/article.png&#34; alt=&#34;Visual Studio Code&#34;&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;introduction&#34;&gt;Introduction&lt;/h2&gt;&#xA;&lt;p&gt;One of the first questions I get when teaching Terraform is, &amp;ldquo;What editor should&#xA;I use?&amp;rdquo; My answer is always, &amp;ldquo;Use what you are comfortable with. If you don&amp;rsquo;t&#xA;have a preference, I recommend Visual Studio Code.&amp;rdquo; Over the years, I have used&#xA;everything from &lt;code&gt;vi&lt;/code&gt; to &lt;code&gt;Notepad++&lt;/code&gt; to &lt;code&gt;Sublime Text&lt;/code&gt; to &lt;code&gt;Atom&lt;/code&gt; to&#xA;&lt;code&gt;Visual Studio Code&lt;/code&gt;. Visual Studio Code is easy to pick up for new users and&#xA;easy to customize to your liking.&lt;/p&gt;&#xA;&lt;p&gt;I am not a developer by trade; I&amp;rsquo;m more of a hack-it-together person. I copy a&#xA;lot of code from the internet or use GenAI and modify it to fit my needs. So, I&#xA;don&amp;rsquo;t work in an IDE every day. I need something easy to use and easy to&#xA;customize.&lt;/p&gt;&#xA;&lt;h2 id=&#34;tldr&#34;&gt;TL;DR&lt;/h2&gt;&#xA;&lt;p&gt;For those of you like me who want the quick and dirty, here are the steps to set&#xA;up your VS Code for Terraform development:&lt;/p&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Install Visual Studio Code - &lt;a href=&#34;https://code.visualstudio.com/&#34; target=&#34;_blank&#34;&gt;Download&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;Create a profile for Terraform - &lt;a href=&#34;#visual-studio-code-profiles&#34;&gt;Profiles&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;Clone my Terraform template repository -&#xA;&lt;a href=&#34;https://github.com/404-code-not-found-com/terraform-module-template&#34; target=&#34;_blank&#34;&gt;Terraform Template&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;Open the repository in VS Code - This will prompt you to install the&#xA;recommended extensions and import the settings.&lt;/li&gt;&#xA;&lt;li&gt;Go forth and Terraform!&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;h2 id=&#34;visual-studio-code-profiles&#34;&gt;Visual Studio Code Profiles&lt;/h2&gt;&#xA;&lt;p&gt;One of the reasons I like Visual Studio Code is that it supports profiles. I can&#xA;create a profile for each type of work I do. I have profiles for Terraform,&#xA;Python, Ansible, and Markdown. Each profile has its settings, extensions, and&#xA;keybindings. By using profiles, I can tailor the editor to the work I am doing.&lt;/p&gt;&#xA;&lt;h3 id=&#34;creating-a-profile&#34;&gt;Creating a Profile&lt;/h3&gt;&#xA;&lt;p&gt;To create a profile, click on the &lt;code&gt;gear&lt;/code&gt; icon in the lower-left corner of the VS&#xA;Code window. Click on the &lt;code&gt;Profiles&lt;/code&gt; option in the&amp;rsquo; Settings&amp;rsquo; window.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;profiles.png&#34; alt=&#34;VS Code Settings&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;The Profiles tab allows you to view your current profiles and create new ones.&#xA;To create a new profile, click the &lt;code&gt;New Profile&lt;/code&gt; button.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;new-profile1.png&#34; alt=&#34;New Profile&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;This creates a new &lt;code&gt;Untitled&lt;/code&gt; profile.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;new-profile2.png&#34; alt=&#34;Untitled Profile&#34;&gt;&lt;/p&gt;&#xA;&lt;p&gt;You can now name your profile by editing the name field. You also have the&#xA;option to copy the settings from an existing profile. This is useful for&#xA;creating a new profile similar to an existing one or importing your default&#xA;settings.&lt;/p&gt;&#xA;&lt;p&gt;&lt;img src=&#34;copy-profile.png&#34; alt=&#34;Copy From Profile&#34;&gt;&lt;/p&gt;&#xA;&lt;h3 id=&#34;vs-code-extensions-for-terraform&#34;&gt;VS Code Extensions for Terraform&lt;/h3&gt;&#xA;&lt;p&gt;I will admit that I am a bit of an extension junkie. I have a lot of extensions,&#xA;but some don&amp;rsquo;t play well together, so I use profiles to help limit the number of&#xA;extensions I have enabled at any one time.&lt;/p&gt;&#xA;&lt;p&gt;I use four extensions for Terraform development in Visual Studio Code:&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://marketplace.visualstudio.com/items?itemName=hashicorp.hcl&#34; target=&#34;_blank&#34;&gt;HashiCorp HCL&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://marketplace.visualstudio.com/items?itemName=hashicorp.sentinel&#34; target=&#34;_blank&#34;&gt;HashiCorp Sentinel&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://marketplace.visualstudio.com/items?itemName=hashicorp.terraform&#34; target=&#34;_blank&#34;&gt;HashiCorp Terraform&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;https://marketplace.visualstudio.com/items?itemName=cardinal90.multi-cursor-case-preserve&#34; target=&#34;_blank&#34;&gt;Multiple cursor case preserver&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;You&amp;rsquo;re probably looking at the list and wondering, &amp;ldquo;Why do you need a case&#xA;preserver?&amp;rdquo; How many times have you decided to change the name of a variable and&#xA;then had to go back and change the case of the variable in multiple places? This&#xA;extension will preserve the case of the variable when you change it. It&amp;rsquo;s a&#xA;small thing, but it saves me a lot of time.&lt;/p&gt;&#xA;&lt;h3 id=&#34;vs-code-settings-for-terraform&#34;&gt;VS Code Settings for Terraform&lt;/h3&gt;&#xA;&lt;p&gt;I like to use a few settings when working with Terraform. Most of these can be&#xA;set in the VS Code UI, but they can also be shared in a &lt;code&gt;JSON&lt;/code&gt; file within the&#xA;&lt;code&gt;.vscode&lt;/code&gt;. directory of your working directory.&lt;/p&gt;&#xA;&lt;p&gt;Here are my Terraform specific VS Code settings:&lt;/p&gt;&#xA;&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-json&#34; data-lang=&#34;json&#34;&gt;{&#xA;  &amp;#34;[terraform]&amp;#34;: {&#xA;    &amp;#34;editor.defaultFormatter&amp;#34;: &amp;#34;hashicorp.terraform&amp;#34;&#xA;  },&#xA;  &amp;#34;[tfvars]&amp;#34;: {&#xA;    &amp;#34;editor.defaultFormatter&amp;#34;: &amp;#34;hashicorp.terraform&amp;#34;&#xA;  },&#xA;  &amp;#34;editor.bracketPairColorization.enabled&amp;#34;: true,&#xA;  &amp;#34;editor.formatOnPaste&amp;#34;: true,&#xA;  &amp;#34;editor.formatOnSave&amp;#34;: true,&#xA;  &amp;#34;editor.formatOnType&amp;#34;: true,&#xA;  &amp;#34;editor.guides.bracketPairs&amp;#34;: &amp;#34;active&amp;#34;,&#xA;  &amp;#34;editor.inlineSuggest.enabled&amp;#34;: true,&#xA;  &amp;#34;editor.linkedEditing&amp;#34;: true,&#xA;  &amp;#34;editor.multiCursorModifier&amp;#34;: &amp;#34;alt&amp;#34;,&#xA;  &amp;#34;editor.renderControlCharacters&amp;#34;: true,&#xA;  &amp;#34;editor.renderWhitespace&amp;#34;: &amp;#34;all&amp;#34;,&#xA;  &amp;#34;editor.rulers&amp;#34;: [&#xA;    {&#xA;      &amp;#34;color&amp;#34;: &amp;#34;#A5FF90&amp;#34;,&#xA;      &amp;#34;column&amp;#34;: 80&#xA;    },&#xA;    {&#xA;      &amp;#34;color&amp;#34;: &amp;#34;#FF628C&amp;#34;,&#xA;      &amp;#34;column&amp;#34;: 100&#xA;    }&#xA;  ],&#xA;  &amp;#34;editor.stickyScroll.enabled&amp;#34;: true,&#xA;  &amp;#34;editor.suggestSelection&amp;#34;: &amp;#34;first&amp;#34;,&#xA;  &amp;#34;editor.tabCompletion&amp;#34;: &amp;#34;on&amp;#34;,&#xA;  &amp;#34;editor.tabSize&amp;#34;: 2,&#xA;  &amp;#34;files.associations&amp;#34;: {&#xA;    &amp;#34;*.sh.tmpl&amp;#34;: &amp;#34;shellscript&amp;#34;&#xA;  },&#xA;  &amp;#34;files.trimTrailingWhitespace&amp;#34;: true,&#xA;  &amp;#34;terraform.languageServer.enable&amp;#34;: true&#xA;}&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Not all of these settings are needed, but they are the ones I like to use. You&#xA;can adjust them to fit your needs.&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.defaultFormatter&lt;/code&gt; - This sets the default formatter for the editor. I&#xA;like to use the HashiCorp Terraform formatter for all of my Terraform,&#xA;&lt;code&gt;tfvars&lt;/code&gt;, and Sentinel policy files.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.bracketPairColorization.enabled&lt;/code&gt; - This will colorize the brackets in&#xA;your code to make it easier to see where they start and end.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.formatOnPaste&lt;/code&gt;, &lt;code&gt;editor.formatOnSave&lt;/code&gt;, &lt;code&gt;editor.formatOnType&lt;/code&gt; - These&#xA;settings will format your code when you paste it, save the file, or type in&#xA;the editor.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.guides.bracketPairs&lt;/code&gt; - This will highlight the bracket pair you work&#xA;within.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.inlineSuggest.enabled&lt;/code&gt; - This will enable inline suggestions in the&#xA;editor.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.linkedEditing&lt;/code&gt; - This will link the editing of a variable name to all&#xA;instances of that variable in the file.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.multiCursorModifier&lt;/code&gt; - This sets the keybinding for creating multiple&#xA;cursors.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.renderControlCharacters&lt;/code&gt; - This will render control characters in the&#xA;editor.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.renderWhitespace&lt;/code&gt; - This will render whitespace in the editor.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.rulers&lt;/code&gt; - This will add rulers to the editor in specified columns. I&#xA;don&amp;rsquo;t like to have lines that are too long, so I set my rulers at 80 and 100&#xA;columns.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.stickyScroll.enabled&lt;/code&gt; - This will enable sticky scrolling so that the&#xA;block header stays at the editor&amp;rsquo;s top as you scroll through the code section.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.suggestSelection&lt;/code&gt; - This sets the default suggestion selection to the&#xA;first suggestion.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.tabCompletion&lt;/code&gt; - This will enable tab completion in the editor.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;editor.tabSize&lt;/code&gt; - This sets the tab size to 2 spaces.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;files.associations&lt;/code&gt; - This will associate the specified file extensions with&#xA;the specified language. I also work with a lot of shell script templates that&#xA;get processed by Terraform, so I associate the &lt;code&gt;.sh.tmpl&lt;/code&gt; files with the&#xA;&lt;code&gt;shellscript&lt;/code&gt; language.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;files.trimTrailingWhitespace&lt;/code&gt; - This will trim trailing whitespace from the&#xA;end of lines.&lt;/li&gt;&#xA;&lt;li&gt;&lt;code&gt;terraform.languageServer.enable&lt;/code&gt; will enable the Terraform language server.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;&#xA;&lt;p&gt;This helps you get started with Terraform development in Visual Studio.&lt;/p&gt;&#xA;&lt;h2 id=&#34;updates&#34;&gt;Updates&lt;/h2&gt;&#xA;&lt;h3 id=&#34;2025-01-25&#34;&gt;2025-01-25&lt;/h3&gt;&#xA;&lt;p&gt;I removed settings for an older Terraform extension I used before the official&#xA;HashiCorp extension was released. Also, I removed file associations that used&#xA;the Terraform extension to format Sentinel policy files and generic HCL files. I&#xA;now use the HashiCorp Sentinel extension for Sentinel policy files and the&#xA;HashiCorp HCL extension for generic HCL files.&lt;/p&gt;&#xA;</description>
    </item>
  </channel>
</rss>
