Jam Notes #2: Terraform Templates

Contributions by Antoni Tzavelas and Darko Mesaroš

Welcome to our note series on our weekly Jam sessions, where we cover relevant DevOps topics with rotating guest speakers.

To save you time and energy, we’ll be uploading each Jam session topic’s notes, along with helpful tips.

For the week of May 5th, we covered Terraform templates (a theme that we’ve also covered in-depth in blog posts). We’ll go over what makes a good template, documentation best practices, and different strategies you can use to deploy Terraform templates.

Antoni Tzavelas (Google Certification Trainer) and Darko Mesaroš were our guest speakers on the topic.

Join our Slack to stay in the loop about our future weekly Jam Sessions on DevOps topics ranging from IaC to Terraform Templates.

1. What makes a good Terraform template?

  • Separate the Terraform template by modules, so that different components can be worked on by different teams — this enables easier and faster collaboration across teams

2. What are common mistakes in Terraform templates?

  • Not separating persistent from non-persistent data. Always separate your persistent data from non-persistent data (things that can’t ever be deleted versus things that can be deleted)
  • Failing to treat Infrastructure as Code (IaC) as code. It is code, so put it in a repo, create tests where you can, and use it in a pipeline

3. What are Terraform templates best practices?

  • Be parameter agnostic
  • Build a TF template whenever you need to do anything twice
  • If you can automate it, you should. No infrastructure is too small to automate. Codify everything, everything is an API
  • KISS (Keep It Simple Stupid):
  • Don’t go overboard in building Terraform templates — It can be tempting to think, “oh what if down the road we need this” — but focus on what you need
  • Don’t reinvent the wheel for something that doesn’t yet exist
  • Spend your time wisely, do what you need to do, don’t add in too much, don’t make your Terraform templates so complex that you’re dealing with 2,000 lines of code per module. Time is money, so don’t make it complex to deploy
  • If you make it complex with layer upon layer and someone else has to maintain it, they’ll be knocking at your door asking why you did it this way

4. Who builds the Terraform templates in an organization and who customizes them?

  • In large organizations, they practice Cloud Center of Excellence where the architects build out the building blocks and individual DevOps teams customizes and maintains the Terraform templates

5. What are small to medium-size organization best practices?

  • Don’t host your code on file share

6. What are documentation best practices?

  • IaC is self-writing documentation
  • Someone should be able to look at your module and read it as a book and pull it apart and understand what it is
  • IaC is also self-evolving documentation

7. How can you make code understandable for non-technical people?

  • Dropbox or Atlassian tools for maintaining documentation

8. How do software developers who don’t know about cloud know how to use the Terraform templates?

  • In most scenarios, you use the commands Terraform plan and apply
  • But, if you want your development team to deploy a change for infrastructure, you want them to do it through a proper CI/CD pipeline, not through Terraform plan and apply (this is a critical point)
  • GitOps methodology for IaC developer side is super important
  • Tooling in the background that will recognize changes and deploy that change to an environment
  • Configuration management tools that will deploy that code without developers struggling with the existing infrastructure

9. What are strategies to deploy Terraform templates to different environments?

  • When it comes to production, if you’re doing less releases per cycle, doing it manually is not bad. This is a scenario where everything else in between is automated but the final step is done manually. This also gives you time to do A/B testing
  • Pipelines orchestrate workflows towards staging, development, or production
  • Create different tfvar files for different Terraform templates (pick and choose which template you want for specific environments)

Final pieces of advice:

Antoni: 3 things to remember: Learn, Build, Document

— -
Did you find these tips on Terraform templates useful? We’ll continue posting our Jam session notes to provide you with up-to-date Terraform resources.

If you’re looking to try a new (free) tool to spin up your infrastructure easily, we recommend that you check out InfraSketch.

And, if you’re looking to get more Infrastructure as Code tips: join our Slack community to connect with other DevOps professionals.

PS: Did you know we have weekly Jam sessions on DevOps topics? The weekly invites go out on our Slack and Twitter channels. We hope to see you there!

Founder at InfraCode — customizable, reliable Infrastructure as Code tools. Simplifying the lives of DevOps professionals. www.infrastructurecode.io