TERRAFORM STATE MANAGEMENT
35daysofterraformwithSahdev

TERRAFORM STATE MANAGEMENT

Advanced Concepts of Terraforming!

?? TERRAFORM STATE MANAGEMENT

When working with Terraform, managing the state of your infrastructure is a crucial aspect of maintaining consistency and ensuring that your infrastructure remains in the desired state.

Terraform uses a state file to keep track of the resources it manages.

(We have already discussed this multiple times in previous posts, so I believe you now have a better understanding and a firm grasp of the concept.)

Here's a deeper dive into Terraform state management.

  • Let's review the concepts once more for our convenience.

(You can see how far we've come, by revisiting concepts multiple times for better comprehension.)

?? UNDERSTANDING TERRAFORM STATE

Terraform state represents your infrastructure based on your defined Terraform configuration.

It is what it is!

It encapsulates crucial information about the resources you've created, their current configurations, and how they interact with each other.

This state is the foundation for planning and executing changes to your infrastructure.

(The state is everything for Terraform; that's why it's a foundational component.)

Why You Should Avoid Direct State File Edits?

Obviously, because our infrastructure solely depends on the state :).

Anyway,

let's delve further to clarify this point.

A fundamental rule in Terraform:

  • Never tamper with the state file directly.

It reminds me of scenes in movies, TV series, and even real life where the culprit/accused is caught red-handed tampering with evidence at the crime scene.

Similarly,

Do not get caught tampering with the state file in our case.

(Haha, just joking, but seriously, do not play with the state file; that's my only concern.)

Doing so can lead to chaos in your infrastructure.

(This goes without saying.)

Instead, Terraform provides a set of commands specifically designed for interacting with the state, ensuring that any changes are tracked and managed meticulously.

So,

We have an alternative way to ensure that changes are tracked.

Hence,

Terraform provides several sub-commands for managing the state effectively.

UNDERSTAND STATE MANAGEMENT COMMANDS WITH PRACTICAL EXAMPLES

1. List (terraform state list):

  • This command helps you see a list of resources that Terraform is keeping track of in its state file. The state file stores information about your infrastructure.
  • It allows you to see what Terraform is currently managing.Example:

If you’ve defined a virtual machine and a security group in your Terraform configuration, use this command

terraform state list        

It will show you these resources in the state.

Practical Hands-on:

THESE 2 RESOURCES ARE IN MY STATE FILE

2. Move (terraform state mv):

  • This command is used when you want to rename a resource without deleting and recreating it. It also makes a backup of the state before making changes.
  • It helps you change the name of a resource without causing unnecessary downtime.Example: Let’s say you have a resource named “myfirstEc2” and you want to rename it to “webserver” You can use this command.

terraform state mv aws_instance.myfirstEc2 aws_instance.webserver         

It will rename your resource.

Practical Hands-on:

CHANGE THE NAME FROM "myfirstEc2" to "webserver"

Outcome:

VERIFY IT, THE NAME HAS CHANGED!

3. Pull (terraform state pull):

  • This command downloads and displays the current state of your infrastructure from a remote location.
  • It’s useful for inspecting the current state of your infrastructure without making any changes.Example: If you have your Terraform state stored remotely, you can use this command

terraform state pull         

It will show you the current status of your resources.

4. Push (terraform state push):

  • This command is used to upload a local state file to a remote location, but it’s not commonly used.
  • It’s rarely used, and most state management is done through other Terraform commands.Example: If you want to move your state file from a local directory to a remote storage backend, you might use

terraform state push        

It will push your state file into your remote backend.

5. Remove (terraform state rm):

  • This command removes items from the Terraform state, but it doesn’t physically delete the associated resources. Terraform will no longer manage these resources.
  • You can use this when you no longer want Terraform to track certain resources without destroying them.Example: If you have an AWS instance you want to stop managing with Terraform, you can use this command

terraform state rm aws_instance.example_instance        

It will remove the resource from your state file.

6. Show (terraform state show):

  • This command displays the attributes of a single resource in the Terraform state.
  • It helps you inspect the details of a specific resource.Example: If you want to see the attributes of an EC2 instance named “webserver” you can use this command

terraform state show aws_instance.webserver        

It will show you the attributes of the resource you mentioned.

Practical Hands-on:

IT WILL SHOW ALL THE ATTRIBUTES OF THE RESOURCE YOU MENTIONED.

Therefore,

These Terraform state commands are essential for managing your infrastructure configuration and understanding what Terraform is doing with your resources.

BEST PRACTICES

  • Always rely on Terraform state commands when making state-related changes. This ensures that Terraform remains aware of these modifications, enabling it to plan future changes effectively.
  • Store your Terraform state in a remote backend. This facilitates collaboration and maintains a single source of truth for your infrastructure.
  • Implement version control, such as Git, to track changes in your Terraform configurations and state.

By adhering to these best practices and leveraging Terraform state commands, you'll master the art of state management, collaborate seamlessly with your team, and ensure the ongoing stability of your evolving infrastructure.

CONCLUSION

By discussing the aforementioned points in more detail and in a clearer manner, we have understood how crucial the state file is in Terraform, and hence, we must manage it carefully and meticulously.

I hope you have grasped the concept of Terraform state as well as the management of the state in Terraform by going through the sub-commands applied manually for managing the state in Terraform.

PS. Adhere to the best practices recommended above for leveraging the use of Terraform more productively.

Let me know if there’s anything else I can assist you with!

signing off!

Author: Sahdev Grover

Follow him for more Informative and Exciting Content.




Sahdev Grover

Data Governance | Snowflake | Microsoft PowerApps | Microsoft Power Automate | Microsoft PowerBI | Alation | Immuta | Airflow | SQL | Python | AWS | Terraform | Docker

1 年
回复
Nakul Grover

Senior DevOps Engineer @ Thomson Reuters | 4x AWS | 2x Kubernetes | 1x GitHub | 1x Terraform | Cloud & DevOps Trainer | Azure | Python | Topmate 78 + Booking

1 年

That's amazing Sahdev Grover, you have well articulated the advanced concepts of terraform in a very simplified manner. I hope this will be very useful for the ones who are learning terraform. Keep up the good work!

要查看或添加评论,请登录

Sahdev Grover的更多文章

  • TERRAFORM CLOUD & SENTINEL

    TERRAFORM CLOUD & SENTINEL

    Terraform Cloud Terraform Cloud manages Terraform runs in a consistent and reliable environment with various features…

    2 条评论
  • PROJECT COLLABORATION USING REMOTE STATES

    PROJECT COLLABORATION USING REMOTE STATES

    Welcome to Advanced Concepts of Terraforming!! Hey, everyone! I hope you all are enjoying this series and finding…

    1 条评论

社区洞察

其他会员也浏览了