How to Implement Infrastructure as Code Step by Step
Infrastructure as code means defining servers, networks and databases in versioned files, reviewing changes like code and applying them through a pipeline.
On this page
Short answer
Infrastructure as code is the practice of defining servers, networks and databases in version-controlled files instead of setting them up by hand. Start with one tool and one environment, write small reusable modules, store state remotely with locking, and review the plan before every apply.
What is infrastructure as code?
Infrastructure as code is the practice of defining servers, networks, databases and permissions in version-controlled files instead of setting them up by hand. The files describe the desired state, and a tool creates or changes the infrastructure to match them.
The change is the same one that version control brought to application code. When infrastructure is code, it can be reviewed, tested, rolled back and reused. When it is set up by hand, it lives in one person's head and breaks when they leave or forget a step.
A typical setup keeps the code in a repository, reviews changes through pull requests and applies them through a pipeline. The tool compares the desired state with the real state and makes only the changes needed, which means applying the same code twice does not create duplicate resources.
Infrastructure as code is not a single tool. It is a way of working that covers how resources are defined, how changes are reviewed and how environments are reproduced. For a plain definition of the pieces, what is infrastructure as code covers the core ideas.
Which infrastructure as code tool should you start with?
The tool choice matters less than the habit of defining infrastructure in code. Pick a tool your team can read and review, and one that supports the cloud platforms you actually use. The table compares the common options.
| Tool | Best for | Language | Learning curve |
|---|---|---|---|
| Terraform | Multi-cloud infrastructure | HCL, declarative | Medium |
| Pulumi | Teams that prefer code | TypeScript, Python, Go | Medium |
| AWS CloudFormation | AWS-only environments | YAML or JSON | Low to medium |
| Ansible | Configuration on existing servers | YAML playbooks | Low |
| Bicep | Azure-only environments | Bicep, declarative | Low to medium |
Terraform is the usual starting point for teams that run more than one cloud or want the widest community support. It is declarative, which means you describe what you want and the tool works out how to get there. For a shortlist of the tools themselves, top infrastructure as code tools compares the common options.
Ansible solves a different problem: configuring servers that already exist, such as installing packages and writing config files. Many teams use Terraform to create resources and Ansible to configure them, which is a sensible combination.
How to implement infrastructure as code in 6 steps
Implementation works best as a sequence that starts small and grows. The steps below take a team from hand-built infrastructure to a reviewed, repeatable process without a risky big-bang migration.
- Inventory what you have. List the servers, databases, networks and permissions that exist today. Most teams find resources nobody remembers creating.
- Pick one tool and one environment. Choose the tool from the table above and start with the least critical environment, usually development or staging.
- Write the first module. Define one piece of infrastructure, such as a virtual network or a database, in code. Keep the module small enough to review in one sitting.
- Store state remotely. Keep the tool's state file in shared storage with locking, so two people cannot apply conflicting changes at the same time.
- Review changes like code. Run a plan step that shows what will change, and require a peer review before anything is applied to production.
- Roll out environment by environment. Apply the same code to staging, then production, fixing the differences between environments as you go.
The first environment is the hardest because the team is learning the tool. After that, each new environment is mostly a copy with different values, which is where the approach starts to pay back the effort.
How do you organise modules and state?
Modules and state are where infrastructure as code either stays manageable or turns into a mess. A module is a reusable block of code for one piece of infrastructure, such as a database with its backups and access rules. State is the record of what the tool has created, and it must be shared and locked.
Keep modules small and focused. A module that creates a network, a database and a server is hard to reuse, because the next project rarely needs all three together. A module that creates one database with sensible defaults can be reused everywhere.
State should live in remote storage with locking enabled, never on one person's laptop. If two people apply changes at the same time without locking, the state file can end up describing infrastructure that does not exist. Cloud storage with a lock table is the standard setup for the common tools.
Use one state file per environment rather than one for everything. A single state file for development, staging and production means a mistake in one environment can affect the others, and the plan output becomes too long to review properly.
Example (hypothetical): moving 36 servers into code
Example (hypothetical): a team runs three environments with 12 servers each, 36 servers in total. Setting up one server by hand takes about 2 hours, so rebuilding all 36 by hand would take 72 hours of work, and each server would end up slightly different.
The team writes modules for the server, the network and the database, then applies the same code to each environment. Applying the code takes about 20 minutes per environment, so all three environments take about 1 hour in total, and every server is configured the same way.
The saving is not just the 71 hours. When a server fails, the team recreates it from code in minutes instead of rebuilding it from memory. When an auditor asks how the servers are configured, the team shows the code and the review history instead of guessing.
The team keeps one difference per environment in a small variables file: the environment name, the instance size and the backup schedule. Everything else is shared, so a fix in the module reaches all three environments at once.
How do you test and review infrastructure changes?
Testing infrastructure code is different from testing application code, because the result is real cloud resources. The review process has three layers: a plan, a policy check and a peer review.
The plan step shows exactly what will change before anything is applied: which resources will be created, changed or destroyed. A plan that destroys a database should stop the change immediately, and the pipeline should refuse to apply a plan that nobody has reviewed.
Policy checks are automated rules, such as requiring encryption on storage, blocking public access to databases and tagging every resource with an owner. These checks run in the pipeline and fail the change if a rule is broken, which is faster and more reliable than remembering the rules in review.
Peer review is the last layer. A reviewer checks that the change does what the description says, that the plan output matches, and that the change is safe to apply. The reviewer does not need to be a specialist, but they do need to read the plan, not just approve the code.
What are the common mistakes?
The most common infrastructure as code mistakes are keeping state on a laptop, writing one giant module and applying changes without reviewing the plan.
- Local state files. State on one person's laptop cannot be shared or locked, and losing it means the tool no longer knows what exists.
- One giant module. A module that creates everything cannot be reused or reviewed. Split it into small, focused modules.
- Skipping the plan review. Applying code without reading the plan is how databases get destroyed. Review every plan before applying.
- Hardcoding secrets. Passwords and keys belong in a secrets manager, never in the code repository.
- Different code per environment. Copying and editing code for each environment creates drift. Share the code and vary only the values.
- No rollback plan. Every change should be reversible, either by reverting the code or by restoring from a backup.
If the team needs help setting up the pipeline and the review process, DevOps services cover infrastructure as code, pipelines and state management as a combined engagement.
Key takeaways
- Infrastructure defined in code can be reviewed, tested and rolled back
- Start with one tool and one low-risk environment, not a full migration
- Store state remotely with locking, never on one person's laptop
- Keep modules small and share code across environments, varying only values
- Review the plan output before every apply, especially for destructive changes
Frequently asked questions
What is the difference between Terraform and Ansible?
Terraform creates and manages cloud resources such as servers, networks and databases, while Ansible configures servers that already exist, such as installing packages and writing config files. Many teams use both together.
Do I need to rewrite all my infrastructure at once?
No. Start with one environment and one piece of infrastructure, such as a network or a database. Prove the process works there, then extend it environment by environment until everything is covered.
What happens if the state file is lost?
The tool loses track of what it created, which makes later changes risky. That is why state should live in remote storage with versioning enabled, so it can be recovered, and why locking matters.
Can infrastructure as code work with multiple clouds?
Yes. Tools such as Terraform support many cloud providers, and the same review and pipeline process applies. Keep cloud-specific code in separate modules so each provider's setup stays clear and independent.
How do I handle secrets in infrastructure code?
Store passwords, keys and tokens in a secrets manager and reference them from the code. Never commit secrets to the repository, even in a private one, because history keeps them.
How long does it take to adopt infrastructure as code?
A first environment usually takes a few weeks, including learning the tool and writing the first modules. Later environments are faster because they reuse the same modules and process, so the pace picks up quickly.