Self-invented “how to Terraform” rules

Levent Yalcin
LevOps
Published in
3 min readJun 2, 2017

While I’m working on something, anything, I really love to work methodically. If there are no rules in the area, I do my own rules and try not to violate them.

I think the reason is my father. He is the most organised and self-motivated human being I ever know. He always has his own rules/methods. I’m glad he is the influence on work-side of my life. I possibly could go and bear-hug him and say “Thank you” once again if I wasn’t thousands and thousands miles away.

So, I’m playing with Terraform every single day. I’ve created some rules for myself and changed them few times. In the end, I gained enough learning-from-mistakes experience with Terraform to stabilise my self-invented rules. Even the team I was working with liked the list and we made the rules for the team.

Here is the my list of rules while I’m Terraform-ing;

General

  • Avoid of creating templates per environment (dev/stg/prod) in subfolders. Try to manage that by passing variables.
  • If you need to create same resource or group of resources more than once, must be a module.
  • Main template should be in main.tf. No multiple files, no hard to read code.
  • Variables should be declared in variables.tf
  • Outputs should be in outputs.tf
  • A description should be provided for each declared variable.
  • Templates must be in `templates` directory on the repo/module root.
  • Modules must be consumed from tags. Not from master or any other branch.

Styling

Indentation and styling matters, write easy to read templates.

  • Use underscore_case for variables, resource and module names. (Always kebab-case for AWS tag names/values and resource’s name attributes)
  • Must use two-space soft tabs.
  • Must not use literal tab characters.
  • Must not contain trailing whitespace.
  • Should leave one empty line between resources.
  • Should leave one empty line between attributes and inner blocks (such as tag {}, ebs_block_device {} )
  • You read the code more than you PR and merge. Attribute assignments (=) should be aligned for clarity, except inner blocks. Inner blocks should have its own alignment.
  • Single line comments: #
  • Multi-line comments: /* followed by */
  • Always keep depends_on attribute at first line of the resource and leave one empty line after.

Modules

  • Repository per module or single repository sub-directory per module.
  • Everything is in the root of repository. No sub-directory (If it’s a single repo approach, directory depth must be 1), no complexity, no confusion.
  • Keep the modules as much as simple and small.
  • Module depth must not be greater than 1.
  • Always add README file into a module folder with description, input and output variables, and example of usage.
  • Any variable must not have default value.
  • Once you tested the module and make sure it works, create a tag with semantic versioning

You can see an example template I wrote in here.

I’m wondering about if you have any list like this or if there is something you didn’t like or a better idea please comment below. Thanks for reading and hope you enjoyed it.

Edit: If you’ve made until here, you might like the following post as well.

--

--

Levent Yalcin
LevOps
Editor for

DevOps, Cyclist, tea consumer, coffee lover, good experience with accidents and injuries