TERRAFORM STATE MANAGEMENT
Sahdev Grover
Data Governance | Snowflake | Microsoft PowerApps | Microsoft Power Automate | Microsoft PowerBI | Alation | Immuta | Airflow | SQL | Python | AWS | Terraform | Docker
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.
(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:
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):
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:
2. Move (terraform state mv):
领英推荐
terraform state mv aws_instance.myfirstEc2 aws_instance.webserver
It will rename your resource.
Practical Hands-on:
Outcome:
3. Pull (terraform state pull):
terraform state pull
It will show you the current status of your resources.
4. Push (terraform state push):
terraform state push
It will push your state file into your remote backend.
5. Remove (terraform state rm):
terraform state rm aws_instance.example_instance
It will remove the resource from your state file.
6. Show (terraform state show):
terraform state show aws_instance.webserver
It will show you the attributes of the resource you mentioned.
Practical Hands-on:
Therefore,
These Terraform state commands are essential for managing your infrastructure configuration and understanding what Terraform is doing with your resources.
BEST PRACTICES
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.
Data Governance | Snowflake | Microsoft PowerApps | Microsoft Power Automate | Microsoft PowerBI | Alation | Immuta | Airflow | SQL | Python | AWS | Terraform | Docker
1 年?? Access my GitHub repo: https://github.com/sahdevgrover/terraform-basic-to-advanced-resources
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!