{"id":8065,"date":"2019-12-20T02:14:05","date_gmt":"2019-12-20T02:14:05","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=8065"},"modified":"2021-11-15T07:09:56","modified_gmt":"2021-11-15T07:09:56","slug":"terraform-state-explained","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/terraform-state-explained\/","title":{"rendered":"Terraform state explained!!!"},"content":{"rendered":"\n<p>By &#8211; christophchamp<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Terraform keeps the&nbsp;<strong>remote state<\/strong>&nbsp;of the infrastructure<\/li><li>It stores it in a file called&nbsp;<code>terraform.tfstate<\/code><\/li><li>There is also a backup of the&nbsp;<em>previous<\/em>&nbsp;state in&nbsp;<code>terraform.tfstate.backup<\/code><\/li><li>When you execute&nbsp;<code>terraform apply<\/code>, a new&nbsp;<code>terraform.tfstate<\/code>&nbsp;and backup is created<\/li><li>This is how Terraform keeps track of the remote state<ul><li>If the remote state changes and you run&nbsp;<code>terraform apply<\/code>&nbsp;again, Terraform will make changes to meet the correct&nbsp;<em>remote state<\/em>&nbsp;again.<\/li><li>E.g., you manually&nbsp;<strong>terminate<\/strong>&nbsp;an instance that is managed by Terraform, after you run&nbsp;<code>terraform apply<\/code>, it will be&nbsp;<strong>started<\/strong>&nbsp;again.<\/li><\/ul><\/li><li>You can keep the&nbsp;<code>terraform.tfstate<\/code>&nbsp;in version control (e.g.,&nbsp;<a href=\"https:\/\/wiki.christophchamp.com\/index.php?title=Git\" target=\"_blank\" rel=\"noopener\">git<\/a>).<ul><li>This will give you a history of your&nbsp;<code>terraform.tfstate<\/code>&nbsp;file (which is just a big JSON file)<\/li><li>This allows you to collaborate with other team members (however, you can get conflicts when two or more people make changes at the same time)<\/li><\/ul><\/li><li>Local state works well with simple setups. However, if your project involves multiple team members working on a larger setup, it is better to store your state remotely<ul><li>The Terraform&nbsp;<strong>state<\/strong>&nbsp;can be saved remotely, using the&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/backends\/index.html\" target=\"_blank\" rel=\"noopener\">backend functionality<\/a>&nbsp;in Terraform.<\/li><li>Using a remote store for the Terraform state will ensure that you always have the latest version of the state.<\/li><li>It avoids having commit and push the&nbsp;<code>terraform.tfstate<\/code>&nbsp;file to version control.<\/li><li>However, make sure the Terraform remote store you choose supports locking! (note: both s3 and consul support locking)<\/li><\/ul><\/li><li>The default state is a&nbsp;<strong>local backend<\/strong>&nbsp;(the local Terraform state file)<\/li><li>Other backends include:<ul><li>AWS S3 (with a locking mechanism using DynamoDB)<\/li><li>Consul (with locking)<\/li><li>Terraform Enterprise (the commercial solution)<\/li><\/ul><\/li><li>Using the backend functionality has definite benefits:<ul><li>Working in a team, it allows for collaboration (the remote state will always be available for the whole team)<\/li><li>The state file is not stored locally and possible sensitive information is only stored in the remote state<\/li><li>Some backends will enable&nbsp;<strong>remote operations<\/strong>. The&nbsp;<code>terraform apply<\/code>&nbsp;will then run completely remotely. These are called&nbsp;<a href=\"https:\/\/www.terraform.io\/docs\/backends\/types\/index.html\" target=\"_blank\" rel=\"noopener\">enhanced backends<\/a>.<\/li><\/ul><\/li><li>There are two steps to configure a remote state:<ol><li>Add the back code to a&nbsp;<code>.tf<\/code>&nbsp;file<\/li><li>Run the initialization process<\/li><\/ol><\/li><\/ul>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_27103\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_84699\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_27103\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>By &#8211; christophchamp Terraform keeps the&nbsp;remote state&nbsp;of the infrastructure It stores it in a file called&nbsp;terraform.tfstate There is also a backup of the&nbsp;previous&nbsp;state in&nbsp;terraform.tfstate.backup When you execute&nbsp;terraform apply, a new&nbsp;terraform.tfstate&nbsp;and backup is created This is how Terraform keeps track of the remote state If the remote state changes and you run&nbsp;terraform apply&nbsp;again, Terraform will make&#8230;<\/p>\n","protected":false},"author":1,"featured_media":8074,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5129],"tags":[5598,383,5597,5130,213],"class_list":["post-8065","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-terraform","tag-expalined","tag-remote","tag-state","tag-terraform","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8065","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=8065"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8065\/revisions"}],"predecessor-version":[{"id":25281,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/8065\/revisions\/25281"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/8074"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=8065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=8065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=8065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}