<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How Refresh works in terraform]]></title><description><![CDATA[How Refresh works in terraform]]></description><link>https://terraform-refresh-state.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sat, 05 Sep 2026 09:12:02 GMT</lastBuildDate><atom:link href="https://terraform-refresh-state.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Terraform State: Behind the Scenes of Plan and Apply]]></title><description><![CDATA[Introduction:
When working with Terraform, understanding what happens during the execution of terraform plan and terraform apply is crucial. In this blog, we will walk through a common scenario where the region in the configuration file is changed, a...]]></description><link>https://terraform-refresh-state.hashnode.dev/how-refresh-works-when-we-run-terraform-plan-and-terraform-apply</link><guid isPermaLink="true">https://terraform-refresh-state.hashnode.dev/how-refresh-works-when-we-run-terraform-plan-and-terraform-apply</guid><category><![CDATA[#terrarm state file]]></category><category><![CDATA[terraform plan]]></category><category><![CDATA[detailed steps terraform]]></category><category><![CDATA[State file]]></category><category><![CDATA[Terraform apply]]></category><dc:creator><![CDATA[Surekha Kokatam]]></dc:creator><pubDate>Tue, 18 Mar 2025 21:25:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1742448371993/8377a22a-656e-4ac6-bcd8-2850a0f4d3a0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-introduction">Introduction:</h2>
<p>When working with Terraform, understanding what happens during the execution of <strong>terraform plan</strong> and <strong>terraform apply</strong> is crucial. In this blog, we will walk through a common scenario where the region in the configuration file is changed, and explain how Terraform handles the state file and interacts with the cloud provider (AWS in this case). Also how ‘refresh’ plays a big role in this process.</p>
<h2 id="heading-explanation-with-example">Explanation with example:</h2>
<p>Let’s dive deep into it it, first we create an EC2 instance in ‘us-west-2’ region with the help of Terraform configuration file.</p>
<pre><code class="lang-plaintext">provider "aws" {
  region     = "us-west-2"
  access_key = "AKIAQ******R3O4SH"
  secret_key = "aRsZ2dhL+U*********s6bHZ4V0Y0FnN"
}


resource "aws_instance" "my-blog-instance" {
  ami = "ami-0b6d6dacf350ebc82"
  instance_type = "t2.micro"
  tags = {
    Name = "blog-instance"
  }
}
</code></pre>
<p>After we run terraform commands(terraform init, terraform plan, terraform apply), EC2 instance will be created in our AWS infrastructure.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742320053147/1979648a-eeb5-44f4-9edc-327cd26db6bd.png" alt class="image--center mx-auto" /></p>
<p>State file called “ <strong>{} terraform.tfstate</strong> “ will also be created automatically by Terraform in the same folder where our Terraform file is, based on our created EC2 instance. This state file contains AMI Id, Instance Type, Name, Region etc. Therefore, it is important to store State file in a secured place, as it contains sensitive information.</p>
<p><strong>State File:</strong></p>
<p>The state file is an exact representation of the current infrastructure as managed by Terraform. It stores details about all the resources, their configurations, and their current state, helping Terraform understand what's already deployed and what changes are needed.</p>
<p>Here is our State file:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742319811239/36068c45-0009-4f73-abaa-6bce05f8e502.png" alt class="image--center mx-auto" /></p>
<p>Now, interesting part comes into picture. Follow the steps:</p>
<h3 id="heading-1-configuration-file-region-change">1. Configuration file region change</h3>
<p>Now, we want to change Region from <strong>us-west-2</strong> to <strong>us-east-1</strong> in our Terraform configuration file. Also, we need to change the AMI Id as per the region. Here is the updated code:</p>
<pre><code class="lang-plaintext">provider "aws" {
  region     = "us-east-1"
  access_key = "AKIAQ******WR3O4SH"
  secret_key = "aRsZ2dhL************pXs6bHZ4V0Y0FnN"
}


resource "aws_instance" "my-blog-instance" {
  ami = "ami-08b5b3a93ed654d19"
  instance_type = "t2.micro"
  subnet_id = "subnet-08166d25f26b02879"
  tags = {
    Name = "blog-instance"
  }
}
</code></pre>
<p>Now interesting part comes into picture.</p>
<h3 id="heading-2-terraform-init">2. Terraform init</h3>
<p>We should run <strong>terraform init</strong> command again, when we change the region in our Terraform configuration file. It ensures that the Terraform is properly configured to interact with the AWS provider in the new region. It reinitializes your working directory, updates any required provider plugins, and sets the proper connection settings for the new region.</p>
<p>Without running <strong>terraform init,</strong> Terraform may continue using the previous region or fail to interact with AWS properly.</p>
<h3 id="heading-3-terraform-plan-what-happens-exactly">3. Terraform plan - what happens exactly</h3>
<p>Next, when we run <strong>terraform plan</strong>, Terraform reads the updated configuration file(.tf file) to check the desired state, including the region change to us-east-1.</p>
<h3 id="heading-i-terraform-compares-configuration-file-and-state-file">i) Terraform compares configuration file and state file</h3>
<p>Terraform re-reads our Terraform configuration file and compares the “ desired state in .tf file ” with the ‘ current state ‘ stored in the Terraform state file”.</p>
<p>In our case, Terraform notices that the configuration has changed region from us-west-2 to us-east-1 and our state file still contains old region information (details of us-west-2 EC2instance).</p>
<h3 id="heading-iiterraform-refreshes-the-state">ii)Terraform refreshes the state</h3>
<p>Before planning any changes, Terraform first refreshes the state. It checks the current state of the infrastructure to ensure that it has the latest information about resources. This helps Terraform to be aware of any changes made outside of the Terraform configuration, like manual updates or changes in the AWS environment.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742325780459/9dd510c1-3caa-48ee-99db-5b2bd6ad374d.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-a-how-terraform-connects-to-aws">a) How Terraform connects to AWS</h3>
<p>Since there is no information in the state file about an instance in <strong>us-east-1</strong>, Terraform directly makes API calls to AWS to check for any existing resources in that region.</p>
<p>If we change something minor, like the <strong>instance name</strong> (while keeping the same region), Terraform first checks the state file to compare the existing resource details. It then makes API calls only if necessary to verify or update the infrastructure.</p>
<h3 id="heading-b-temporary-storage-in-memory-ram">b) Temporary storage in Memory (RAM)</h3>
<p>Terraform temporarily stores the updated infrastructure details in memory, not in a state file. This temporary storage is what Terraform uses to compare the <strong>current state</strong> (the current infrastructure) with the <strong>desired state</strong> (the configuration in your Terraform files).</p>
<h3 id="heading-iii-plan-calculation">iii) Plan Calculation</h3>
<p>Terraform then compares the actual state (from the refresh) with the desired state (from your configuration files), and it calculates the changes required to match our infrastructure with the configuration in your Terraform files. This calculation is done in memory, and the output shows us the <strong>differences</strong>.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742326286544/9e26bbf0-4630-4573-ac8c-2354924ca508.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742330283690/fa2ccde6-4ad2-4cf2-8b24-f740c77e40e0.png" alt class="image--center mx-auto" /></p>
<p>Here, as there is no instance exists in us-east-1, it is showing to add 1 resource in the output.</p>
<p>The output generally includes:</p>
<ul>
<li><p><strong>Resources to be added</strong></p>
</li>
<li><p><strong>Resources to be modified</strong></p>
</li>
<li><p><strong>Resources to be deleted</strong></p>
</li>
</ul>
<h3 id="heading-4-review-the-proposed-changes-and-confirm-to-apply">4. Review the proposed changes and Confirm to Apply</h3>
<p>Now, we have to review the <strong>output</strong> carefully provided by terraform, when we execute <strong>terraform plan</strong>.</p>
<p>We have to run the <strong>terraform apply</strong> command after confirming the changes we want to apply to our infrastructure.</p>
<p>This will provide us prompt to confirm this. Type “yes” if we want to move further. We can also use <strong>terraform apply -auto-approve</strong> command instead of terraform apply to approve automatically, without any prompt.</p>
<h3 id="heading-i-apply-the-execution-plan">i) Apply the execution plan</h3>
<p>Once confirmed, Terraform proceeds with applying the execution plan. It uses the Terraform configuration file to communicate with the cloud provider (AWS, in our case) and makes the necessary API calls to create EC2 instance in “ us-east-1 “ as specified in the plan.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742332786745/fc8f0e7c-b341-40b3-91fe-6529d2b51790.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-ii-terraform-updates-the-state-file">ii) Terraform updates the state file</h3>
<p>After applying changes in our AWS infrastructure (i.e; after creation of EC2 instance), Terraform updates the state file to reflect the current state of our infrastructure. Don’t delete the state file, as it would be big mess to find out the resources in our infrastructure, especially if we have many resources.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1742332895625/e28a21d4-d4ee-47f1-a3c6-108c9822beca.png" alt class="image--center mx-auto" /></p>
<p>The priority always will be given to desired state(in configuration file), <strong>terraform plan</strong> and <strong>terraform apply</strong> always helps us to change our infrastructure according to the desired state.</p>
<p>These are the detailed steps behind what happens exactly when we run <strong>terraform plan</strong> and <strong>terraform apply</strong> commands. And the process is repetitive when we make changes to our infrastructure.</p>
<h2 id="heading-conclusion">conclusion:</h2>
<p>Understanding how the Terraform state file updates is crucial. Without a proper understanding, there is a risk of losing important details in the state file, which can lead to infrastructure issues. Proper management of the state file ensures consistency and prevents unexpected changes.</p>
<h2 id="heading-thanks-note"><strong>Thanks note:</strong></h2>
<p><strong>Thank you for visiting my blog!</strong> I hope you found the steps helpful. If you enjoyed this post, feel free to follow me for more interesting and useful information. All my blogs will be in simple, general English, making it easy to understand. Stay tuned for more updates!</p>
]]></content:encoded></item></channel></rss>