Apply and Destroy
After writing your configuration, you need to apply it to create resources and later destroy them. This chapter covers the
apply and destroy commands in detail, along with best practices.terraform apply
The
apply command creates or updates resources to match your configuration. By default, it shows a plan and asks for confirmation.terraform applyTo auto‑approve (use with caution):terraform apply -auto-approveApply a saved plan:terraform plan -out=tfplan
terraform apply tfplanterraform destroy
The
destroy command removes all resources defined in your configuration.terraform destroyTo auto‑approve:terraform destroy -auto-approveDestroy specific resources (using target):terraform destroy -target=aws_instance.webApplying with Variables
Pass variables via files or CLI:
terraform apply -var-file="prod.tfvars"
terraform apply -var="instance_type=t3.micro"State Refresh
Before planning, Terraform refreshes state. You can manually refresh:
terraform refreshBest Practices
- Always review the plan before applying.
- Use remote state and locking for teams.
- Destroy resources in a controlled order (Terraform does this automatically).
- Run
terraform planin CI/CD to catch changes before merge. - Use workspaces or variable files for different environments.
Two Minute Drill
terraform applycreates/updates resources.terraform destroyremoves all resources.- Use
-auto-approveto skip confirmation (careful!). - Pass variables with
-var-fileor-var. - Always review the plan before applying in production.
Need more clarification?
Drop us an email at career@quipoinfotech.com
