The Write, Plan, Apply workflow represents the core operational cycle in Terraform, providing a structured approach to infrastructure management that ensures predictability and safety when making changes to your environment.
**Write Phase:**
This is where you author your infrastructure as code usi…The Write, Plan, Apply workflow represents the core operational cycle in Terraform, providing a structured approach to infrastructure management that ensures predictability and safety when making changes to your environment.
**Write Phase:**
This is where you author your infrastructure as code using HashiCorp Configuration Language (HCL). During this phase, you define resources, variables, outputs, and data sources in .tf files. You specify the desired state of your infrastructure, including providers, resource configurations, and their relationships. This phase encourages collaboration through version control systems like Git, allowing teams to review and iterate on infrastructure definitions before any changes are applied.
**Plan Phase:**
The plan phase is executed using the 'terraform plan' command. Terraform compares your written configuration against the current state file and the actual infrastructure. It generates an execution plan showing what actions will be taken - resources to be created, modified, or destroyed. This preview mechanism is crucial for safety, as it allows you to verify changes before they affect real infrastructure. The plan output uses symbols like + for additions, - for deletions, and ~ for modifications. You can save plans to files for later application, ensuring the exact reviewed changes are implemented.
**Apply Phase:**
The apply phase executes the planned changes using 'terraform apply'. Terraform provisions, updates, or removes resources according to the execution plan. By default, Terraform prompts for confirmation before proceeding, adding another safety layer. After successful execution, the state file is updated to reflect the new infrastructure configuration. This phase handles resource dependencies automatically, creating resources in the correct order based on implicit and explicit dependencies defined in your configuration.
This workflow promotes infrastructure reliability through its methodical approach, enabling teams to safely manage complex environments while maintaining clear visibility into all proposed modifications before implementation.
The Write, Plan, Apply Workflow in Terraform
Why is the Write, Plan, Apply Workflow Important?
The Write, Plan, Apply workflow is the fundamental operational cycle of Terraform. Understanding this workflow is essential because it represents how infrastructure changes are safely and predictably implemented. This three-stage approach ensures that infrastructure modifications are deliberate, reviewed, and executed with full visibility into what changes will occur.
What is the Write, Plan, Apply Workflow?
This workflow consists of three distinct phases:
1. Write During the Write phase, you author your infrastructure as code using HashiCorp Configuration Language (HCL). This involves: - Creating .tf files that define resources, variables, and outputs - Defining providers to interact with cloud platforms - Structuring your configuration in a logical, maintainable way - Using version control to track changes to your configuration
2. Plan The Plan phase executes terraform plan, which: - Compares your current configuration against the existing state - Generates an execution plan showing proposed changes - Displays resources to be created, modified, or destroyed - Allows review before any actual changes occur - Can be saved to a file for later execution
3. Apply The Apply phase runs terraform apply, which: - Executes the changes outlined in the plan - Provisions, updates, or removes infrastructure resources - Updates the state file to reflect the new infrastructure state - Provides output showing the results of each operation
How Does This Workflow Work in Practice?
The workflow operates as a continuous cycle:
1. Write your configuration or make changes to existing code 2. Run terraform plan to preview changes 3. Review the plan output carefully 4. Run terraform apply to implement changes 5. Return to step 1 when new changes are needed
This cycle promotes infrastructure as code best practices by ensuring all changes are documented, reviewed, and applied systematically.
Key Commands Associated with Each Phase:
- Write: Use any text editor or IDE; run terraform fmt to format code and terraform validate to check syntax - Plan: Run terraform plan or terraform plan -out=planfile to save the plan - Apply: Run terraform apply or terraform apply planfile to use a saved plan
Exam Tips: Answering Questions on The Write, Plan, Apply Workflow
Tip 1: Remember the correct order - Write comes first, then Plan, then Apply. Questions may try to confuse you by presenting them in different orders.
Tip 2: Understand that terraform plan is a read-only operation. It does not make any changes to your infrastructure. This is a commonly tested concept.
Tip 3: Know that the Plan phase compares the desired state (your configuration) against the current state (stored in the state file).
Tip 4: Be aware that terraform apply will show a plan and ask for confirmation by default, unless you use -auto-approve flag or provide a saved plan file.
Tip 5: Remember that saved plan files ensure the exact planned changes are applied, which is crucial for CI/CD pipelines and team workflows.
Tip 6: Questions may ask about the benefits of this workflow. Key benefits include: predictability, safety through preview, collaboration through review, and audit trails.
Tip 7: Understand that terraform validate and terraform fmt are often used during the Write phase but are separate from the core workflow commands.
Tip 8: When questions mention infrastructure changes, always think about which phase of the workflow addresses the scenario being described.