Skip to main content

Execution Planning and State Control

🌟 Objective​

Ensure that all Infrastructure as Code (IaC) projects maintain consistent, secure and traceable control of the infrastructure state and of the execution plans (plan) generated before any change.

This practice makes it possible to:

  • Prevent drift and unauthorised changes;
  • Avoid concurrency and conflicts between multiple operators;
  • Enable secure auditing and rollback;
  • Enable integration with CI/CD pipelines and approval processes;
  • Ensure integrity and compliance with execution policies.

📌 What must be done​

  1. Configure an authenticated remote backend to store the state (terraform.tfstate, or equivalent);
  2. Enable a locking mechanism to avoid concurrency during apply;
  3. Generate and version execution plans (plan) before applying changes;
  4. Associate plans with approval mechanisms (e.g. Pull Request, Change Request);
  5. Store plan artefacts and execution logs with verifiable integrity;
  6. Detect and alert on drift (the difference between actual and expected state);
  7. Prohibit manual local executions outside a validated and controlled context.

⚙️ How to apply​

ActionPrescription
Remote backendS3 + DynamoDB (AWS), Azure Blob + CosmosDB, or GCS, with strong authentication
Lockinglock = true + a mechanism such as DynamoDB, Consul or equivalent
CI/CD executionProhibit manual apply; plan generated and versioned in the pipeline
TraceabilityTags, branches, PR/MR ID, release name and readable output in the diff
AuditingKeep plan and logs with hashes and metadata (timestamp, hash, author)
Drift detectionterraform plan -detailed-exitcode, driftctl, tfsec, or scheduled CI

🕒 When to apply​

MomentExpected action
Start of the IaC projectDefine and document backend, environment and locking
New apply in productionGenerate plan, submit for approval, version the artefact
After a rollback, error or patchValidate the current state and correct any divergence
Periodically (e.g. scheduled CI)Check for drift between expected state and reality

👥 Profiles involved​

RoleResponsibility
DevOps/CloudBackend configuration, execution via pipeline
SecurityDefinition of the controlled execution policy
ArchitectureApproval of environments and isolation mechanisms
GRC/ComplianceVerification of traceability and audit mechanisms

🧪 Practical examples​

  • terraform backend "s3" with encrypt = true, lock_table and versioning;
  • Pipeline with a job that runs terraform plan, stores the .plan and waits for approval before apply;
  • DynamoDB table configured for locking, with TTL and per-project tags;
  • Storage of plans and logs in a versioned bucket (branch + timestamp + PR as the artefact name).

✅ Good practices​

  • Never execute terraform apply locally in production environments;
  • Validate and record plan before any significant change;
  • Use artefacts with integrity hashes and cross-traceability with the PR or release;
  • Periodically monitor divergences between the actual and the expected state (drift detection);
  • Establish a formal organisational policy on controlled infrastructure execution.

🔗 Cross-references​

DocumentRelation to this practice
08-rastreabilidade-vulnerabilidades.mdReinforces auditing and traceability
04-integracao-ci-cd.mdAutomated execution and validation of plans
02-matriz-requisitos-iac.mdRequirements IAC-001, REQ-004, REQ-005
SSDF (PW.5) / SLSA (Build L2)Normative requirements on controlled execution

🔐 This control is essential to ensure integrity, traceability and compliance in the lifecycle of infrastructure as code - being one of the pillars of maturity in secure IaC according to the SbD-ToE model.