Terraform best practices for infrastructure as code center on three pillars: managing state safely, designing reusable modules, and detecting configuration drift. This guide explains each pillar with concrete examples and practical workflows you can apply immediately.
Why State Management Is the Foundation
Terraform stores the real-world infrastructure it manages in a state file. This file maps your configuration to actual resources, tracks metadata, and improves performance. Without proper state management, you risk corruption, lost resources, or conflicting changes.
The golden rule: never edit the state file manually. Always use terraform state commands or let Terraform handle it. Store state remotely (e.g., in an S3 bucket with DynamoDB locking) to enable team collaboration and prevent concurrent writes. Local state files are fine for personal projects but fail in team environments.
How Terraform State Works
When you run terraform apply, Terraform compares the desired configuration with the state file. It then creates, updates, or deletes resources to match. The state file also stores sensitive data like passwords and access keys, so protect it with encryption and strict access controls.
Key components of state:
- Resource metadata: IDs, ARNs, and attributes.
- Dependencies: Implicit and explicit references between resources.
- Outputs: Values you export for use elsewhere.
- Version: The Terraform version that wrote the state.
For example, if you create an AWS EC2 instance, the state file stores its instance ID, public IP, and security group associations. This allows Terraform to update that specific instance on subsequent runs.
Designing Reusable Modules
Modules are self-contained packages of Terraform configuration that encapsulate a set of resources. They promote reusability, consistency, and maintainability. A good module follows these principles:
- Single responsibility: Each module does one thing well (e.g., a VPC module, an EC2 module).
- Clear inputs and outputs: Define variables with descriptions and defaults, and export useful outputs.
- Versioning: Use tags or releases to version modules, allowing teams to upgrade predictably.
- No hard-coded values: Everything configurable should be a variable.
For instance, a module for an AWS security group might take name, vpc_id, and ingress_rules as inputs, and output the security group ID. You can then reuse it across environments with different rule sets.
Detecting and Managing Drift
Drift occurs when the actual infrastructure diverges from the configuration. This can happen when someone manually changes a resource in the console, or when a resource is deleted outside of Terraform. Drift leads to confusion and unexpected failures.
To detect drift:
- Run
terraform planregularly (e.g., in CI/CD). It shows changes needed to match the configuration. - Use tools like
terraform validateandterraform fmtto catch syntax and style issues. - Consider third-party tools like Terratest or driftctl for automated drift detection (but verify their current capabilities).
When drift is found, decide whether to adopt the change (update the configuration) or revert it (use terraform apply to force the resource back). For example, if someone resizes an EC2 instance manually, terraform plan will show a change to the instance type. You can update the configuration to match the new size, or run apply to revert it.
Common Pitfalls and How to Avoid Them
Even experienced users hit these issues:
- State file corruption: Avoid concurrent applies without locking. Use remote state with locking.
- Module version pinning: Always pin module versions to avoid unexpected changes.
- Ignoring drift: Regularly run
terraform planto catch drift early. - Hard-coded credentials: Use environment variables or a secrets manager instead of putting keys in code.
- Not using workspaces or environments: Separate dev, staging, and prod to prevent accidental changes.
For example, forgetting to lock state can cause two team members to apply conflicting changes, leading to corruption. Using an S3 backend with DynamoDB locking solves this.
State Backends and Remote Storage
Remote state backends store the state file in a shared location. Popular options include AWS S3, Azure Storage, Google Cloud Storage, and HashiCorp Consul. Each has its own locking mechanism (e.g., DynamoDB for S3, Blob storage leases for Azure).
Benefits of remote state:
- Team collaboration
- Automatic versioning
- Encryption at rest
- Reduced risk of local file loss
For example, configuring an S3 backend looks like:
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/network/terraform.tfstate"
region = "us-east-1"
dynamodb_table = "terraform-locks"
}
}
Module Composition and Versioning
Modules can be composed to build complex infrastructure. For instance, a network module might use submodules for VPC, subnets, and gateways. Versioning modules with Git tags or semantic versioning ensures stability.
When using modules from the Terraform Registry, specify a version constraint like version = "~> 2.0" to allow minor updates while preventing breaking changes. For internal modules, use Git references like ref = "v1.2.0".
Example of using a module:
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
}
Frequently Asked Questions
What is the best way to store Terraform state?
Use a remote backend with locking, such as S3 with DynamoDB. This provides durability, concurrency control, and encryption. Avoid local state for any team project.
How do I handle drift when someone manually changes infrastructure?
Run terraform plan to see the diff. If the change is intentional, update your configuration to match. If not, run terraform apply to revert it. For automated detection, integrate plan into CI/CD.
Should I use modules for everything?
Not necessarily. Modules add abstraction and complexity. Use them for reusable patterns (e.g., VPC, EC2) but keep simple one-off resources in the root module.
How can I prevent state file corruption?
Always use locking, never edit state manually, and back it up. Also, avoid running multiple applies simultaneously without locking.
What is the difference between terraform plan and terraform apply?
plan shows what changes will be made without applying them. apply executes those changes. Use plan to review and catch drift.
Can I use Terraform with multiple cloud providers?
Yes, Terraform supports many providers. Use separate state files or workspaces for each provider to avoid mixing resources.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Reliable Subaru vs New Honda Civic Cost
- Pokemon Bookmark Corner Designs & Pokemon GO DIY
- Forgotten Games That Created Your Favorite Genres
- Lexus vs Genesis Luxurious Compact SUV0.075 hp/lb
- Uber VW Robotaxi LA Launch Date
- Notebook vs Tablet: Which Saves You More Money?