Skip to main content

Secure IaC Policy

1. Objective​

This policy defines the security requirements applicable to the design, validation, execution and maintenance of Infrastructure as Code (IaC) in all environments managed by the organisation.

IaC defines the foundation on which software runs - a configuration error in IaC is not an application bug: it is an infrastructure vulnerability with potential impact on every system that shares it. Permissive security groups, IAM roles with excessive access, public buckets, disabled encryption - all of these errors recur in unreviewed IaC and have proportionally more serious consequences than most code vulnerabilities.

The objective of this policy is to ensure that:

  • IaC is treated as critical software: reviewed, tested, signed and audited
  • Insecure configurations are detected automatically before any apply
  • Policy-as-code enforces guardrails that do not depend on individual human review
  • Manual changes to the real state of the infrastructure (drift) are detected and corrected
  • External IaC modules are assessed, versioned and approved before adoption

2. Scope​

This policy applies to all infrastructure defined as code, regardless of the tool used (Terraform, OpenTofu, Pulumi, CloudFormation, Bicep, Ansible, etc.) and of the environment (public, private or hybrid cloud, Kubernetes, etc.).


3. Principles of secure IaC​

PrinciplePractical application
IaC as codeVersioned, reviewed, tested and audited with the same rigour as application code
Principle of least privilegeIAM roles and policies with the minimum permissions for the function; no wildcards in prod
Environment separationSegregated environments with separate state and distinct credentials; no cross-references between backends
Infrastructure immutabilityChanges via pipeline - not manual; manual changes are drift and must be reverted
Fail secureDefault configurations are the most restrictive; explicit permissions for what is needed
No hardcodesSecrets, IPs and sensitive references never hardcoded; passed as variables or via a vault

4. Mandatory automated validation​

4.1 Linting and syntax validation​

Run on every PR with IaC changes, before any plan or apply:

  • Formatting checked (terraform fmt, or the equivalent for each tool)
  • Syntax validated (terraform validate, or equivalent)
  • Best-practice linter (tflint, or equivalent)

4.2 Security scanning​

GateL1L2L3
IaC configuration scanner (tfsec, checkov, kics, terrascan)AlertBlocks High/CriticalBlocks Medium+
Policy-as-code (OPA/Rego, Sentinel, Conftest)RecommendedMandatoryMandatory
Validation of minimum IAM permissionsRecommendedMandatoryMandatory
Detection of hardcoded secretsMandatoryMandatoryMandatory

4.3 Policy-as-code​

At L2/L3, infrastructure security policies must be codified as automatically verifiable rules:

  • Policies covered include (examples): no public buckets, mandatory encryption at rest, security groups without unrestricted 0.0.0.0/0 access, mandatory tags on all resources
  • Policies versioned and approved by an AppSec Engineer
  • A policy failure blocks the apply regardless of human review
  • Compliance reports exported per environment

5. Environment separation​

RequirementL1L2L3
Environments in separate directories/workspacesRecommendedMandatoryMandatory
Separate remote state per environment (no state shared between dev/prod)RecommendedMandatoryMandatory
Distinct credentials per environmentRecommendedMandatoryMandatory
No cross-references between backends of different environmentsRecommendedMandatoryMandatory
State encrypted at restRecommendedMandatoryMandatory
State locking for concurrent operationsRecommendedMandatoryMandatory

6. External modules and internal catalogue​

6.1 Assessment of external modules​

IaC modules from external sources (e.g. Terraform Registry, public GitHub) must be assessed before adoption:

  • Origin verified (recognised maintainer, active repository)
  • No known vulnerabilities in the version to be adopted
  • Compatible licence
  • Version fixed (pinning) - no use of latest or ranges in prod

6.2 Internal catalogue of approved modules​

At L2/L3, the organisation must maintain a catalogue of approved internal IaC modules:

  • Modules versioned with a changelog
  • Modules with documented and typed outputs
  • Formal approval before publication (AppSec Engineer + DevOps/SRE)
  • New or changed modules subject to review and scanning before publication
  • Projects reference modules from the internal catalogue - not local copies

7. Plan review and approval​

Before any apply in staging or production environments:

RequirementL1L2L3
Plan generated in the pipeline (not locally)RecommendedMandatoryMandatory
Readable plan output attached to the PRRecommendedMandatoryMandatory
Plan reviewed by DevOps/SRE before applyRecommendedMandatoryMandatory
Plan reviewed by an AppSec Engineer (security impact)Not applicableRecommendedMandatory
Plan signed before apply (L3)Not applicableNot applicableMandatory
Apply blocked without explicit approval in staging/prodRecommendedMandatoryMandatory (dual approval)

The plan review must validate:

  • No resources being unexpectedly destroyed
  • No unplanned changes to security groups, IAM roles or policies
  • Changes confined to the scope of the PR

8. Drift detection and correction​

Manual changes to the real infrastructure create a divergence (drift) from the state defined in IaC. This state is unacceptable at L2/L3:

RequirementL1L2L3
Scheduled drift detection jobRecommended (monthly)Mandatory (fortnightly)Mandatory (weekly; drift resolved within ≤ 7 days)
Drift report per environment generated and archivedRecommendedMandatoryMandatory
Critical drift in production triggers an immediate alertRecommendedMandatoryMandatory
Drift correction via PR with approval (never manual)RecommendedMandatoryMandatory
warning

Direct manual changes to production infrastructure, outside the IaC pipeline, are treated as non-compliance at L2/L3 and must be recorded as an incident. The resulting drift must be corrected by the pipeline, not legitimised retroactively.


9. Mandatory resource tagging​

All resources created by IaC must carry mandatory tags that enable traceability:

TagDescription
Environmentdev / staging / prod
OwnerResponsible team or product
ApplicationApplication identifier
CriticalityL1 / L2 / L3
ManagedByterraform / pulumi / etc.

Tagging compliance must be verified automatically by policy-as-code at L2/L3.


10. Expected artefacts​

ArtefactDescriptionRetention
backend.tf + locking logsRemote state configuration and locking evidenceWhile active
Lint and scanning reportsOutput of tfsec, checkov, etc., per PR90 days (L2), 1 year (L3)
Policy-as-code reportsPolicy compliance per environment90 days (L2), 1 year (L3)
Plan history with metadataTimestamp, author, PR/MR ID1 year (L2/L3)
Drift reportsPer environment, per audit cycle1 year (L2/L3)
Catalogue of approved modulesActive version + changelogHistory

11. Responsibilities​

RoleResponsibility
Developer / DevOpsWrite IaC and submit it for review; fix scanning findings; reference modules from the catalogue
DevOps / SREManage state backends; configure the IaC pipeline; run drift detection; manage the module catalogue
AppSec EngineerDefine policy-as-code policies; approve modules; review plans with security impact; manage exceptions
GRC / ComplianceAudit compliance reports; verify drift; validate artefact retention

12. Review and audit of this policy​

This policy must be reviewed annually or after any of the following events:

  • An incident originating from an insecure IaC configuration or undetected drift
  • Adoption of a new IaC tool or a new cloud provider
  • A change to the scanning or policy-as-code tools

13. Normative and technical references​

ReferenceRelevance
SbD-ToE Ch. 08 - IaC and InfrastructurePrinciples, user stories, artefacts, lifecycle
IaC Plan Approval Policy (22_policy-aprovacao-plan-iac.md)Detailed plan review and approval process
Secure CI/CD Policy (17_policy-cicd-seguro.md)IaC validation pipeline
Secrets Management Policy (18_policy-gestao-segredos.md)Secrets in IaC and providers
tfsec / checkov / kicsIaC scanning tools
OPA / Rego / Sentinel / ConftestPolicy-as-code tools
CIS Benchmarks (AWS, Azure, GCP)Reference controls for cloud infrastructure
NIST SP 800-190Application Container Security Guide (applicable to container IaC)
SLSA FrameworkBuild integrity - applicable to IaC artefacts