Spain · 2026 World Cup champions
AXTO
EngineeringJuly 21, 2026· 8 min read

Infrastructure as Code: Security's Best Friend and Newest Risk

Defining infrastructure in code makes environments consistent and auditable — and turns a single misconfiguration into a repeatable one. How to get the upside safely.

Infrastructure as code — defining servers, networks and cloud resources in version-controlled files rather than clicking through consoles — has transformed how systems are built. For security it is a double-edged gift. On one edge, it makes environments consistent, reviewable and auditable in ways manual setup never could. On the other, it means a single insecure pattern, once written, gets deployed everywhere automatically and repeatedly. The same mechanism that spreads good configuration spreads bad configuration just as efficiently.

Understanding both edges is how teams capture the security upside without amplifying mistakes.

Why it's a security win

Defining infrastructure in code brings the discipline of software engineering to systems that used to be assembled by hand and memory. Every change is version-controlled, so you have a complete history of who changed what and when. Changes can be reviewed before they are applied, catching insecure configurations in code review rather than in production. Environments become reproducible and consistent, eliminating the drift and one-off tweaks where misconfigurations hide. And the entire configuration is auditable at a glance — you can actually read what your infrastructure is, rather than clicking through a hundred console screens hoping nothing was missed.

This visibility and reviewability is a genuine leap for security. Misconfiguration is one of the leading causes of cloud breaches, and infrastructure as code makes misconfiguration something you can see, review and test.

Why it's a new risk

The flip side is that automation faithfully reproduces whatever it is told. An overly permissive firewall rule, a storage bucket left public, a database exposed to the world — written once into a reusable module, it deploys identically across every environment that uses it. A mistake that once affected one manually built server now affects fifty automatically built ones. And the code itself becomes sensitive: it describes your entire attack surface, and if it contains hard-coded secrets, those secrets are now in version control for anyone with repository access.

The lesson is that infrastructure code deserves the same security scrutiny as application code, because its blast radius is larger.

Getting the upside safely

A few practices tilt the balance decisively toward the upside. Scan infrastructure code automatically for insecure configurations before it is applied — dedicated tools catch public buckets, open security groups and missing encryption in the pipeline. Never store secrets in the code; reference them from a proper secrets manager. Enforce review on infrastructure changes just as you would on application changes. Apply least privilege to what the automation itself can do, so a compromised pipeline cannot reshape everything. And use the version history as the security asset it is, to audit and, when needed, roll back.

Done this way, infrastructure as code becomes exactly what security always wanted: infrastructure that is consistent, reviewed, tested and auditable. The trick is remembering that the code defines your attack surface, and treating it with the seriousness that surface deserves.

The common misconfigurations

A small number of patterns account for a large share of cloud security findings, and infrastructure code is where they can be stopped. Storage buckets readable by anyone. Security groups or firewall rules that open administrative ports such as SSH or remote desktop to the whole internet. Databases with public endpoints. Encryption at rest switched off. Logging and audit trails disabled. Identity policies granting wildcard permissions on all resources. Default credentials left on managed services. Each of these is easy to write by accident and easy to detect automatically, which is why scanning infrastructure code pays off so quickly.

Policy as code

Written guidelines are hard to enforce consistently; executable rules are not. Policy as code expresses security requirements — every bucket must block public access, every database must be encrypted, no rule may open port 22 to the world — as automated checks that run against infrastructure definitions and plans. The same rules can run on a developer's machine, in the pull request and in the deployment pipeline, so problems surface early, when they are cheap to fix. Keep the policies in version control, review changes to them like any other code and document the reason behind each rule.

Secure modules as paved roads

Most organizations repeat the same building blocks: a storage bucket, a database, a virtual network, a container service. Wrapping these in shared, reviewed modules with secure defaults turns good practice into the easiest option. A team that needs a bucket uses the approved module and gets encryption, access logging and public-access blocking automatically, without having to remember them. Version the modules, publish clear documentation and changelogs, and make secure configuration the path of least resistance rather than an extra burden.

Keeping secrets out of the code and the state

Hard-coded secrets in infrastructure files are an obvious risk, but a less obvious one is state. Many infrastructure tools keep a state file recording the current configuration of every resource, and it can contain sensitive values such as database passwords or generated keys in plain text. Store state remotely in a backend that encrypts it, restrict access to the few identities that need it, enable versioning so it can be recovered, and use locking to prevent simultaneous changes. Reference secrets from a dedicated secrets manager so that they never appear in the code itself.

Protecting the pipeline

The automation that applies infrastructure changes typically holds powerful credentials, which makes it a prime target. Use short-lived credentials obtained through workload identity federation rather than long-lived access keys stored in pipeline settings. Separate the permissions for planning, which needs read access, from applying, which needs write access, and protect the apply step with required reviews and branch protection. Pin the versions of the tools and providers the pipeline uses, and verify their integrity, so that a compromised dependency cannot quietly change what gets deployed.

Drift detection

Infrastructure code is only a reliable description of reality if nobody changes resources by hand. In practice, people make urgent fixes in the console, and attackers who gain access may change configurations directly. Run regular drift detection that compares the live environment with the code and reports differences. Each difference should be either brought back into the code or reverted. Unexplained drift on security-relevant settings, such as a firewall rule or an identity policy, deserves the same attention as any other security alert.

Reviewing plans, not just code

A small change to a module or variable can have large consequences: replacing a database instead of updating it, opening a rule to a wider range than intended, or deleting a resource that something else depends on. Most tools can produce a plan showing exactly what will be created, changed and destroyed. Make reviewers look at that plan, not only the code diff, and flag destructive or security-sensitive changes automatically. For critical resources, enable deletion protection so that an accidental plan cannot remove them without a deliberate extra step.

Least privilege for the resources themselves

Infrastructure code often defines identities and permissions as well as servers and networks. It is tempting to grant broad permissions to make things work quickly, and the code then reproduces that over-permissiveness everywhere. Write narrow policies that grant specific actions on specific resources, use condition keys to restrict context, and run tools that analyze policies for excessive access. Review the permissions granted to service accounts as carefully as those given to people, because automated identities are frequently the easiest way for an attacker to move sideways.

Testing infrastructure code

Infrastructure deserves tests just as application code does. Static checks catch syntax errors and policy violations. Plan-based tests verify that a change produces the expected resources with the expected settings. Short-lived test environments, created and destroyed automatically, confirm that modules actually work and that security controls behave as intended. Running these tests on every change prevents regressions, such as a refactor that silently removes encryption from a module used by every team.

Recovery and incident response

Infrastructure as code is a powerful recovery tool. If an environment is compromised or destroyed, the code can rebuild it consistently and quickly — provided the code, the state and the pipeline are themselves protected and backed up. Keep the repository and state in locations an attacker in the production environment cannot reach, and practise rebuilding a non-production environment from scratch. In an incident, the version history also helps investigators see exactly which configurations changed, when and through which approved process.

Where to begin

Start by adding an infrastructure scanner to the pull request process in report-only mode, and fix the most serious findings it surfaces. Move state to an encrypted, access-controlled backend and remove any secrets from the code. Introduce secure shared modules for the resources you create most often. Then add drift detection, enforce policies that block the riskiest patterns, and tighten the pipeline's permissions. Each step is modest on its own; together they turn infrastructure code into one of the strongest security controls you have. Revisit the rules every few months, because cloud providers add services and settings faster than any policy can anticipate.

Run AI on your own terms

AXTO gives you world-class AI apps and self-hosted editions — on your own keys, your own data.