Why infrastructure as code
When infrastructure is created from the console, every environment ends up slightly different and nobody can rebuild it with confidence. With infrastructure as code (IaC), networks, databases and clusters are described in versioned files: they are reviewed like any other change, reproduced identically in every environment and can be rebuilt after an incident.
Terraform is the most widely used tool for this on AWS because it uses a declarative language (HCL), has a very complete AWS provider and also manages third-party services such as DNS, monitoring or repositories.
Remote state and locking from the first commit
Terraform keeps track of the resources it created in a state file. That file should never live on a laptop: on AWS the usual setup is an S3 bucket with versioning and encryption, plus a locking mechanism so two people can't apply changes at the same time. Recent Terraform versions can lock state directly in S3, without a separate DynamoDB table.
Split state by environment and by component. A single giant state makes every plan take minutes and lets an error in one area block all the others.
Small, versioned modules with a clear purpose
One module per component (network, cluster, database, bucket with policies) with well-defined inputs and outputs. Modules are versioned with tags and each environment pins the version it uses, so you can upgrade staging first and production afterwards.
Avoid “all-in-one” modules with dozens of optional variables: they are hard to understand and any change affects every consumer.
Environments: folders before workspaces
For most teams, one folder per environment (dev, staging, prod) calling the same modules with different values works better than workspaces. It is explicit, allows separate AWS accounts per environment and makes it obvious what is about to change.
Splitting environments into separate accounts with AWS Organizations adds a real barrier: a mistake in dev cannot touch production even if someone runs a command in the wrong directory.
Plan on the pull request, apply from the pipeline
Every infrastructure change goes through a pull request: the pipeline runs formatting, validation, a security scan (tfsec, Checkov or similar) and posts the plan as a comment. Reviewers see exactly what will be created, changed or destroyed.
The pipeline runs the apply with a dedicated AWS role and temporary credentials via OIDC, never with a user's access keys. That leaves a trail of who approved each change and nobody needs administrator permissions on their machine.
Frequently asked questions
Want to apply this at your company?
Book a free 30-minute technical consultation.
