Terraform Tutorial — terraform apply
terraform apply executes the plan and creates, updates, or destroys real infrastructure resources.
Running apply
$ terraform apply
Terraform will perform the following actions:
# aws_s3_bucket.main will be created
+ resource "aws_s3_bucket" "main" {
+ bucket = "my-app-bucket"
}
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.main: Creating...
aws_s3_bucket.main: Creation complete after 3s [id=my-app-bucket]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
bucket_name = "my-app-bucket"
Apply with Auto-Approve
Skip the interactive confirmation:
$ terraform apply -auto-approve
⚠️ Warning: Only use
-auto-approvein automated pipelines where a plan has already been reviewed. Never use it manually in production.
Apply a Saved Plan
# Earlier: saved the plan
$ terraform plan -out=tfplan
# Now: apply exactly that plan
$ terraform apply tfplan
When applying a saved plan, Terraform does not prompt for confirmation because the plan was already reviewed.
Apply with Variables
# From file
$ terraform apply -var-file="prod.tfvars"
# From command line
$ terraform apply -var="environment=prod" -var="instance_count=5"
Apply Targeting
Apply only a specific resource:
$ terraform apply -target=aws_instance.web
Useful when:
- Debugging a single resource
- Applying a critical fix without touching everything else
What Happens During Apply
terraform apply
├─ 1. Acquire state lock
├─ 2. Refresh state (query cloud APIs)
├─ 3. Generate plan
├─ 4. Prompt for approval
├─ 5. Execute changes (API calls)
├─ 6. Update state file
└─ 7. Release state lock
Handling Apply Failures
If apply fails midway (e.g., network error), Terraform:
- Saves the state with resources created so far
- Does not roll back successful resources
- Running
terraform applyagain continues where it left off
aws_vpc.main: Creating... ✅
aws_subnet.public: Creating... ✅
aws_instance.web: Creating... ❌ Error
# Run apply again — VPC and subnet already exist, only instance is created
$ terraform apply
What’s Next?
You can create infrastructure. Let’s learn how to tear it all down with terraform destroy.