Understanding terraform init, plan, and apply: Behind the Scenes
Welcome to the advanced Terraform guide! Whether you’re already familiar with the basics or looking to level up, we’re going to explore three essential Terraform commands: terraform init, terraform plan, and terraform apply. We’ll break down what happens behind the scenes, provide real-world examples, and explain the output step by step
Let’s get started! 🔥
1. What Happens When You Run terraform init
?
The terraform init
command is the first step in any Terraform workflow. It sets up your project and ensures Terraform is ready to execute your configurations.
Behind the Scenes
When you run terraform init
, several things happen:
- Provider Plugins: Terraform downloads the necessary provider plugins (like AWS, Azure, Google Cloud) for your project. These plugins allow Terraform to interact with cloud platforms and provision resources.
- Backend Initialization: If you’re using a remote backend (e.g., storing your state files in S3), Terraform configures it during this step.
- Lock File Creation: A
.terraform.lock.hcl
file is generated, locking the versions of your provider plugins, ensuring consistency across environments.
Example
Let’s say you have the following configuration in your main.tf
:
provider "aws" {
region = "us-east-1"
}
Running terraform init
will look something like this:
$ terraform init
Initializing the backend...
Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.0.0...
- Installed hashicorp/aws v4.0.0 (signed by HashiCorp)
Terraform has been successfully initialized!
The
init
command is like preparing your toolbox before you start a project. It makes sure you have everything you need to proceed: the right plugins, correct versions, and any backend configuration.
2. What Happens When You Run terraform plan
?
The terraform plan
command is where Terraform shows you what changes it would make to your infrastructure before actually making any changes. It compares the current state of your infrastructure (what exists) with the desired state (your configuration files).
Behind the Scenes
Here’s what happens when you run terraform plan
:
- State Comparison: Terraform compares the current state of your resources (e.g., EC2 instances, databases) with the desired state defined in your configuration files.
- Execution Plan Creation: It creates a plan that shows what will be added, modified, or destroyed.
- No Changes Yet: This is just a preview. Nothing is modified during
terraform plan
.
Example
Let’s extend our previous example and say we want to add an EC2 instance:
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "my_instance" {
ami = "ami-12345678"
instance_type = "t2.micro"
}
Running terraform plan
will produce something like this:
$ terraform plan
Terraform will perform the following actions:
# aws_instance.my_instance will be created
+ resource "aws_instance" "my_instance" {
+ ami = "ami-12345678"
+ instance_type = "t2.micro"
}
Plan: 1 to add, 0 to change, 0 to destroy.
The
plan
command is like looking at a blueprint before you start building. It tells you exactly what Terraform will do, allowing you to review changes and avoid surprises.
3. What Happens When You Run terraform apply
?
Now comes the exciting part — making changes! The terraform apply
command executes the plan and applies the desired state to your infrastructure.
Behind the Scenes
When you run terraform apply
, here’s what happens:
- Execution of the Plan: Terraform takes the plan generated by
terraform plan
and applies the changes to your infrastructure. - Resource Creation/Modification/Deletion: Depending on the plan, Terraform will add, update, or destroy resources.
- State Update: Once the changes are applied, Terraform updates the
terraform.tfstate
file, which keeps track of your infrastructure's current state.
Example
Continuing with our EC2 example, here’s what happens when you run terraform apply
:
$ terraform apply
Terraform will perform the following actions:
# aws_instance.my_instance will be created
+ resource "aws_instance" "my_instance" {
+ ami = "ami-12345678"
+ instance_type = "t2.micro"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions? (yes/no)
If you type yes
, Terraform will create the EC2 instance:
aws_instance.my_instance: Creating...
aws_instance.my_instance: Creation complete after 1m12s [id=i-0abcdef1234567890]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
The
apply
command is like starting the actual construction based on your blueprint. It makes the changes for real and updates your infrastructure to match your configuration.
To recap, here’s what each command does:
1. terraform init
Setup: Initializes your project by setting up the working directory.
Download Plugins: Installs the necessary provider plugins (e.g., AWS, Azure).
Initialize Backends: Configures any remote state storage (e.g., S3).
Lock Files: Creates a
.terraform.lock.hcl
file to lock provider versions for consistency.
2. terraform plan
State Refresh: Updates the current state by pulling the latest data from your cloud provider.
Compare States: Compares the current infrastructure with your desired configuration.
Execution Plan: Shows what will be added, changed, or destroyed.
Dry Run: No actual changes are made — this is just a preview.
3. terraform apply
State Refresh (Optional): Optionally refreshes the state before making changes.
Execute the Plan: Applies the planned changes to the infrastructure.
Create/Modify/Destroy: Updates resources according to the plan.
Update State: Saves the new state to the
terraform.tfstate
file for future use.
In short: init
prepares your environment, plan
shows what will change, and apply
makes those changes!
And that’s it — happy Terraforming! 😄