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:
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.