0
0
Terraformcloud~15 mins

Terraform apply for execution - Deep Dive

Choose your learning style9 modes available
Overview - Terraform apply for execution
What is it?
Terraform apply is a command that makes your planned changes happen in your cloud or infrastructure. It reads the plan you created and then creates, updates, or deletes resources to match that plan. This command is how you turn your infrastructure code into real, working systems. It ensures your infrastructure matches exactly what you described in your configuration files.
Why it matters
Without Terraform apply, you would have no way to safely and automatically change your infrastructure. You would have to manually create or update resources, which is slow, error-prone, and hard to track. Terraform apply solves this by automating the process, making infrastructure changes repeatable, predictable, and version-controlled. This reduces downtime and mistakes, helping teams deliver faster and safer.
Where it fits
Before using Terraform apply, you should understand how to write Terraform configuration files and run terraform plan to preview changes. After mastering apply, you can learn about advanced topics like state management, modules, and automation pipelines that use Terraform apply for continuous delivery.
Mental Model
Core Idea
Terraform apply is the action that turns your infrastructure plan into real cloud resources by creating, updating, or deleting them as needed.
Think of it like...
Terraform apply is like pressing the 'start' button on a recipe you wrote down; it takes the list of ingredients and steps and actually cooks the meal for you.
┌───────────────┐      ┌───────────────┐      ┌───────────────┐
│ Terraform     │      │ Terraform     │      │ Cloud or      │
│ Configuration │─────▶│ Plan          │─────▶│ Infrastructure │
│ Files (.tf)   │      │ (terraform    │      │ Resources     │
└───────────────┘      │ plan output)  │      └───────────────┘
                       └───────────────┘            ▲
                             │                      │
                             │ terraform apply      │
                             └──────────────────────
Build-Up - 6 Steps
1
FoundationWhat terraform apply does
🤔
Concept: Terraform apply executes the planned changes to your infrastructure.
When you run terraform apply, Terraform reads the plan it created earlier or generates a new plan if none exists. It then makes the necessary changes to your cloud resources to match your configuration. This can include creating new servers, updating settings, or deleting old resources.
Result
Your cloud infrastructure matches the desired state described in your Terraform files.
Understanding that terraform apply is the step that actually changes your infrastructure helps you see the difference between planning and doing.
2
FoundationHow terraform apply uses the state file
🤔
Concept: Terraform apply uses a state file to know what resources exist and what needs changing.
Terraform keeps a state file that records the current status of your infrastructure. When you run terraform apply, it compares this state with your configuration and plan to decide what actions to take. This state file is crucial for tracking resources and avoiding conflicts.
Result
Terraform knows exactly which resources to create, update, or delete.
Knowing that terraform apply relies on state prevents confusion about why changes happen or don't happen.
3
IntermediateUsing terraform apply with a saved plan
🤔Before reading on: Do you think terraform apply can run without a saved plan file? Commit to yes or no.
Concept: Terraform apply can use a saved plan file to apply changes exactly as planned.
You can run terraform plan -out=planfile to save a plan. Later, terraform apply planfile applies exactly those changes. This ensures no surprises happen between planning and applying, which is important for team workflows and automation.
Result
Infrastructure changes are applied exactly as reviewed in the saved plan.
Understanding the use of saved plans helps prevent accidental changes and supports safer collaboration.
4
IntermediateInteractive approval during terraform apply
🤔Before reading on: Does terraform apply always require your approval before making changes? Commit to yes or no.
Concept: By default, terraform apply asks for your confirmation before applying changes.
When you run terraform apply without flags, Terraform shows the planned changes and asks you to type 'yes' to proceed. This interactive step helps prevent accidental changes. You can skip this with the -auto-approve flag if you want to automate the process.
Result
You have control to approve or cancel changes before they happen.
Knowing about interactive approval helps you avoid unintended infrastructure changes.
5
AdvancedHandling errors and partial applies
🤔Before reading on: If terraform apply fails halfway, do you think your infrastructure is left inconsistent or fully rolled back? Commit to your answer.
Concept: Terraform apply may partially apply changes if an error occurs, leaving infrastructure in a mixed state.
If an error happens during apply, Terraform stops and reports the failure. Some resources may have been created or updated, while others not. Terraform does not automatically roll back changes. You must fix issues and run apply again to reach the desired state.
Result
Infrastructure may be partially updated, requiring manual attention or re-application.
Understanding partial applies helps you plan for error handling and recovery in real deployments.
6
ExpertTerraform apply in automation pipelines
🤔Before reading on: Do you think terraform apply in automation should always run with interactive approval? Commit to yes or no.
Concept: In automated environments, terraform apply runs non-interactively with saved plans and approval steps outside Terraform.
In CI/CD pipelines, terraform apply is run with -auto-approve and uses saved plans created earlier. Approval is handled by pipeline controls or code reviews. This ensures consistent, repeatable deployments without manual intervention. State locking and backend configuration prevent conflicts.
Result
Infrastructure changes are applied automatically, safely, and consistently in production.
Knowing how terraform apply fits into automation is key for scaling infrastructure management in teams.
Under the Hood
Terraform apply reads the current state file and compares it to the desired configuration. It calculates a set of actions (create, update, delete) needed to reach the desired state. Then it calls cloud provider APIs in the correct order to perform these actions. It updates the state file after each successful change to keep track of the real infrastructure.
Why designed this way?
Terraform was designed to separate planning from execution to give users confidence and control. The state file allows Terraform to track resources across runs and avoid recreating or losing track of infrastructure. This design balances automation with safety and transparency.
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Desired      │       │ Terraform     │       │ Cloud         │
│ Configuration│──────▶│ Apply Engine  │──────▶│ Provider APIs │
│ (.tf files)  │       │ (Compare &    │       │ (Create,      │
└──────────────┘       │ Execute)      │       │ Update, Delete)│
                       └───────────────┘       └───────────────┘
                             ▲
                             │
                      ┌───────────────┐
                      │ State File    │
                      │ (Tracks real  │
                      │ resources)    │
                      └───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does terraform apply always create all resources from scratch? Commit to yes or no.
Common Belief:Terraform apply always creates new resources every time it runs.
Tap to reveal reality
Reality:Terraform apply only creates, updates, or deletes resources as needed to match the desired state. It reuses existing resources tracked in the state file.
Why it matters:Believing this leads to unnecessary resource destruction and recreation, causing downtime and extra costs.
Quick: Does terraform apply automatically roll back changes if an error occurs? Commit to yes or no.
Common Belief:Terraform apply rolls back all changes if something fails during execution.
Tap to reveal reality
Reality:Terraform apply stops on error but does not roll back changes already made. Partial changes may remain.
Why it matters:Assuming automatic rollback can cause confusion and inconsistent infrastructure if errors happen.
Quick: Can you safely run terraform apply without reviewing the plan? Commit to yes or no.
Common Belief:You can run terraform apply without looking at the plan and trust it will do the right thing.
Tap to reveal reality
Reality:Skipping plan review risks unintended changes, including resource deletion or misconfiguration.
Why it matters:Not reviewing plans can cause costly mistakes and outages.
Quick: Does terraform apply always require manual approval? Commit to yes or no.
Common Belief:Terraform apply always needs you to type 'yes' to confirm changes.
Tap to reveal reality
Reality:You can use the -auto-approve flag to skip manual approval, especially in automation.
Why it matters:Thinking manual approval is mandatory limits automation and continuous delivery.
Expert Zone
1
Terraform apply respects resource dependencies and applies changes in the correct order to avoid failures.
2
State locking during apply prevents multiple users or processes from making conflicting changes simultaneously.
3
Terraform apply can be influenced by lifecycle rules like create_before_destroy to control resource replacement behavior.
When NOT to use
Terraform apply is not suitable for making manual, one-off changes directly in the cloud console; instead, update the Terraform code and apply. For very large infrastructures, consider using targeted applies or splitting configurations into modules to manage complexity.
Production Patterns
Teams use terraform apply in CI/CD pipelines with saved plans and automated approvals. They configure remote state backends with locking to coordinate changes. Terraform apply is often combined with policy checks and notifications to ensure compliance and visibility.
Connections
Version Control Systems
Terraform apply builds on version-controlled infrastructure code.
Understanding how terraform apply executes code stored in version control helps grasp infrastructure as code principles and safe change management.
Continuous Integration/Continuous Deployment (CI/CD)
Terraform apply is a key step in automated deployment pipelines.
Knowing terraform apply's role in CI/CD clarifies how infrastructure changes become part of automated, repeatable delivery workflows.
Transaction Processing in Databases
Terraform apply's partial apply and lack of automatic rollback resembles database transactions without full ACID guarantees.
Recognizing this similarity helps understand why manual error handling and retries are needed in infrastructure changes.
Common Pitfalls
#1Running terraform apply without reviewing the plan output.
Wrong approach:terraform apply # User blindly types 'yes' without checking changes
Correct approach:terraform plan # Review changes carefully terraform apply # Confirm only after review
Root cause:Misunderstanding that terraform apply can make destructive changes without warning.
#2Running terraform apply concurrently from multiple machines.
Wrong approach:Two users run terraform apply at the same time on the same state file.
Correct approach:Use remote state with locking (e.g., Terraform Cloud, S3 with DynamoDB lock) to prevent concurrent applies.
Root cause:Not knowing about state locking leads to conflicting changes and corrupted state.
#3Assuming terraform apply rolls back on failure.
Wrong approach:terraform apply # Error occurs but user expects full rollback automatically
Correct approach:terraform apply # On error, fix issues and re-run apply to reach desired state
Root cause:Confusing terraform apply with transactional systems that support automatic rollback.
Key Takeaways
Terraform apply is the command that makes your planned infrastructure changes real by creating, updating, or deleting resources.
It relies on a state file to track existing resources and decide what actions are needed to reach the desired state.
Reviewing the plan before applying changes is essential to avoid unintended consequences.
Terraform apply does not automatically roll back changes on errors, so careful error handling is necessary.
In automation, terraform apply runs non-interactively with saved plans and approval processes outside Terraform.