Skip to content

Infrastructure as Code: Work Environments We Can Rebuild

A modular glass configuration builds three related infrastructure environments with separate state and a controlled approval checkpoint.

Series II: From Infrastructure to Agentic Projects in Production. Episode 05 of 20.

In the previous episode, we separated the identities and credentials of the fictional B2B portal for requests and documents. The next step is to turn decisions about networks, services, and access into configurations that can be read, reviewed, and recreated. The main principle is that an approved change in code and a validated plan become the standard path to infrastructure, while manual interventions are documented exceptions that must be reconciled.

What is Infrastructure as Code

Infrastructure as Code, shortened IaC, means describing infrastructure resources in files that can be versioned and processed by a tool. OpenTofu is an open-source example that can manage both cloud and on-premises resources through human-readable configurations. We use it here to explain the method, not as a mandatory choice for every company.

Code can describe networks, virtual machines, rules, storage, DNS, or managed services, depending on available providers. It doesn’t automatically include the application, customer data, backups, or incident procedures. Nor does it turn a poor architecture into a secure one. IaC makes intent repeatable and differences visible; the quality of the outcome still depends on decisions, access, and checks.

Minimal Portal Structure

Hypothetical scenario: the portal has development, test, and production environments. In the infrastructure repository, we keep modules for network, compute, storage, and technical identities. Each environment has its own approved values and its own state. Reusing modules reduces accidental differences but doesn’t require environments to be identical. Production can have redundancy and stricter policies, while development might use smaller resources and synthetic data.

A simple structure could include:

  • modules, for reusable components such as network and application services;
  • environments/dev, test, and prod, for each environment’s composition and values;
  • automated checks for format, validation, and policies;
  • documentation on owner, order of application, recovery, and exceptions.

Secrets are not stored in versioned files. The configuration can reference an identity or controlled location from which an authorized process retrieves them. AI agents developing the portal can propose changes in this structure. The agent integrated into the portal, which classifies a request, doesn’t need to modify the infrastructure.

Flow: Write, Plan, Review, Apply

OpenTofu documentation describes three basic steps: write, plan and apply. In a team, we turn these into operational control.

  1. Changes are made on a branch and explained in a pull request.
  2. A controlled environment validates the configuration and generates the plan, which is a preview of proposed actions.
  3. The reviewer compares intention and plan: what is added, what is changed, what is replaced or deleted, whether interruptions occur, and who they affect.
  4. After approval, the final plan is generated for the same commit, using the current state, then applied via the target environment’s identity.
  5. The team checks the service and records the result. If the application is partial, don’t assume reverting the code undoes all effects.

The plan seen in the pull request may differ from the final one if infrastructure, the order of integrations, or the state has changed in the meantime. That’s why an old plan isn’t applied mechanically. We regenerate and re-review it when the context has changed. Plan files may contain sensitive values, so they should not be published or attached without control.

For a small company, approval can belong to a single designated person, but author and approver should be separate for high-risk changes. A complex platform is not needed from day one. However, a clear application path, a change history, and an emergency rule are required.

State Is Not Just Any File

State, or the state of infrastructure, links actual resources to the configuration’s declared objects. OpenTofu uses it to decide what needs to be created, changed, or removed. State can contain identifiers and sensitive attributes, including default passwords for some resources. Marking a value as “sensitive” can hide it from display, but doesn’t guarantee its removal from state.

In a team, we keep state in a controlled backend with minimal access, encryption, history, and appropriate backups. We explicitly check if the backend supports locking, i.e. blocking concurrent writes. OpenTofu locks automatically when the backend supports it, but not all implement this. Forcing unlock without confirming who owns the operation can allow two writes and corrupt state.

OpenTofu can encrypt state and plan files, but the encryption key then becomes a critical dependency. Losing it can make state unrecoverable. Encryption doesn’t protect against file loss, use of an old plan, or someone running the tool with legitimate access. The recovery procedure must be tested, not just written down.

IaC state is not a backup for the portal’s database and documents. It can help reconstruct resources, but persistent content requires separate backups, retention, and tested restore.

Drift: the gap between code and reality

Drift means the discrepancy between the approved configuration and the existing infrastructure, for example after a manual change made in the console. An updated plan can reveal the differences, but remediation shouldn't be blindly automated. The change may be accidental, an emergency intervention that's still needed, or a sign that the code model is incomplete.

The rule for the portal is simple: we identify the owner, the reason, and the impact, then explicitly choose whether to revert to the code or adopt the change in the configuration. After an emergency, we update the plan and reconcile the code before the next delivery. This way, the console doesn't become a separate source of truth known only to one person.

What we allow development agents to do

An AI agent can prepare a pull request, explain the plan, and flag unusual resources. It doesn’t automatically get production credentials and cannot approve its own changes. We rerun validation and security checks after every fix, not just at the end. A study published at ESEM 2026 observed security regressions in some iterative IaC repair loops with language models; results vary based on the detection method, so they shouldn’t be generalized to every tool. The practical takeaway is that a functional fix can also affect other properties.

The piece added to the project is the minimal repository for environments and the rule: no changes are applied in production without a final plan linked to a commit, review, and authorized identity. The result doesn’t promise perfectly identical infrastructure, since images, provider APIs, and external services can change. For a realistic rebuild, we pin important versions, preserve required artifacts, and periodically test a fresh environment. In the next episode, we’ll turn project objectives into verifiable tasks for both people and AI agents.

Sources

Build reproducible environments

Do you want reproducible environments and verifiable infrastructure changes? Talk to i8 about an Infrastructure as Code workflow tailored to your team.