# 🚀TERRAFORM: - State-File Priority

## 🌟 Terraform State File Priorities

Understanding how Terraform determines the state file location and behavior is crucial for effective infrastructure management. Here’s a breakdown of the priority order, with some helpful emojis to guide you:

### Priority Order

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1717087744136/345c8d9b-c2f2-4160-8c3f-fb3a9e44d8a2.jpeg align="center")

1. **🚩 CLI Flags (**`-state` and `-state-out`)
    
    * These have the highest priority and directly influence the input and output state files for the current operation.
        
    * Example:
        
        ```sh
        terraform plan -state="path/to/your/input.tfstate" -state-out="path/to/your/output.tfstate"
        ```
        
2. **🌐 Environment Variables (**`TF_STATE`)
    
    * If set, this will override the default local state file location.
        
    * Example:
        
        ```sh
        export TF_STATE=path/to/your/terraform.tfstate
        ```
        
3. **🔧 Workspace Configuration**
    
    * The currently selected workspace determines the state file within the backend configuration.
        
    * Example:
        
        ```sh
        terraform workspace new dev
        terraform workspace select dev
        ```
        
4. **📦 Backend Configuration in** `terraform` Block
    
    * Defines the default remote storage location and settings for the state file.
        
    * Example:
        
        ```powershell
        terraform {
          backend "s3" {
            bucket = "my-terraform-state"
            key    = "path/to/my/key"
            region = "us-west-2"
          }
        }
        ```
        
5. **📁 Local Default (**`terraform.tfstate`)
    
    * If no other configuration or overrides are provided, Terraform will use the local `terraform.tfstate` file in the working directory.
        

### 📝 Summary

* **🗂️ Defaults**: By default, Terraform uses a local `terraform.tfstate` file.
    
* **⚙️ CLI Overrides**: The `-state` and `-state-out` flags, `TF_STATE` environment variable, and workspace commands can override the default behavior.
    
* **🌍 Backend Configuration**: The backend specified in the Terraform configuration files determines the remote state storage.
    
* **📊 Priority**: CLI flags &gt; Environment Variables &gt; Workspace &gt; Backend Configuration &gt; Local Default.
    

Understanding these priorities and options allows you to effectively manage and customize the behavior of Terraform state files according to your infrastructure needs and workflows. Happy Terraforming! 🌱

# **Written by**

![](https://miro.medium.com/v2/resize:fit:381/0*du4W2OuqnYSli4DD align="left")

# [**Sivaranjan**](https://hashnode.com/@SivaRanjanAsokan)

♾️DevOps Engineer || ☁️ AWS Cloud || 🤵🏻 Jenkins || 🐳 Docker || 🏗️ Terraform || ☸️ K8s || 🐧Linux || 🐙 GitHub || 🌐Networking || 🖥️ Windows

# ***Follow For More: Thank You***

---

[![](https://miro.medium.com/v2/resize:fit:95/0*brrQ80dmcIkGPSZu.png align="left")](www.linkedin.com/in/sivaranjandevopscloud)

[Sivaranjan Asokan | LinkedIn](https://www.linkedin.com/in/sivaranjandevopscloud/)

---

[![](https://miro.medium.com/v2/resize:fit:95/0*_XsFuudLXP7wm6Gw.png align="left")](https://medium.com/@avsivaranjan)

[SivaRanjan — Medium](https://medium.com/@avsivaranjan)

---

[![](https://miro.medium.com/v2/resize:fit:143/0*GF27hLqim4C1-ZyI.png align="left")](https://sivadevops.hashnode.dev/)

[sivadevops.hashnode.dev](http://sivadevops.hashnode.dev)
