How do I write reusable Terraform modules?

Hi Everyone,

I’m Kamal Hinduja, based in Geneva, Switzerland (Swiss) . Can someone guide me on how to write reusable Terraform modules?

Thanks, Regards

Kamal Hinduja Geneva , Switzerland

Terraform (Hashicorp) has a great set of tutorials: https://developer.hashicorp.com/terraform/tutorials
* first, you will want to create a HasiCorp Developer account and sign in so it can keep track of what you’ve done.

There is a sequence specifically for this: “Reuse Configuration with Moduleshttps://developer.hashicorp.com/terraform/tutorials/modules

This is a pretty general question.

High level, I think the main thing is to figure out who your audience is, as well as what things should be configurable or how varied your use cases are. In some cases, it’s also a good idea to consider third party modules, however, one disadvantage of this is that third party modules often are designed to handle lots of different use cases, whereas you may have relatively simple needs, in which case an in-house module may make a lot of sense. Even if you write modules in-house, you can also get a lot of good ideas about how to write a module from looking at / stealing from these third party modules.

The official style guide has recommendations for file naming, variable naming, etc.

I recommend a couple of tools:

  • tflint: Great tool that will give you good recommendations for module structure and detect some other problems. Make sure to enable the plugin(s) for your cloud provider(s), and turn on the checks for things like recommended module structure

  • terraform-docs: I recommend setting up your modules to be self-documenting from the start. If you add a headers (what I like to do here is to make a subdirectory in the module called examples/, and make my usage examples validatable (via terraform validate) and self-generating.

  • Some kind of test framework. Historically, these have been third party tools, but now you can do unit and / or integration tests with Terraform’s builtin test framework.

    • In particular, if you’re doing complicated business logic / conditionals / regex, strongly recommend writing unit tests using the mock provider to be able to quickly test various scenarios and avoid regressions or unexpected problems with your module’s logic.

In addition, do CI checks to make sure your module validates, has correct terraform formatting, etc. on every PR.

In the long run, you may want to look at module versioning and / or publishing. That said, for a smaller organization / team, I tend to recommend starting with a simple setup where you include and test your modules into the same repo the rest of your IAC stuff is.. this means, you’ll have to update anything that calls the module when things change, so there’s some level beyond which it won’t scale, but it makes things easier when you’re starting out and iterating fast IMO.

1 Like

@kamal12362 you can write a production ready reusable terraform module, by introduction variable and tfvars file where you only write things you need to to change into one file there after you can use var.config that will make it resuable and you just need to edit the only tf file you want to change.