TerraForm State Management

TerraForm State Management

#TerraWeek Day 4

The importance of Terraform state in managing infrastructure.

Terraform must store the state of your managed infrastructure and configuration. This state is used by Terraform to map real-world resources to your configuration, keep track of metadata, and improve performance for large infrastructures.

This state is stored by default in a local file named "terraform.tfstate", but we recommend storing it in Terraform Cloud to version, encrypt, and securely share it with your team.

By using a remote backend or Terraform Cloud, you can manage your state files in a centralized location and work collaboratively with your team. This ensures consistency and prevents conflicts in the configuration of your infrastructure.

Terraform uses the state to determine which changes to make to your infrastructure. Before any operation, Terraform does a refresh to update the state with the real infrastructure.
The primary purpose of Terraform state is to store bindings between objects in a remote system and resource instances declared in your configuration. When Terraform creates a remote object in response to a change of configuration, it will record the identity of that remote object against a particular resource instance, and then potentially update or delete that object in response to future configuration changes.

Understand different methods of storing state files:

Local state and terraform state command:

By default, Terrafrom stores the state file locally on the machine running the terrafrom command. This method is called local state storage and is suitable for small and simple infrastructures. Managing state files manually can lead to errors and data loss.

Local State Storage:

To enable local state storage, you don't need to do anything special. This is the default behaviour of Terraform. When you run a command like terraform apply, terraform will automatically create a state file named terraform.tfstate in the same directory as your configuration file.

Managing State with "terraform state " command

You can use the terraform state command to view, modify and delete resources within your state file.

terraform state list

Remote State Management:

With remote state management, you store the state file remotely, allowing multiple team members to work together on the same infrastructure code without affecting each other. There are various options for remote state management, including terraform cloud, AWS S3, Azure storage account or Hashicorp Consul.

When using remote state management, the terraform state is stored in a central location that can be accessed by all team members involved in the project. This helps eliminate issues associated with local state management, such as conflicting changes, version control, etc.

Creating Remote backend storage for tfstate files

  1. create an EC2 instance manually from the AWS dashboard with the free tier architecture to perform further procedures on it.

  2. Create a directory remote_infra.

  3. Now create separate files for providers, resources and main.tf file

providers.tf file: -

In resources.tf file:-

terraform.tf file:

Now run, $terraform init

Run terraform plan and terraform apply the command to make the changes. And when now you see, a tfstate file is created in the same directory

Now make another directory in which we will make a remote backend so that tfstate file will be created on the AWS S3 bucket.

$mkdir remote_demo

Here is the terraform.tf file.

the bucket is where the tfstate file is going to store

dynamodb_table to use for state locking.

In the main.tf file creates aws instance.

Now run, $terraform init

Then run,$terraform plan

You can see what we are going to do with the infrastructure. Now finally run,

$terraform apply

Now run the ls command and let see tfstate file is created or not?

There is no file called .tfstate locally. Let's check the AWS console now. Go to S3 buckets.

See our .tfstate file here.

Go to the Dynamdb table you can see the table is created.

By running terraform state file command, you can see the listed state.

Thank you for Reading!!!!!