Monday, November 9, 2020

How does Terraform stores state information Remotely | Terraform state Best practices | Terraform Remote state store

One of the things that Terraform does (and does really well) is to “track” your infrastructure that you provision. It does this through the means ofstate.

Terraform state is used to reconcile deployed resources with Terraform configurations. State allows Terraform to know what resources to add, update, or delete. 

This state is stored by default in a local file named "terraform.tfstate", but it can also be stored remotely, which works better in a team environment. 

Why state file should not be stored in your local machine?

  • Local state doesn't work well in a team or collaborative environment.
  • Terraform state can include sensitive information.
  • Storing state locally increases the chance of inadvertent deletion.
Terraform State Management Best practices:
1. Never store state file locally. store in remote places such as AWS S3, Azure blob storage.
2. Always create versioning for remote state file incase if you loose your remote state file or file is corrupted.
3. Never make any manual changes to the state file as it is a JSON file can be modified
4. Always create state file per environment
5. Deploy infrastructure changes using CICD tools instead of manually doing it 

Terraform uses this local state to create plans and make changes to your infrastructure. Prior to any operation, Terraform does a refresh to update the state with the real infrastructure.

Terraform state can include sensitive information. Storing state locally increases the chance of inadvertent deletion. Terraform supports the persisting of state in remote storags such Azure Storage or AWS S3 bucket

Please watch the steps in YouTube channel to learn how to store state file in S3 Bucket.


Please click here to learn how to automate infra set up using Jenkins and Terraform.


No comments:

Post a Comment

How to check whether my user data passing to EC2 instance is working | AWS EC2 bootstrap script log

The cloud-init output log file ( /var/log/cloud-init-output.log ) captures console output so it is easy to debug your scripts following a la...