Home / Insights / Architecture and Modernization

Architecture and Modernization

Bringing Hand-Built Cloud Infrastructure under Infrastructure as Code

Most cloud environments start with a few resources created by hand in a console. Bringing them under infrastructure as code later is entirely possible, but it needs a careful order of work so production keeps running while the code catches up.

TopicsArchitecture and ModernizationWeb and SaaS Development

Key takeaways

  • Infrastructure as code (IaC) makes environments reproducible, reviewable and recoverable; hand-built infrastructure is none of those.
  • Start with an inventory and a risk ranking, then import resources in small batches, beginning with low-risk ones.
  • Terraform import blocks, optionally with generated configuration, bring existing resources under management without recreating them.
  • A plan with no changes after import is the test that code and reality match. Review every proposed change before applying.
  • Once a resource is in code, stop console changes to it. Drift detection and restricted permissions keep it that way.

A familiar story: a product launched quickly, someone created the database, the load balancer and a few servers in the cloud console, and it worked. Over time more resources were added, some by people who have since left. Now nobody is entirely sure which security group rules matter, a staging environment is “roughly” like production, and rebuilding anything after an incident would mean clicking through screens from memory.

Infrastructure as code (IaC) fixes this by describing infrastructure in version-controlled files that tools such as Terraform, OpenTofu, AWS CloudFormation or Azure Bicep apply. Starting with IaC on a new project is straightforward. Retrofitting it to a live, hand-built environment is where teams get nervous, and rightly so: a careless apply can delete or replace production resources. This article sets out a safe order of work.

Why retrofit infrastructure as code at all

The business case is usually one of these:

  • Recovery. Rebuilding an environment from code is far faster and more reliable than rebuilding it by hand. Disaster recovery strategies such as backup and restore depend on it.
  • Consistency. Staging and production built from the same code behave the same way, so tests in staging mean something.
  • Review and audit. Infrastructure changes go through pull requests, with a history of who changed what and why.
  • Security. Network rules, permissions and encryption settings are visible and can be scanned automatically.
  • Onboarding and handover. New engineers, or a new partner, can read the infrastructure rather than reverse-engineer it.

If you are inheriting an environment from another team, IaC adoption often belongs in the first phase of the handover; see our checklist for taking over an inherited codebase.

Step 1: Inventory and plan

Before writing code, find out what exists.

  1. List resources per account and region. Cloud providers offer resource inventories and tagging reports. Include networking, compute, databases, storage, DNS, certificates, identity and access policies, queues and scheduled jobs.
  2. Identify owners and purpose. For each resource, record which application uses it. Resources nobody can explain are candidates for removal, but confirm before deleting.
  3. Rank by risk. Stateful resources (databases, storage buckets) and shared networking carry the most risk. Stateless and isolated resources carry the least.
  4. Choose the tool. Terraform or OpenTofu suit multi-cloud and broad provider coverage; CloudFormation, CDK or Bicep suit teams committed to one provider. Pick one and be consistent.
  5. Design the structure. Decide how code is split: per environment, per application, per layer (network, data, compute). Decide where state is stored and who can access it.

Step 2: Import existing resources safely

The goal is to bring each resource under management without recreating it. In Terraform, an import block declares the existing resource’s ID and the address it will have in configuration. Terraform can also generate starter configuration for imported resources when you run a plan with the -generate-config-out option, which works well for single resources or small batches. For large sets, HashiCorp documents a bulk import workflow.

A safe import loop

  1. Pick a small batch of related, low-risk resources.
  2. Write import blocks and generate or write the matching configuration.
  3. Run a plan. The target is no changes: code and reality match.
  4. If the plan proposes changes, adjust the code to match reality. Do not “fix” production settings during import; that is a separate, reviewed change.
  5. Watch especially for any plan that proposes to replace or destroy a resource. Stop and investigate.
  6. Apply, commit, and move to the next batch.

Generated configuration is a starting point. Tidy it into readable modules and variables once the import is stable.

Step 3: State, structure and environments

IaC tools keep a record, the state, mapping code to real resources. Treat it carefully:

  • Store state remotely with locking, so two people cannot apply at once.
  • Restrict access: state can contain sensitive values.
  • Back up state and enable versioning on its storage.
  • Keep state files reasonably small. One huge state for everything makes plans slow and changes risky.
Structure choiceBenefitTrade-off
Separate state per environmentA mistake in staging cannot touch productionSome duplication, managed with modules
Separate state per layer (network, data, apps)Smaller blast radius; network rarely changesOutputs must be shared between layers
Shared modulesConsistent patterns across environmentsModule changes need versioning and testing

Step 4: Pipelines, permissions and drift

Once resources are in code, the process has to change, or the code will drift away from reality again.

  • Apply through a pipeline. Pull requests show the plan; merges apply it. Our guide to designing a CI/CD pipeline covers quality gates that apply equally to infrastructure.
  • Restrict console write access. Engineers keep read access and break-glass emergency access, but routine changes go through code.
  • Detect drift. Run scheduled plans against each environment and alert when reality differs from code.
  • Scan for security issues. Static analysis of IaC catches open storage, overly broad permissions and unencrypted resources before they are applied. See our DevSecOps checklist.
  • Tag consistently. Owner, application, environment and cost centre tags make cost and ownership visible.

Common pitfalls

  • Importing everything at once. Large batches produce noisy plans where a dangerous change is easy to miss.
  • Changing settings during import. Mixing “make code match reality” with “improve reality” hides risky changes.
  • Ignoring default values. Some provider defaults differ from what the console set, producing unexpected diffs.
  • Secrets in code. Keep passwords and keys in a secrets manager and reference them; never commit them.
  • No owner for the code. IaC needs maintenance: provider upgrades, module updates and reviews.

PoliteTech’s cloud and DevOps team defines infrastructure as code using Terraform or cloud-native tools, and can work inside your existing cloud account with access you grant.

Frequently asked questions

Can we adopt Terraform without downtime?

Usually, yes. Importing existing resources does not recreate them. The risk comes from applying unreviewed changes, so aim for a plan with no changes after each import and investigate any proposed replacement.

Should we use Terraform or our cloud provider’s native tool?

Terraform or OpenTofu suit multi-cloud estates and broad provider coverage. Native tools such as CloudFormation or Bicep suit teams fully committed to one provider. Consistency matters more than the choice itself.

What about resources we cannot explain?

Tag and monitor them first. If nothing uses them after a reasonable period and owners confirm, remove them through a reviewed change rather than importing them.

Do engineers lose console access?

They should keep read access and an emergency path, but routine changes should go through code so the code stays accurate.

Sources and methodology

Guidance reflects PoliteTech delivery experience. The external references below informed specific points in this article.

  1. HashiCorp Developer: Import resources overview (Terraform) — Supported how import blocks work, configuration generation for single or small batches of resources, and the bulk import option for large sets.
  2. AWS Whitepaper: Disaster recovery options in the cloud — Supported the point that infrastructure as code is essential for meeting recovery time objectives with backup and restore.

Could you rebuild your cloud environment from code today?

We can inventory your existing resources, plan a safe import order and set up the pipeline and drift checks that keep code and reality aligned.