Category: Data

by In this article, we will see a subtle introduction to terraform modules, how to pass data into the module, get something from the module and create a resource (GKE cluster), it’s intended to be as simple as possible just to be aware of what a module is composed of, or how can you do your own modules, sometimes it makes sense to have modules to abstract implementations that you use over several projects, or things that are often repeated along the project. So let’s see what it takes to create and use a module.

A module can be any folder with a main.tf file in it, yes, that is the only required file for a module to be usable, but the recommendation is that you also put a README.md file with a description of the module if it’s intended to be used by people if it’s a sub-module it’s not necessary, also you will need a file called variables.tf and other outputs.tf of course if it’s a big module that cannot be split into sub-modules you can split those files for convenience or readability, variables should have descriptions so the tooling can show you what are they for, you can read more about the basics for a module here.

Then terraform.tfvars has some values to override the defaults that we defined: Now into the module itself, this module will create a GKE cluster, and while it’s not a good practice to have a module as a wrapper but for this example, we will forget about that rule for a while, this is the main.tf file: The variables.tf file: And finally the outputs.tf file: Notice that we have a lot more outputs than the one we decided to print out, but you can play with that and experiment if you want :)

Related Articles