# Infrastructure as code for an inherited cloud account

> How to bring an inherited AWS, Google Cloud or Azure account under infrastructure as code: inventory, Terraform import, review, a staging rebuild, drift checks.

URL: https://webxlr8.com/blog/infrastructure-as-code-inherited-cloud-account/

By Muhammed Azharudheen K J, Founder, CTO & Director. Published 30 November 2026.

Many companies run production on a cloud account that nobody fully understands. It was set up by hand in the console by a contractor, a former employee or a mix of both, over several years. It works. Nobody wants to touch it, because nobody knows which of its resources matter.

Infrastructure as code fixes that without a rebuild. You can bring an existing account under code one resource at a time, with production running throughout. The work has five steps, and each one leaves the account in a better state than it found it.

## Find what exists

Start with a full inventory of the account, region by region. The console shows one service in one region at a time, so use the provider's inventory tools instead.

- **AWS**: Resource Explorer, or the AWS Config resource inventory, across every region you use
- **Google Cloud**: Cloud Asset Inventory, for example `gcloud asset search-all-resources` at the project or organization level
- **Azure**: Azure Resource Graph, which lists resources across subscriptions in one query

Export the list and sort it into three groups. Some resources are clearly in use. Some are clearly abandoned, such as a test server left over from an old project or a snapshot of a database that no longer exists. The rest are resources nobody can explain, and that last group is where the risk lives. Check their billing, their network connections and their access logs before deciding anything.

Look at identity and access at the same time. List every user and role, and every access key, with when each was last used and what it can do. Inherited accounts often have long-lived keys with full administrator rights, created for a deployment script years ago. Those go on the list for [cloud hardening](https://webxlr8.com/cybersecurity/cloud-hardening/), whatever else happens.

## Import it into code

Write Terraform for the resources you are keeping, and import each one into state, so Terraform manages what already exists instead of creating a copy.

Since Terraform 1.5, imports can be declared in code, which makes them reviewable like any other change:

```hcl
import {
  to = aws_s3_bucket.assets
  id = "your-app-assets"
}
```

Running `terraform plan -generate-config-out=generated.tf` then writes a first draft of the resource configuration from the live settings. Treat the generated file as a starting point. Rename resources so they make sense, remove arguments that only repeat defaults, and split the code into modules that match how the system is organized. OpenTofu supports the same import blocks.

Work in order of dependency: the network first, then databases and storage, then compute, then DNS and the content delivery layer. Import a small group, run a plan, and carry on until the plan for that group shows no changes. A clean plan is the proof that the code describes what is running.

Store the state remotely, with locking, before more than one person works on it. A state file on one laptop is a single point of failure for the whole account.

## Review in pull requests

Once the code exists, every change to the account goes through it. A change is a pull request. The pull request runs `terraform plan` and posts the output for review, and the merge applies it.

The plan output is what reviewers read. It lists exactly what will be created, changed or destroyed, and the summary line at the end shows the size of the change at a glance. The first real change after an import is often a fix the inventory turned up, such as a storage bucket that anyone on the internet could read.

Remove console write access for a service once the code covers it. People keep read access for investigating problems. Write access through the console is how drift starts.

## Rebuild a staging copy

The strongest test of the code is to build a second environment from it. Apply the same modules to a separate staging account with smaller instance sizes, and see what fails.

Expect some of it to fail the first time. A security group rule that only ever existed by hand, a parameter set in the console and never imported, a certificate created in a different region from the one the code expects. Each failure is a piece of production the code did not describe, and each fix makes the code more complete. When staging builds cleanly from the repository, you have a description of production you can trust, and a place to test every future change before it reaches production.

Staging also gives you somewhere to rehearse the operations you never want to try first in production: a database version upgrade, a region move, a restore from backup.

## Keep drift out

Drift is any difference between the code and the account. It creeps back in through emergency fixes in the console, tools that change their own settings, and people who were never told about the new process.

Catch it with a scheduled check. A pipeline job that runs `terraform plan -detailed-exitcode` each night returns exit code 2 when the account no longer matches the code, and that result can open a ticket or alert the team. Each drift report gets one of two answers: bring the change into code, or revert it.

Keep the process light enough that nobody is tempted to skip it. An emergency change through the console is acceptable if it is imported or reverted on the next working day and recorded in the incident note. A rule that forbids it outright gets ignored the first time production is down.

When we take on an existing account, the [cloud architecture](https://webxlr8.com/services/cloud-and-managed-operations/cloud-architecture/) work runs in this order: we find what exists, import it into Terraform, rebuild a staging copy from it before changing production, and review every later change in a pull request. The code lives in your repository, so it stays with you whoever runs the account.

---

WebXLR8 is a brand of YIB Global Technology Services LLP, Kochi, Kerala, India. Book a scoping call: https://webxlr8.com/book/. Email: contact@webxlr8.com.
