A Quick Overview of Modules and Variables in Terraform

Mohammed Hoche
Feb 26 · 3 min read
Photo by Sigmund on Unsplash

Defining all our services in one main configuration file is all good … until we start building bigger environments.

As our infrastructure architecture becomes bigger, it becomes difficult to understand and navigate through a single configuration file. Furthermore, there will be an increase in duplication of similar blocks of configuration across multiple environments.

To address these issues, Terraform has introduced concepts that would tackle these problems, and make our Terraform scripts more dynamic and reusable.

In this blog, we will be investigating how to introduce Modules and input variables into our Terraform Script. We will be using the azure resource group as an example to understand these terraform concepts.

Input Variables :

Variables in Terraform are a great way to specify values that configure your infrastructure. The information in Terraform variables is saved independently from the deployment plans, which makes the values easy to read and edit from a single file.

Input variables are usually defined by stating a name, type, and default value. However, the type and default values are totally optional. Terraform can deduct the type of the variable from the default or input value.

It is considered best practice to predetermine input variables in a separate file. The syntax to define a variable is as simple as stating “variable “ followed by the name of the variable.

Here is an example below :

variable "ScottishSummit" 
{
type = "string"
default = "ScottishSummitResorurceGroup"
}

To use the input variable in the configuration file, it is as simple as this :

resource "azurerm_resource_group" "ScottishSummit"
{
name = var.ScottishSummit
location = "east us"
}

Modules in Terraform :

Modules allow the main configuration file to be shortened. They also allow Terraform scripts to be more reusable across multiple environments.

In simple Terraform modules is any terraform configuration file in a folder. All the configurations that we have currently written have been modules, however, it’s not a sophisticated module as we have deployed them directly.

To get started with Terraform modules, you will need to define a new file structure. This would allow us to create an Azure resource group in many environments without having to repeat the code.

Here is an example of a Terraform module file structure :

Environment 
└ dev
└ main.tf
└ vars.tf
└ test
└ main.tf
└ vars.tf
Modules
└ resource_group
└ main.tf
└ vars.tf

Once the file structured has been established, we can move the configuration code into the newly defined module.

resource "azurerm_resource_group" "ScottishSummit"
{
name = var.ScottishSummit"
location = "east us"
}

we can make use of this module in the development environment by defining a module in the configuration file.

module "resource_group" 
{
source = "../../Modules/resource_group"
name = var.ScottishSummit"
location = "east us"
}

Conclusion :

By adding variables and modules to your infrastructure as code, you will be able to increase your ability to build infrastructure quickly and reliably. If you would like to learn more about these concepts, check out Yevgeniy Brikman blog page.

Check out this GitHub page to see an example.

Bonne journée

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Mohammed Hoche

Written by

Full time developer | Lover of nature | Hoping to inspire and share productivity tools .

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Mohammed Hoche

Written by

Full time developer | Lover of nature | Hoping to inspire and share productivity tools .

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store