+91 88606 33966            edu_sales@siriam.in                   Job Opening : On-site Functional Trainer/Instructor | Supply Chain Management (SCM)
Terraform Essentials: When to Use terraform refresh vs terraform plan

When working with Terraform, two commonly used commands are terraform refresh and terraform plan. While both involve examining the state of your infrastructure, they serve distinct purposes.

What is terraform refresh?

The terraform refresh command updates the Terraform state file with the current state of your real-world infrastructure.

Key Points:

  • Purpose: Synchronizes the state file with actual resources in the cloud.
  • Result: Updates the local state file without making any changes to your infrastructure.
  • Use Case: Use when you suspect the state file might be outdated due to manual changes or external modifications to resources.

Example:

Suppose you manually change the tags of a Compute Instance in Oracle Cloud Infrastructure (OCI) using the OCI Console. Your Terraform state file will no longer match the actual configuration

terraform refresh

This command will update the state file to reflect the new tags, ensuring Terraform has accurate information for future operations.

What is terraform plan?

The terraform plan command generates an execution plan, showing what changes Terraform will make to your infrastructure to align it with your desired configuration.

Key Points:

  • Purpose: Identifies what will change but does not modify resources.
  • Result: Provides a preview of additions, deletions, and updates.
  • Use Case: Use before applying changes to understand their impact.

Example:

After updating the instance size in your Terraform configuration:

resource “oci_core_instance” “example” {
shape = “VM.Standard2.2” # Previously VM.Standard2.1
}

Run:

terraform plan

This will output a summary like:

~ shape = “VM.Standard2.1” -> “VM.Standard2.2”

so, the major differences are:

Purpose:

  • terraform refresh : Updates the state file with the real infrastructure’s current state.
  • terraform plan : Shows what changes Terraform will make to match the configuration.

Impact on Resources:

  • terraform refresh :No change to actual resources.
  • terraform plan : No change to actual resources, it just shows what will change.

State File:

  • terraform refresh :Updates the local state file to reflect real changes.
  • terraform plan : Does not update the state file.

When to Use:

  • terraform refresh :When there are manual changes outside of Terraform.
  • terraform plan : Before applying changes to check what will happen.

Main Use:

  • terraform refresh :Ensures the state file is up to date.
  • terraform plan : Previews the changes Terraform will make.

Best Practices

  1. Check the Current State First: Always use terraform refresh before making changes if manual adjustments were made outside Terraform.
  2. Review Plans: Never skip reviewing the output of terraform plan to avoid unintended changes.
  3. Integrate into CI/CD: Automate terraform plan in pipelines to verify changes before applying them.

Now that you understand the key differences between terraform refresh and terraform plan, which command will you use in your next Terraform project to ensure everything is running smoothly?

Terraform Essentials: When to Use terraform refresh vs terraform plan

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top