Key takeaway
- Infrastructure as code (IaC) manages servers, networks and cloud resources through versioned files. See how it works, the tools, and how to start.
Infrastructure as code (IaC) is the practice of defining servers, networks and cloud resources in files that are versioned, reviewed and applied by tools, instead of configuring machines by hand. AWS describes the principle as treating infrastructure the same way developers treat code.
This guide is for DevOps engineers, platform teams and engineering managers who want to understand how IaC works, how it fits continuous delivery, and how to adopt it safely. It covers the declarative and imperative styles, the link to CI/CD, best practices, and what AI agents change. See also the IaC best practices and benefits guide.
What is infrastructure as code?
The AWS DevOps whitepaper says that practising infrastructure as code means "applying the same rigor of application code development to infrastructure provisioning". It adds that configurations should be defined in a declarative way and stored in a source control system, the same as application code.
HashiCorp describes Terraform as an infrastructure as code tool that lets you build, change and version cloud and on-prem resources, using readable configuration files that can be versioned, reused and shared. The idea is the same across tools: the files are the record of what should exist, and a change to infrastructure starts as a change to a file. The IaC tools guide compares the options in more depth.
How does infrastructure as code work?
Most IaC tools follow one of two styles. A declarative tool describes the end state and works out the steps. An imperative tool runs a sequence of commands in a set order. Terraform's introduction says its configuration files are declarative, meaning that they describe the end state of your infrastructure.
Terraform's documented workflow has three stages. In write, you define resources, possibly across several providers. In plan, Terraform produces an execution plan showing what it will create, update or destroy. In apply, after approval, it carries out the changes in the correct order, respecting resource dependencies. It records real infrastructure in a state file that acts as a source of truth for your environment.
| Question | Declarative | Imperative |
|---|---|---|
| You write | The desired end state | The ordered steps to reach it |
| The tool decides | What to create, change or remove | Nothing: it runs the steps given |
| Re-running | Designed to converge on the same state | Depends on how the steps are written |
| Typical use | Provisioning cloud resources | Scripted, one-off or procedural tasks |
Is infrastructure as code just automation?
No. A shell script automates a task. IaC adds engineering discipline to it: source control, review, testing and repeatable runs. AWS contrasts IaC with traditional scripts and manual runbooks, which it says can lead to environments that are not always repeatable, reliable or consistent. Tools such as Ansible and Terraform are used for this, with Terraform focused on provisioning resources and Ansible commonly used for configuration.
How does IaC fit with CI/CD?
Continuous integration merges code changes into a shared repository and tests them. Continuous delivery keeps each tested change ready to release. When infrastructure definitions live in the same repository workflow, they can pass through the same stages: a pull request, automated checks, a plan, an approval and an apply. Tools such as GitLab CI and Jenkins run those pipelines. See DevOps processes and DevOps tools for the wider picture.
What are the best practices for infrastructure as code?
- Make the files the source of truth. Keep every definition in version control so changes are audited and reversible, as AWS advises.
- Review the plan before applying. A plan step shows what will be created, updated or destroyed before anything changes.
- Test and monitor configurations. IaC is code, so it can be linted and tested before it reaches production.
- Be specific. Define storage, network and instance requirements explicitly, and involve application developers who know what the workload needs.
- Control cost and blast radius. Cloud services typically bill for what is running, so a faulty definition can create many resources quickly. Limit who can apply changes, for example on AWS.
The six principles of IaC go further on each of these.
How do AI agents change infrastructure as code?
Agents can draft IaC changes, read a plan and flag risky differences, and open pull requests for a human to approve. That makes the guardrails around IaC more important, not less: an agent that can apply changes needs scoped credentials, a mandatory plan review for destructive actions, and a record of what it changed and why.
ElixirClaw, XenonStack's AI agents computing platform, is where teams build and run such agents. To adopt IaC in your own estate, see how XenonStack can help with DevOps.
What should you do first?
- Pick one environment you rebuild often and describe it in a declarative tool.
- Store the files in version control with a required review.
- Add automated checks and a plan step to your pipeline.
- Restrict who and what can apply changes, including AI agents.
- Retire manual changes: if it was not in the files, it should not exist.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is infrastructure as code in simple terms?
It is defining servers, networks and cloud resources in versioned files and using a tool to apply them, instead of configuring machines by hand.
What is the difference between declarative and imperative IaC?
Declarative IaC describes the end state and lets the tool work out the steps. Imperative IaC lists the ordered commands to run.
Is Terraform declarative?
Yes. Terraform's introduction says its configuration files are declarative, meaning that they describe the end state of your infrastructure.
How does IaC relate to CI/CD?
Infrastructure definitions can live in the same repository workflow as application code, so changes pass through review, automated checks, a plan and an apply.
Can AI agents manage infrastructure as code?
They can draft changes and review plans, but they should use scoped credentials, require human approval for destructive actions and leave an audit record.
Ready to put AI agents to work? See an ElixirClaw demo: the agentic OS to build, orchestrate and govern AI agents across your enterprise systems.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE