How to Use Terraform Apply: Simple Guide for Beginners
Use
terraform apply to create or update your cloud infrastructure based on your Terraform configuration files. It shows a plan of changes and asks for your confirmation before making any updates.Syntax
The basic syntax of terraform apply is simple. You run it in your terminal inside your Terraform project folder. It reads your configuration files and shows what changes it will make.
terraform apply: Runs the apply process interactively.-auto-approve: Skips the confirmation prompt and applies changes immediately.planfile: You can provide a saved plan file to apply exactly what was planned.
bash
terraform apply [options] [planfile]
Example
This example shows how to use terraform apply to create a simple AWS S3 bucket. It first initializes Terraform, then applies the configuration, showing the planned changes and asking for confirmation.
bash
terraform init terraform apply
Output
Initializing the backend...
Initializing provider plugins...
Terraform has been successfully initialized!
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# aws_s3_bucket.example will be created
+ resource "aws_s3_bucket" "example" {
+ bucket = "my-unique-bucket-name-12345"
+ acl = "private"
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
aws_s3_bucket.example: Creating...
aws_s3_bucket.example: Creation complete after 2s [id=my-unique-bucket-name-12345]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when using terraform apply include:
- Running
terraform applywithoutterraform initfirst, which causes errors because providers are not downloaded. - Not reviewing the plan carefully before confirming, which can lead to unintended changes.
- Using
-auto-approvecarelessly, skipping the safety check. - Applying changes without saving a plan file, making it hard to reproduce exact changes.
bash
Wrong way: terraform apply -auto-approve Right way: terraform init terraform plan -out=tfplan terraform apply tfplan
Quick Reference
| Command | Description |
|---|---|
| terraform apply | Interactively applies changes after showing the plan |
| terraform apply -auto-approve | Applies changes immediately without confirmation |
| terraform plan -out=planfile | Saves the plan to a file for later apply |
| terraform apply planfile | Applies changes exactly as saved in the plan file |
Key Takeaways
Always run terraform init before terraform apply to prepare your environment.
Review the plan output carefully before confirming to avoid unwanted changes.
Use terraform apply -auto-approve only when you are sure about the changes.
Saving a plan file with terraform plan -out helps apply consistent changes.
terraform apply updates your infrastructure to match your configuration files.