Terraform Refresh
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform import.
https://cloudaffaire.com/terraform-import/
In this blog post, we will discuss terraform refresh. The terraform refresh command is used to reconcile the state Terraform knows about (via its state file) with the real-world infrastructure. This can be used to detect any drift from the last-known state, and to update the state file. This does not modify infrastructure but does modify the state file. If the state is changed, this may cause changes to occur during the next plan or apply.
Next, we are going to explain terraform refresh with a demo.
Terraform refresh:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
##------------------------ ## Terraform: Refresh ## ##------------------------ ## Create a directory and get inside it mkdir terraform && cd terraform ## Create resources vi main.tf ------------------ #creates a vpc provider "aws" { access_key = " secret_key = " region = "ap-south-1" } resource "aws_vpc" "myvpc" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true tags { Name = "myvpc" } } ------------------ :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply ## Show state terraform show #login to aws console and change myvpc tag to mynewvpc ## Refresh the state ## terraform plan\apply\destroy also does a refresh terraform refresh ## Show state (tags.Name changed from myvpc to mynewvpc) terraform show ## Plan the change (matches main.tf with terraform.tfstate and output changes that will be applied) #tags.Name: "mynewvpc" => "myvpc" terraform plan ## Apply the change terraform apply ## Show state (tags.Name changed from mynewvpc to myvpc again) terraform show ## Cleanup terraform destroy -auto-approve cd && rm -r terraform |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform terragrunt.
To get more details on terraform, please refer below terraform documentation.
https://www.terraform.io/docs/index.html