Skip to main content
December 1, 2026

How to Verify Agent-Written Terraform for an AWS Exit

How to Verify Agent-Written Terraform for an AWS Exit

"A coding agent wrote our Terraform, we left AWS for rented servers, and our bill dropped 10x." Stories like this are getting common, and some of them are true. But a headline number like that usually mixes three separate claims: which bill line went down, whether the new Terraform does what the team thinks it does, and whether the new setup is as reliable as the old one. This guide shows how to check each claim before you repeat it to your CFO: what to port first, how to prove cost and reliability parity, and which parts of an AWS exit an agent should not own.

The commands below apply to Terraform 1.5 and later (import blocks need 1.5). The current release on the Terraform install page is 1.17.2.

Step 1: Find the Bill Line You Are Actually Cutting

Start with the bill, grouped by service, for full months before the move:

aws ce get-cost-and-usage \
    --time-period Start=2026-08-01,End=2026-11-01 \
    --granularity MONTHLY \
    --metrics "UnblendedCost" "AmortizedCost" \
    --group-by Type=DIMENSION,Key=SERVICE

The Cost Explorer CLI reference documents the output: a ResultsByTime list, one entry per month, each with Groups that carry Keys (the service name) and Metrics (an Amount and a Unit). Each Cost Explorer API request against your primary billing view costs $0.01, so pull the data once and save the JSON.

Now do the arithmetic that most cost stories skip. Here is an illustrative example, not real data:

Line Before After the move Change
EC2 compute $45,000 $4,500 (rented servers) 10x
RDS $20,000 $20,000 (not moved yet) none
S3 and data transfer $25,000 $25,000 none
Everything else $10,000 $10,000 none
Total $100,000 $59,500 1.7x

A 10x cut on the largest line is real money, but it is not a 10x cut on the bill. Ask which line the claim is about before you plan around it.

Step 2: Choose What to Port First

Port the parts where a bad Terraform plan is cheap to undo. Leave the parts where AWS does a lot of hidden work for later, and keep a human in charge of them.

Workload Port first? Why
Stateless services behind a load balancer Yes Easy to run in both places and shift traffic slowly
CI runners, batch and build jobs Yes Failures cost time, not data
Managed databases (RDS, Aurora) Late, human-owned Backups, failover and patching become your job
Object storage with large datasets Plan the egress first Data transfer out is a bill line of its own
IAM, KMS, managed identity Redesign, do not translate There is no one-to-one bare metal equivalent
CDN and edge (CloudFront) Separately Excluded from the AWS exit egress credits (see Step 6)

For more on where the risk goes when an agent does the porting, see our post on unattended agent migrations.

Step 3: Prove the Terraform Does What You Reviewed

An agent can write plausible HCL fast. The review has to be about the plan, not the code. Save the plan, review it, and apply the same file:

terraform validate
terraform plan -input=false -detailed-exitcode -out=plan.tfplan
rc=$?
case $rc in
  0) echo "no changes" ;;
  2) echo "changes present: review plan.tfplan" ;;
  *) echo "plan failed"; exit 1 ;;
esac
terraform show -json plan.tfplan > plan.json

The plan command docs define the exit codes of -detailed-exitcode: 0 means "Succeeded with empty diff", 1 means "Error", and 2 means "Succeeded with non-empty diff". Run a policy check such as Conftest on plan.json (see our Terraform auto-apply policy gates post), and then run terraform apply plan.tfplan, so the change that runs is the change that was reviewed.

Two traps from the docs matter when an agent is in the loop:

  • A saved plan contains your full configuration and sensitive values in cleartext, and terraform show -json prints sensitive state values in plain text. Do not paste either into an agent's context.
  • prevent_destroy = true makes Terraform reject plans that would destroy the resource. But the lifecycle docs say it does not prevent a destroy "if you remove its configuration". An agent that "cleans up" by deleting a resource block walks straight past the guard. Your policy check should deny every delete action on resources that hold data, not rely on prevent_destroy alone.

Step 4: Manage State and Drift

Keep state in a backend that supports locking. Per the state locking docs, Terraform locks state for every operation that could write it if the backend supports locking, and if locking fails, Terraform does not continue.

Bring hand-built servers under Terraform with import blocks instead of letting an agent run state commands. The syntax, from the docs example (use your bare metal provider's resource type in to):

import {
  to = aws_s3_bucket.this
  id = "example-bucket"
}

terraform plan -generate-config-out=PATH writes draft resource blocks for the imports. The flag is marked experimental. When an agent refactors module names, have it write moved blocks, which change a resource address so that Terraform "does not destroy the resource". Both land in a pull request you can review, unlike terraform state mv.

Then schedule a read-only drift check:

terraform plan -refresh-only -detailed-exitcode -input=false

The drift tutorial shows that drift appears as "Note: Objects have changed outside of Terraform". With -detailed-exitcode, exit code 2 tells your scheduler that something changed by hand. On rented hardware, someone will log in and fix things by hand during the first weeks, so expect drift and route it to a person.

Step 5: Prove Reliability Parity

Cost parity without reliability parity is a loan, not a saving. Compare the same signals over the same length of time, before and after:

  • Availability and p99 latency measured from outside, by the same synthetic checks, not by internal metrics that moved with the service.
  • Error rate per endpoint, from the same dashboards.
  • Time to recover from a host failure. On AWS, an Auto Scaling group often replaced failed instances for you; now test it yourself: power off one server and time the recovery.
  • A real restore from backup, timed, for every database you moved.
  • Hardware lead time. Ask the provider how long a replacement disk or server takes, and size spare capacity for it.

If the new setup needs more spare servers to match the old error budget, add them to the "after" column in Step 1.

Step 6: Plan the Exit Egress

AWS runs a free data transfer out program for customers who move off AWS. According to the EC2 FAQ, you must contact AWS Support and wait for approval ("Do not start your move before your credit request is approved"), and by the end of the transition period you must delete the remaining data and workloads from the services you are leaving, or close the account. Data transfer out from CloudFront, Direct Connect, Snow Family and Global Accelerator is not included. If you store less than 100 GB, the normal monthly free tier already covers the move. AWS can change the program, so read the current terms before you set a cutover date.

What an Agent Should Not Own in an AWS Exit

Task Agent Human
Write modules, import blocks and moved blocks Owns it Reviews the plan
Pull and compare per-service cost data Owns it Checks the totals
Run read-only drift checks Owns it Handles the drift
Database cutover and replica promotion No Owns it
DNS cutover and TTL changes No Owns it
terraform destroy on AWS resources No Owns it
state rm, state mv, force-unlock No Owns it
Egress credit request and contracts No Owns it

The rule is simple: an agent can do work that a saved plan, a pull request or a read-only command can show. Anything that loses data or traffic when it goes wrong needs a person with a rollback plan.

Troubleshooting

State lock error. If locking fails, Terraform stops. If a crashed run left the lock behind, Terraform outputs the lock ID when unlocking fails. Check that no other run is active, then run terraform force-unlock LOCK_ID (reference). Never let an agent run this on its own; a lock usually means another apply is in progress.

Plan rejected by prevent_destroy. The plan would replace or destroy a protected resource. Find the argument that forces replacement in the plan output and fix the config. Do not remove the guard to get a green run.

-generate-config-out fails. The target file must not already exist. Delete or rename the old generated file first.

terraform show -json gives incomplete plan data. The plan must be created without -refresh=false. Create it again with refresh enabled.

Cost Explorer numbers do not match the invoice. Check the Estimated field in ResultsByTime: the current month is not final. Compare AmortizedCost too, because Savings Plans and Reserved Instances spread upfront fees across months.

Where Akmatori Fits

After the cutover, your on-call team watches servers that AWS used to watch for them. Akmatori's SSH connector is read-only by default: the agent can run diagnostic commands such as journalctl or systemctl status on the new hosts, and anything that changes a host needs write commands allowed on that host or an explicit allowed-commands pattern that you set.

Conclusion

"Agent-written Terraform cut our bill 10x" is three claims: a cost claim, a correctness claim and a reliability claim. Check the cost per bill line, check the Terraform through saved plans and drift checks, and check reliability with the same external signals before and after. Let the agent write and compare. Keep the cutover, the destroys and the state surgery with people.

If your team wants AI-assisted incident workflows on your own infrastructure, Akmatori helps SRE teams investigate alerts, coordinate response, and automate safe infrastructure actions. Powered by Gcore for global infrastructure reliability.

Automate incident response and prevent on-call burnout with AI-driven agents!