Working with Terraform Modules

Following from our last few weeks we would like to introduce you to some more advanced concepts in Terraform.

Last week we talked about the creation of resources using the terraform, and it’s a great way, however as you going to be creating multiple of the same type of resource it will become very long and repetitive code making it very hard for you to manage and maintain as you continue. That’s why Terraform incorporate an idea of Modules.

Modules

Think of modules in Terraform as libraries of re-usable content. They allow you to create multiple resources by using the re-usable code over and over again. There are few ways you can create modules, you can build them yourselves, use a terraform registry of modules, or even refer to a git repository. An example of a module is below:

Let’s break that code up then and explain all parts of it:

module “master_global” –  this tells terraform that we are creating a new module, the name here has to be unique in your terraform and you can’t re-use the same module names the same as you can’t create resources that have the same names.

source – this is telling terraform where to look for the .tf files that are being used by that module, you can point that at many different items, in our example I have a folder inside this folder that contain all my required references, but you could call a terraform module library (https://registry.terraform.io/) or you can even point it at a git repository if you would like.

Variables – in our scenario our module require us to provide the 3 variables, the company_name, the logging_cloudtrail_name and aws_organisation_id the variable that the module require to be provided in order to run correctly.

Variables

Let’s talk about the variables for a bit, there can be multiple types of variables int the terraform, and you would normally create a file variable.tf inside the module folder for ease of management and maintenance of it. The most often used types is string and list and map.  The beauty of variables is that they can have a default value, so for example you can determine that the size of VM by default one value, meaning that only if you see that your specific resource needs a different size you provide that in your module code, otherwise you don’t have to supply that. This allows you to minimise the amount of code needed to be provided, and as well allows you to quickly update a value for all your resources. An example of variable with default value can be seen below:

If you don’t have a default value supplied the value need to be provided to the module in order for it to work correctly.

In order to call on specific variable on your code you put var.name_of_variable in the value field, so for example:

In this example we are going to create an Azure API Management, what you can see is we are using variable inside a name string, this means that we can re-use that code over and over and it will create different API Management based on the resource_prefix variable that we would supply from the module. In order to supply a variable with a default value you would need to have

Variable – String

The string would be just a text so you can see above we have company name as well as logging_cloudtrail_name as a string – this will be used as reference somewhere else, you could provide for example a string like: eu-west-2 and re-use that as a variable for region, so when you want to change your AWS or Azure region for your entire application you could possibly change it in just one place and this would then update all your resources.

Variable – list

A list is going consist of multiple values, you create a list by putting [] and inserting values in there, so you could have:  [“eu-west-1a”,”eu-west-1b”] and this would then create a resources in 1a and 1b region

Variable – map

A map is a key/value structure, very similar to the lists in the way you create it, a map allows you to for example specify what image you will use based on the value of the region, the map is created by putting {} and putting the key/value in side, so an example would be:

              {“eu-west-01” = “ami-basdf72”
               “eu-west-02” = “ami-asdf34”}

 

Next week we will look at using Terraform in your CI/CD pipelines and how to incorporate the Terraform with your DevOps practises as well as other tools.

If you got any questions or you would like us to help you enable the value of Terraform in your organisation please feel free to reach out!

Share

Share on linkedin
Share on twitter
Share on facebook

Leave a Comment

Your email address will not be published. Required fields are marked *

TOP ON INSIGHTS

Single, Multi or Poly Cloud?

Those three terms are appearing a lot at the moment, you hear consultancies and companies discussing the right approach. However, in our experience, we have

Read More »

The Journey

Monday the 8th of April 2019 marked a new landmark on our Journey — We have officially moved into our new office space in Reading. There is

Read More »
Scroll to Top