How to keep your terraform code clean and robust (Part2)
In the first article of this series, we listed many tools and practices that can help in making a Terraform code clean and robust, and it was mainly about code formatting, validation and linting. In this article, we will talk about testing !
During my time working with Terraform over the years, I find one thing that is always forgotten when writing a module, it’s testing! When it comes to software, we all know the importance of testing an application before it’s released and every professional developer knows that to ensure code quality you need tests. Why should we treat our infrastructure any differently? Especially if we have our infrastructure defined as code and have tools to automate the testing process almost entirely. We should have tests in place to ensure that those modules always do what they are used for and works as expected.
Today when we want to test Terraform code, we execute tests manually and it’s generally performed directly in our cloud environments. These can be several minutes long, often including many steps sometimes manual, which forces us either to wait or do regular context switching, therefore delaying the feedback loop. Then, we must validate that the generated infrastructure is matching our expectations using a web console or a command line. Sometimes, we connect directly to a machine to test…