Injecting Static Secrets into CI/CD Pipelines with Terraform and Vault
The Git repository is the natural home for pipeline configuration, which makes it an uncomfortable place for a database password or a third-party API key. Teams that start with a single shared credential rarely stay with one: build agents copy values into environment variables, secret files land in temporary directories, and the same token ends up pasted into a chat message at two in the morning. Terraform and HashiCorp Vault give infrastructure teams a way to separate configuration from credential material, so the pipeline describes what it needs while the secret itself is fetched at run time and discarded when the job finishes.
Terraform fits the deployment side of the problem. Infrastructure as code expresses the Vault cluster, its auth methods, policies and secret paths as reviewable HCL, giving a change to a credential policy the same peer review, plan and apply cycle as a network change. Vault holds the material, issues it on demand, records every read in an audit log, and lets you revoke a single secret without touching every consuming system.
For Australian infrastructure teams the combination is especially relevant because most estates are hybrid. Workloads run in the AWS Sydney and Melbourne regions while line-of-business applications sit on VMware clusters in Perth, Brisbane and Canberra datacentres, and agencies in Belconnen have procurement rules about where data can physically reside. This guide covers authenticating a pipeline to Vault without long-lived keys, scoping policies per environment, injecting values into jobs, and proving afterwards who read what.
Where static credentials actually leak
Most exposures are mundane rather than exotic. A developer exports DATABASE_PASSWORD into a shell profile, a Jenkins job prints its environment during debugging, and a CI runner with a shared Docker daemon retains the value in a layer. Forked pull requests, cached dependencies and uploaded test artefacts each keep a copy alive well after the build that needed it.
Local regulatory and commercial pressure makes this harder to ignore. Retailers such as Coles and Woolworths, banks including CBA and NAB, and health providers handling Medicare-style records all operate under obligations that turn credential handling into an audit question. The Australian Signals Directorate's Essential Eight expects privileged access to be tightly controlled, APRA's CPS 234 applies to information assets held by financial institutions, and PCI DSS governs card data in payment environments. A leaked build-time credential can move an organisation from a compliance finding to a notification obligation under the Privacy Act 1988.
Australian teams also feel the practical cost of a leak. When a vendor key is exposed at 3pm AEST on a Friday, the remediation call crosses Perth business hours and lands with an on-call engineer in Sydney still finishing a handover. Revoking and reissuing access is far cheaper if the credential was scoped to one pipeline and one environment in the first place.
Terraform as the control plane for Vault
The hashicorp/vault provider lets you manage auth methods, policies, secret engines and namespaces as code. A small module describing a KV v2 mount, an approle role and a policy bound to a team path gives reviewers a readable diff rather than a click-path through a UI.
State hygiene deserves attention, because Terraform state frequently accumulates sensitive values. Keep remote state in an S3 bucket in ap-southeast-2 or ap-southeast-4 with versioning, native locking and KMS encryption, and treat the state file as a secret-bearing asset in its own right. Marking a variable sensitive hides it from CLI output but does not encrypt it, so prefer reading values through a Vault data source at apply time rather than passing literals into .tfvars files.
A common pattern is to run plans through Terraform Cloud or an Atlantis-style pull request controller, then let a pipeline apply changes using an identity token instead of a stored access token. That single change removes the bootstrap credential that otherwise has to live in the repository to unlock the automation that removes credentials.
Authenticating the pipeline without long-lived keys
Vault's OIDC and JWT auth methods let a pipeline present a short-lived identity token. GitHub Actions, GitLab CI and Azure DevOps all issue such tokens, and Vault validates them against configured trust settings, audiences and bound claims. The result is that a job authenticates as the workflow that triggered it, and the token expires with the run.
For workloads on AWS, Vault's IAM auth method maps an EC2 instance profile, ECS task role or EKS workload identity to a Vault policy, which suits long-running services better than pipelines. Kubernetes installations add the Vault Agent injector, while on-premise VMware estates can use Kubernetes auth, AppRole or AWS IAM where a build farm runs in the cloud.
Region choice matters more here than usual. Australian customers with data residency requirements often want Vault to fail closed outside approved geographies, which means configuring regional endpoints and disabling any global cluster address. Confirm the fail behaviour during a test, because a silent fallback to a foreign endpoint is exactly the kind of finding an assessor raises.
Policies that scope access per team and environment
Default-deny policies with explicit paths keep the blast radius small. Organise KV v2 data by environment and team, for example secret/data/payments/prod, then grant a pipeline read access to that path and nothing else. Avoid wildcards on secret paths, and review capabilities carefully, since list on a mount can expose metadata that reveals the shape of your estate.
A minimal policy looks something like this:
path "secret/data/payments/prod" {
capabilities = ["read"]
}
Organisations running multiple business units usually add Vault namespaces or the Sentinel seat-based licensing model, and Vault Enterprise unlocks features such as HSM-backed root token protection and advanced audit controls that some Australian financial institutions require in their vendor assessments.
Where the consuming system supports it, prefer a dynamic secret with a lease. Static injection becomes the deliberate fallback for systems that cannot rotate credentials, which makes those exceptions visible in policy rather than hidden in a shared variable.
Injecting secrets into the job at run time
Vault Agent runs alongside the job, authenticates using the pipeline identity, caches a policy for the life of the run, and writes the requested value into a file or environment variable through a template stanza. The job never holds a Vault token and never sees the credential in source control.
In a GitHub Actions workflow the credential step is short:
- name: Fetch credentials
run: vault agent -config=/etc/vault/agent.hcl
Templates can render environment variables, but file sinks are often safer. Writing to a path with restrictive permissions and having the application read it at start-up keeps the value out of process listings and debug output. Whichever sink you choose, disable the pipeline's habit of echoing the full environment, and add masking for the specific keys that might still appear in a stack trace.
Auditing reads and detecting drift
Every secret read generates an audit record containing the requester, path, time and IP address. Shipping that stream to a SIEM retained locally or in-region gives you a defensible answer to a PCI DSS 4.0 evidence request or an Essential Eight assessment, and it turns credential use into something you can review in a standup rather than investigate after an incident.
Detection works alongside audit. Scanning Git history with tools such as gitleaks or trufflehog surfaces tokens committed before Vault was introduced, while policy analysis in CI catches new roles that request broader paths than the team needs. Vault Enterprise drift detection helps here, comparing running configuration against the last known good state, though it pairs best with Terraform plan output when policies themselves live in HCL.
When an incident does occur, rotation is the response. If values are issued from Vault with automatic rotation or leases, reissuing affects only active sessions. If a static secret was exposed, every consumer of it needs replacement, and the audit log tells you which pipelines touched it during the exposure window.
Running Vault across Australian hybrid environments
A highly available Vault cluster needs three or five nodes spread across failure domains, and Australian geography complicates that layout. Nodes in Sydney and Melbourne AWS availability zones give you quorum with modest latency, while a third node in an on-premise VMware cluster in Brisbane or Canberra keeps the estate resilient to a regional cloud outage, provided the WAN path is stable and MTU settings account for encrypted traffic.
Backup and disaster recovery planning should assume a regional failure rather than a single host failure. Snapshot the cluster, replicate it to a second Australian region, and rehearse restores at a time when the engineering team can actually respond, taking into account the December and January holiday period when staffing thins out. Rotation windows also deserve scheduling care across AEST and AEDT, since a shift in daylight saving can move a job by an hour relative to the previous week.
Adoption tends to succeed when the platform team owns the Vault cluster as a shared service, publishes a self-service path convention, and pairs the technical work with the professional development many Australian engineers value, including HashiCorp Vault and Terraform certifications and the local user-group scene. That combination of auditable infrastructure code and short-lived credential issuance is what turns static secret handling from a tolerated risk into a solved problem.