Home
›
Articles
›

How to use Terraform without breaking your infrastructure

How to use Terraform without breaking your infrastructure

By Ibi Hasanli

·

·

4 min read

Terraform is HashiCorp's infrastructure as code tool: you describe the cloud and on-premises resources you want in configuration files, then Terraform creates, changes or removes them to match. The habits around three commands, init, plan and apply, are what decide whether a change to your infrastructure is safe. Skip the review, or treat the state file casually, and it is easy to change the wrong thing.

Init, plan and apply

You write the desired infrastructure in .tf files. terraform init is the first command after you write a new configuration or clone one from version control. It initialises the working directory, installs the required provider plugins, and sets up the state backend. It is safe to run more than once, and it will not delete your configuration or state.

This example pins the AWS provider, uses eu-west-2 (London), and declares one bucket.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

resource "aws_s3_bucket" "logs" {
  bucket = "example-company-logs"
}

terraform plan compares current state with your configuration and proposes the create, update and destroy actions that would make the real infrastructure match. It does not carry out those changes. terraform apply executes a plan. Without a saved plan file, Terraform generates a new plan, prompts you to approve it, then performs the work. If you pass a saved plan file, it applies that plan and does not prompt again: passing the file is the approval.

terraform init
terraform plan -out=tfplan
terraform apply tfplan

Review the plan before you apply it

A plan run without -out is a speculative plan: it shows what would happen, but it is not the plan you will apply. A later terraform apply generates a fresh plan, and other changes in the meantime can make the result different. Re-check the final, saved plan before you apply it, and read it for destroy and replace actions, not only creates. A saved plan file is applied without a second confirmation, so skip that review and you can apply a plan you never saw, including destroys that were not in the earlier output.

State, remote state and locking

By default, Terraform stores state in a local file named terraform.tfstate, which maps your configuration to the real resources. If you lose it, Terraform no longer knows what it manages. On a shared account, put state in a remote backend with locking and access control, and keep it out of version control. A local or committed state file can expose secrets and cannot stop two people applying at once. Treat saved plan files the same way.

If the backend supports it, Terraform locks state automatically for every operation that could write it. If it cannot acquire the lock, it stops. Not every backend supports locking. On the S3 backend, locking is opt-in: set use_lockfile to true, because it defaults to false. DynamoDB-based locking is deprecated and will be removed in a future minor version. You can configure both while migrating off it. Turn on versioning on the state bucket as well, so you can recover the state if a bad write overwrites it.

If you need to document an existing estate in Terraform, or to set up remote state and locking on a shared account, that is a good point to bring in help.

Pin provider versions

In the terraform block, required_providers should name the source and a version constraint. In a root module, use a ~> constraint, which lets only the rightmost version component increment and so sets an upper bound. version = "~> 6.0" accepts later 6.x releases of the AWS provider and refuses 7.0. Leave it unpinned and a later init can pull a breaking major release into a working estate. If Terraform cannot satisfy the constraint, it will not run plan, apply or state operations. After init, commit the dependency lock file so later inits select the same provider versions.

Keep secrets out of files and state

Do not put passwords, API tokens or access keys in .tf files, and do not commit them to git. Marking a variable sensitive = true only redacts it in the CLI output. Terraform still stores those values in state and plan files, and anyone who can read the files can read the secrets. Keep state and saved plans out of git, and use a remote backend with access control and encryption at rest.

Start with something small

Do not begin by importing a whole production account. Write a small configuration, like the bucket above, run the workflow, and read the plan. Once you know how to use Terraform on something small you can afford to get wrong, widen the scope. If you are moving an existing app onto AWS, describe the new side in code and prove it before you cut over.

The AWS accounts should be yours, not a supplier's login. That is the same ownership point as leaving a freelancer with your AWS root login: you keep the account, and anyone helping you works inside it. Eight Mile builds and runs infrastructure on AWS as code, and can document an existing estate in Terraform, with UK and EU regions as the default.

If you want infrastructure described in code, on accounts you own, talk to an engineer. The conversation is a free scoping chat; you get a written scope and price, and billing starts only once that scope is agreed.

AWS
Cloud

Can you leave your managed AWS provider?

How to leave a managed AWS hosting provider without lock-in: who owns the account, the code, DNS and backups, and what a written exit should cover.

·

4 min read

AWS
Infrastructure

Leaving a freelancer with your AWS root login

Leaving a freelancer with your AWS root login is a risk even after a good build. How to take back control with named IAM, MFA and a handoff.

·

4 min read

AWS
Infrastructure

The EC2 server nobody has patched since launch

The EC2 server nobody has patched since launch is a risk once the OS is out of support. What to check, and what managed patching should include.

·

4 min read

Infrastructure
Cloud

Cloud bill went up and nobody can say why

Cloud bill went up and nobody can say why: usually orphaned resources, missing tags, or one shared account. Fix ownership first.

·

3 min read

AWS
Infrastructure

AWS vs managed hosting for a small SaaS

AWS vs managed hosting for a small SaaS: when simple platforms fit, and when owned cloud, IAM and infra in git win.

·

3 min read

AI
AWS

Why the AWS bill now includes AI

Why the AWS bill now includes AI, how agent workflows make that spend move, and what to put in place before a pilot becomes a surprise.

·

3 min read

CRM
SaaS

CRM for a small firm off the shelf or built

CRM for a small firm off the shelf or built: when HubSpot-class tools fit, and when a system on your own job data serves trades and clinics better.

·

4 min read

Websites
Hosting

Uptime monitoring for a small business website

Uptime monitoring for a small business website only works with a named engineer and response path. What a care plan or managed hosting should cover.

·

3 min read

TradeOps
SME

When WhatsApp is running your jobs

When WhatsApp is running your jobs, groups and voice notes become the job board. What a proper ops system replaces, without taking phones away.

·

4 min read