Skip to content

Terraform state

Represents the current state of the resources in the cloud Terraform
states

state mv allows to change the refrence so u avodi create and destroy action - rename exisitng resources - move a resource into a module - move a module into a module

Rename resource

# example
terraform state mv packet_device.worker packet_device.helper

Move resource into module

terraform state mv packet_device.worker module.worker.packet_device.worker

Move moudle into a module

terraform state mv module.app module.parent.module.app

Replacing Resources

Cloud resoruce can become degraded or damaged and u want to return it to a healty state

# provide the flag and the resource adress
#Works only on one resource!!!
# avaibable on plan and apply 
terraform apply -replace="aws_instance.example[0]"
Terraform State Backups

*All commands that modify state will write a backup file** - Terraform sotores it in terraform.tfstate.backup

Note Backup cannot be disabled!

to get rid of it u have to delete it mannualy

### Resource addressing Resource adressing terraform
#### Refresh-Only Mode Refresh-Only mode diffrences
### Terrafrom_remote_state Retrieves the root module output values from another Terraform configuration - Only the root-level values from the remote state snapshot are exposed. - Resource data and output values from nested modules are not accessible. - To enable that, explicitly configure a passthrough in the root module.**
Terraform Remote State
### Alternative to Remote State
The terraform_remote_state only exposes output values, requiring users to access the entire state snapshot, which may contain sensitive information.

IMPORTANT Publish data for external consumption to a sepearte location instead of using remote state Alternaive to the Remote
State


Terraform State Locking

Terraform will lock your state for all operations that could write sate.

  • Disableing Locing(not recommended -lock flag)
  • Force unlock(mannualy unlock satte if it failes)
    • If u unlock the sate when someone is hold a lock it causes multiple wirters issues

      • The command requaiers the uniqe lock id
      • Terraform wil oputput lock ID if unlocking fails
      terraform force-unlock <lock id> -force
      

NoteTerraform log doesn’t show if it fails or succeeds only if it takes to long