A practical guide to understanding when and why to use different Terraform commands, illustrated through real-world infrastructure management scenarios.
Have you ever wondered when exactly you need to run terraform init
versus terraform plan
? Let’s explore these commands through practical, real-world scenarios that every DevOps engineer encounters.
Scenario 1: Setting Up a New Web Application Infrastructure
Imagine you’re tasked with setting up the infrastructure for a new web application. You’ll need an EC2 instance, an RDS database, and an S3 bucket for static assets.
|
|
First, you’ll need to run terraform init
. Why? Because:
- This is a new project directory
- You’re using the AWS provider for the first time
- Terraform needs to download provider plugins
Scenario 2: Adding Application Load Balancer
A month later, your application grows, and you need to add a load balancer. You create a new file:
|
|
In this case, you don’t need to run terraform init
again because you’re not adding any new providers or modules. Instead, start with terraform plan
to see what changes will be made:
|
|
Scenario 3: Introducing Remote State
As your team grows, you decide to store the Terraform state in an S3 backend. You add:
|
|
This requires running terraform init
again because:
- You’re changing the backend configuration
- Terraform needs to migrate the state file
When to Use Auto-Approve
While developing locally, you might be tempted to use:
|
|
However, in production, a safer approach is to use plan files:
|
|
Best Practices Checklist
- Always run
terraform plan
beforeapply
- Use
-out
flag to save plans for critical environments - Run
terraform init
when:- Starting a new project
- Adding/changing providers
- Modifying backend configuration
- Adding new modules
- Use workspaces to manage multiple environments
- Version your Terraform configurations in git
When to Use Targeted Apply
You might face a situation where you need to manage specific resources rather than your entire infrastructure. This is where terraform apply -target
comes in handy. However, use it cautiously as it can lead to state inconsistencies if not used properly.
|
|
Good use cases for targeted apply:
Emergency Fixes: When you need to quickly update a specific resource
1 2
# Update only the security group during a security incident terraform apply -target="aws_security_group.web_sg"
Resource Dependencies: When you need to ensure a specific resource is created first
1 2
# Create the VPC first before other networking resources terraform apply -target="aws_vpc.main"
Testing Changes: When validating changes to specific resources in a large infrastructure
1 2
# Test changes to RDS instance configuration terraform apply -target="aws_db_instance.database"
⚠️ Important: Always follow up targeted applies with a full terraform plan
and apply
to ensure your state is consistent. Using -target
should be a temporary measure, not a regular practice.
Common Gotchas
- State drift: Always run
terraform plan
before making changes to catch any manual modifications - Provider versions: Specify versions to ensure consistency across team members
- Resource dependencies: Use
depends_on
when implicit dependencies aren’t enough
Cheers! 🍺