Module Structure

Study in detail the module structures in Terraform.

Now that we learned about the different parts of a Terraform module, we can quickly cover how Terraform modules are structured.

For understanding the structure of the module, we’ll create a module with the following files:

  • main.tf
  • vars.tf
  • outputs.tf

There’s no hard requirement to have these three files or name them this way, but it’s a convention followed by most publicly available modules, so we’ll use them as well.

In this lesson, we’ll create the now-familiar consumer and consumed modules and introduce Terraform functions.

We can use this as an opportunity to consolidate our knowledge by playing around with the code as we type it out.

A three-file structure

We’ll create the following files:

  • vars.tf: Contains the variables we need for our module.
  • outputs.tf: Contains the outputs from the module.
  • main.tf: Includes Terraform resources by convention.

Get hands-on with 1200+ tech skills courses.