Understanding Terraform State

Tags:

terraform logo

Terraform state is a database kept by Terraform to help it reconcile Terraform resource dependencies, and reconcile between Terraform code and the actual state on the target infrastructure. For example, in the state, Terraform expects to find an entry that matches your AWS resource defining a Route53 record and in your Terraform code. Whether or not the entry exits determines if Terraform will make a change or not.

A record's state

The following table describes what Terraform will do regarding a resource depending on whether or not it exists in state and in AWS.

Exists in Terraform state Exist in AWS Terraform action
Yes Yes None, all is well.
Yes No Terraform will create this resource.
No Yes Terraform will destroy this resource.
No No None, all is well.

Terraform only considers resources that have a history in state. Resources that exist that were never in state are ignored. Thus you can have multiple states in multiple projects and not interfere with each other.

Importing state

State is created by terraform apply or terraform import. Import to import existing infrastructure into the state. You can further use terraforming to an imported resource into AWS Terraform resources code.

submit to reddit