Infrastructure as Code

Terraform Best Practices for Managing AWS Infrastructure

Terraform makes AWS infrastructure reviewable and repeatable, but only if the project is organized with care. Most problems teams run into later, such as slow plans, risky applies and fragile state, trace back to early structural choices.

These are the practices I apply on AWS projects, in the order they tend to matter.

1. Separate environments and limit blast radius

Keep development, staging and production in separate states, and ideally in separate AWS accounts. A mistake in one environment should not be able to touch another.

infra/
  modules/
    network/
    ecs-service/
    rds/
  live/
    dev/
      network/
      app/
    staging/
      network/
      app/
    prod/
      network/
      app/

Split state by lifecycle and ownership as well. The network changes rarely, while application stacks change often. Putting them in one state makes every plan slower and every apply riskier.

2. Use remote state with locking and encryption

Local state files do not work for teams. Store state in an S3 bucket with versioning and encryption turned on, restrict access with IAM, and enable locking so that two applies cannot run at once.

terraform {
  required_version = ">= 1.10"

  backend "s3" {
    bucket       = "example-terraform-state"
    key          = "prod/network/terraform.tfstate"
    region       = "eu-west-1"
    encrypt      = true
    use_lockfile = true
  }
}

Native S3 locking with use_lockfile is available from Terraform 1.10. On older versions, use a DynamoDB table for locking instead. State can contain sensitive values, so treat the bucket as sensitive data.

3. Pin versions

Unpinned providers cause surprise changes. Pin Terraform and provider versions, and commit the dependency lock file so everyone and every pipeline uses the same builds.

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

Commit .terraform.lock.hcl, and update providers deliberately in their own pull request.

4. Write small, focused modules

A module should do one thing and expose a few clear inputs and outputs. Avoid wrapping every resource in a module on day one, because premature abstraction makes simple changes harder.

  • Create a module when the same pattern repeats, such as an ECS service or an RDS instance.
  • Validate inputs with validation blocks and give variables descriptions.
  • Version shared modules with Git tags, and reference a tag instead of a branch.
module "api" {
  source = "git::https://example.com/infra-modules.git//ecs-service?ref=v1.4.0"

  name           = "api"
  cluster_arn    = module.cluster.arn
  desired_count  = 2
  container_port = 8080
}

5. Tag everything consistently

Tags make cost allocation, ownership and automation possible. Set common tags once at the provider level.

provider "aws" {
  region = "eu-west-1"

  default_tags {
    tags = {
      Environment = var.environment
      Project     = var.project
      ManagedBy   = "terraform"
    }
  }
}

6. Run plans in CI and apply in a controlled way

Every change should go through a pull request that shows the plan. Use short-lived credentials from your CI system instead of stored access keys. With GitHub Actions, OpenID Connect lets the workflow assume an IAM role without long-lived secrets.

Terraform change workflow: edit code, open a pull request, plan in CI, peer review, apply from the main branch Terraform change workflow: edit code, open a pull request, plan in CI, peer review, apply from the main branch

A sensible pipeline:

  1. terraform fmt -check and terraform validate on every pull request.
  2. A speculative terraform plan, posted for review.
  3. Static checks for security and policy issues with a tool of your choice.
  4. terraform apply only from the main branch, using the reviewed plan, with an approval step for production.

7. Change existing infrastructure safely

  • Use moved blocks to rename or restructure resources without destroying them.
  • Use import blocks to bring existing resources under management, and check that the plan shows no unintended changes.
  • Protect critical resources such as databases with lifecycle { prevent_destroy = true }.
  • Read every plan for destroy and replace actions before approving.

8. Keep secrets out of code and plans

Do not commit secrets or put them in variable files. Reference secrets from AWS Secrets Manager or SSM Parameter Store, and mark sensitive outputs with sensitive = true. Remember that values can still end up in state, which is another reason to restrict state access.

9. Document and hand over

A good Terraform repository has a short README per stack, a note on how to run plans, and a record of why non-obvious choices were made. The aim is that any engineer on the team can make a change safely.

Common mistakes to avoid

  • One giant state for everything.
  • Applying from laptops with personal credentials.
  • Leaving drift unaddressed, so the console and the code disagree.
  • Using -target as a normal workflow instead of an exception.
  • Deeply nested modules that nobody can follow.

Need help with Terraform on AWS?

I help teams structure Terraform repositories, import existing infrastructure and build safe CI workflows. See Infrastructure as Code and Terraform consulting, or discuss your project.

Related articles

Related services

Discuss your infrastructure with a senior engineer

Share your goals and current setup. I will respond with sensible next steps.

Talk to a DevOps Engineer