8.4 Infrastructure as Code Security, Policy Gates & Drift Control
Key Takeaways
- Infrastructure as Code expresses desired cloud state in versioned templates, making infrastructure reviewable, repeatable, and testable.
- IaC repositories, modules, pipelines, state files, and deployment identities form a software supply chain and require their own controls.
- Security scanning should combine syntax and misconfiguration tests, policy as code, secret detection, dependency review, and a safe plan review before apply.
- Production deployment identities should be short lived and scoped; separation of duties prevents template authors from bypassing approval and policy gates.
- Drift detection compares deployed state with approved desired state and should normally remediate through code rather than unmanaged console changes.
8.4 Infrastructure as Code Security, Policy Gates & Drift Control
Quick Answer: Infrastructure as Code (IaC) represents cloud infrastructure as versioned desired state. It improves consistency only when the code, modules, state, pipeline, and deployment identity are protected. A secure workflow reviews proposed changes, scans them for secrets and misconfiguration, evaluates policy before deployment, uses short-lived scoped credentials, records the result, and detects drift between approved code and deployed resources.
Declarative desired state and repeatability
Manual console changes are difficult to reproduce, review, and test. IaC uses templates or programs to declare networks, identities, compute, storage, managed services, and policy. The engine calculates the difference between desired and current state and applies an execution plan.
Benefits include consistent environments, peer review, rapid rebuilding, traceable change history, and automated tests. IaC is not automatically secure: a repeatable public bucket or wildcard role is still a repeatable vulnerability. The control value comes from a governed pipeline around the code.
Threat model the IaC supply chain
The attack surface includes:
- Repository: unauthorized commits, stolen maintainer sessions, weak branch protection, or malicious workflow changes.
- Modules and providers: compromised public modules, dependency confusion, unpinned versions, and abandoned dependencies.
- Secrets: credentials or sensitive values embedded in templates, variable files, build logs, or generated plans.
- State: IaC state may contain resource identifiers, topology, outputs, and plaintext sensitive values even when templates refer to a secret manager.
- Pipeline runner: a compromised runner can alter the plan, steal credentials, or deploy code different from the reviewed commit.
- Deployment identity: broad permanent credentials can create persistence and cross-environment impact.
- Artifact handoff: an approved plan can be replaced before apply unless the commit, plan, and execution are bound together.
Treat modules and providers as dependencies. Use trusted registries, pin versions or verified digests where supported, review provenance, monitor advisories, and remove unused packages.
Secure plan-to-apply pipeline
A mature flow separates validation from deployment:
- A developer changes a reviewed module or environment declaration on a protected branch.
- Automated checks validate syntax, formatting, dependency policy, and secret scanning.
- Static IaC analysis detects risky constructs such as public ingress, unencrypted storage, disabled logging, or wildcard permissions.
- Policy as code evaluates organization requirements such as approved regions, mandatory tags, network boundaries, backup protection, and key ownership.
- The engine creates a plan showing additions, changes, replacements, and deletions.
- A qualified reviewer checks high-impact changes and the plan's exact scope.
- The deployment job exchanges its workload identity for short-lived credentials restricted to the target environment.
- The pipeline applies the reviewed artifact, records events and outputs, and verifies post-deployment state.
The apply stage should use the same commit and approved plan. Rebuilding an unpinned module between review and apply creates a time-of-check/time-of-use gap.
Policy as code and provider guardrails
Policy as code evaluates proposed or deployed configuration and returns a consistent decision. It is useful for conditions that can be tested from configuration, such as whether storage encryption is enabled or a network rule exposes an administrative port.
Not every policy belongs only in the pipeline. A malicious actor or emergency administrator may bypass IaC and call the provider API directly. Place critical preventive restrictions, such as forbidden regions or protected logging, in provider organization guardrails where possible. Pipeline policy improves developer feedback; provider policy enforces the outer boundary. Detective posture controls find residual drift.
Protect state and credentials
Store remote state in a restricted backend with encryption, versioning, locking, audit logs, and recovery controls. Grant read access only to roles that genuinely need it. Do not expose state in ordinary build artifacts or support tickets. Marking a value sensitive may hide it in output but does not guarantee it is absent from state.
Prefer workload identity federation from the CI/CD platform to the cloud provider. The runner obtains a short-lived token with claims bound to repository, branch, environment, and workflow. Avoid permanent cloud keys in repository variables. Separate plan-only access from apply authority, and give production deployments narrower rights than broad human administration.
Drift and break-glass change
Drift occurs when deployed state differs from approved desired state. It can result from console changes, provider defaults, automated remediation, incident response, or an attacker. Schedule comparison, subscribe to configuration-change events, and investigate unexpected differences.
Do not blindly overwrite every difference. A responder may have applied an emergency isolation rule. Determine whether the change is authorized, then either import it into code through review or revert it through the normal pipeline. A break-glass process should record the actor, reason, expiration, and reconciliation task.
IaC does not eliminate testing
Validate the live environment after deployment. Provider behavior, inherited policy, service defaults, and interactions among modules may differ from a static template assumption. Combine pre-deployment tests with runtime posture, network testing, access analysis, and monitoring. IaC supplies a reviewable desired state; assurance comes from proving that the desired state was safely deployed and remains effective.
A CI/CD system deploys production IaC using a permanent cloud administrator key stored in a repository variable. Which replacement most directly reduces credential and blast-radius risk?
Why must an IaC state backend be treated as sensitive even when templates retrieve application secrets from a secret manager?
Drift detection finds a production network isolation rule that is absent from approved code. What should the team do first?