DEV Community

Kevin Mack
Kevin Mack

Posted on • Originally published at welldocumentednerd.com on

Working With Modules in Terraform

I’ve done a bunch of posts on TerraForm, and there seems to be a bigger and bigger demand for it. If you follow this blog at all, you know that I am a huge supporter of TerraForm, and the underlying idea of Infrastructure-as-code. The value-prop of which I think is essential to any organization that wants to leverage the cloud.

Now that being said, it won’t take long after you start working with TerraForm, before you stumble across the concept of Modules. And it also won’t take long before you see the value of those modules as well.

So the purpose of this post is to walk you through creating your first module, and give you an idea of how to do this benefit you.

So what is a module? A module in TerraForm is a way of creating smaller re-usable components that can help to make management of your infrastructure significantly easier. So let’s take for example, a basic TerraForm template. The following will generate a single VM in a Virtual Network.

provider "azurerm" { subscription\_id = "...."}resource "azurerm\_resource\_group" "rg" { name = "SingleVM" location = "eastus" tags { environment = "Terraform Demo" }}resource "azurerm\_virtual\_network" "vnet" { name = "singlevm-vnet" address\_space = ["10.0.0.0/16"] location = "eastus" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" tags { environment = "Terraform Demo" }}resource "azurerm\_subnet" "vnet-subnet" { name = "default" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" virtual\_network\_name = "${azurerm\_virtual\_network.vnet.name}" address\_prefix = "10.0.2.0/24"}resource "azurerm\_public\_ip" "pip" { name = "vm-pip" location = "eastus" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" allocation\_method = "Dynamic" tags { environment = "Terraform Demo" }}resource "azurerm\_network\_security\_group" "nsg" { name = "vm-nsg" location = "eastus" resource\_group\_name = "${azurerm\_resource\_group.rg.name}"}resource "azurerm\_network\_security\_rule" "ssh-access" { name = "ssh" priority = 100 direction = "Outbound" access = "Allow" protocol = "Tcp" source\_port\_range = "\*" destination\_port\_range = "\*" source\_address\_prefix = "\*" destination\_address\_prefix = "\*" destination\_port\_range = "22" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" network\_security\_group\_name = "${azurerm\_network\_security\_group.nsg.name}"}resource "azurerm\_network\_interface" "nic" { name = "vm-nic" location = "eastus" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" network\_security\_group\_id = "${azurerm\_network\_security\_group.nsg.id}" ip\_configuration { name = "myNicConfiguration" subnet\_id = "${azurerm\_subnet.vnet-subnet.id}" private\_ip\_address\_allocation = "dynamic" public\_ip\_address\_id = "${azurerm\_public\_ip.pip.id}" } tags { environment = "Terraform Demo" }}resource "random\_id" "randomId" { keepers = { # Generate a new ID only when a new resource group is defined resource\_group = "${azurerm\_resource\_group.rg.name}" } byte\_length = 8}resource "azurerm\_storage\_account" "stgacct" { name = "diag${random\_id.randomId.hex}" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" location = "eastus" account\_replication\_type = "LRS" account\_tier = "Standard" tags { environment = "Terraform Demo" }}resource "azurerm\_virtual\_machine" "vm" { name = "singlevm" location = "eastus" resource\_group\_name = "${azurerm\_resource\_group.rg.name}" network\_interface\_ids = ["${azurerm\_network\_interface.nic.id}"] vm\_size = "Standard\_DS1\_v2" storage\_os\_disk { name = "singlevm\_os\_disk" caching = "ReadWrite" create\_option = "FromImage" managed\_disk\_type = "Premium\_LRS" } storage\_image\_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "16.04.0-LTS" version = "latest" } os\_profile { computer\_name = "singlevm" admin\_username = "uadmin" } os\_profile\_linux\_config { disable\_password\_authentication = true ssh\_keys { path = "/home/uadmin/.ssh/authorized\_keys" key\_data = "{your ssh key here}" } } boot\_diagnostics { enabled = "true" storage\_uri = "${azurerm\_storage\_account.stgacct.primary\_blob\_endpoint}" } tags { environment = "Terraform Demo" }}

Now that TerraForm script shouldn’t surprise anyone, but here’s the problem, what if I want to take that template and make it deploy 10 VMs instead of 1 in that virtual network.

Now I could take lines 64-90 and lines 103-147 (a total of 70 lines) and do some copy and pasting for the other 9 VMs, which would add 630 lines of code to my terraform template. Then manually make sure they are configured the same, and add the lines of code for the load balancer, which would probably be another 20-30….

If this hasn’t made you cringe, I give up.

The better approach would be to implement a module, so the question is, how do we do that. We start with our folder structure, I would recommend the following:

  • Project Folder
    • Modules
    • Network
    • VirtualMachine
    • LoadBalancer
  • main.tf
  • terraform.tfvars
  • secrets.tfvars

Now the idea here being, that we create a folder to contain all of our modules, and then a separate folder for each. Now when I was learning about modules, this tripped me up. You can’t have the “tf” files for your modules in the same directory, especially if they have any similar named parameters like “region”. If you put them in the same directory you will get errors about duplicate variables.

Now once you have your folders, what do we put in each of them, the answer is this…main.tf. I do this because it makes it easy to reference and track the core module in my code. Being a developer and devops fan, I firmly believe in consistency.

So what does that look like, below is the file I put in “Network\main.tf”

variable "address\_space" { type = string default = "10.0.0.0/16"}variable "default\_subnet\_cidr" { type = string default = "10.0.2.0/24"}variable "location" { type = string}resource "azurerm\_resource\_group" "basic\_rig\_network\_rg" { name = "vm-Network" location = var.location}resource "azurerm\_virtual\_network" "basic\_rig\_vnet" { name = "basic-vnet" address\_space = [var.address\_space] location = azurerm\_resource\_group.basic\_rig\_network\_rg.location resource\_group\_name = azurerm\_resource\_group.basic\_rig\_network\_rg.name}resource "azurerm\_subnet" "basic\_rig\_subnet" { name = "basic-vnet-subnet" resource\_group\_name = azurerm\_resource\_group.basic\_rig\_network\_rg.name virtual\_network\_name = azurerm\_virtual\_network.basic\_rig\_vnet.name address\_prefix = var.default\_subnet\_cidr}output "name" { value = "BackendNetwork"}output "subnet\_instance\_id" { value = azurerm\_subnet.basic\_rig\_subnet.id}output "networkrg\_name" { value = azurerm\_resource\_group.basic\_rig\_network\_rg.name}

Now there are a couple of key elements, that I make use of here, and you’ll notice that there is a variables section, a TerraForm template, and an outputs section.

It’s important to remember that every TerraForm template is self contained, similar to how you scope parameters, you pass the values into the module and then use them accordingly. And by identifying the “Output” variables, I can pass things back to the main template.

Now the question becomes, what does that look like to implement it. When I go back to my root level “main.tf”, I find I can now leverage the following:

module "network" { source = "./modules/network" address\_space = var.address\_space default\_subnet\_cidr = var.default\_subnet\_cidr location = var.location}

A couple of key elements to reference here, are that the “source” property points to the module folder that contains the main.tf. And then I am mapping variables at my environment level to the module. This allows for me to control what gets passed into each instance of the module. So this shows how to get module values into the module.

The next question is how do you get them out, in my root main.tf file, I would have code like the following:

network\_subnet\_id = module.network.subnet\_instance\_id

To reference it and interface with the underlying map, I would just reference, module.network.___________ and reference the appropriate output variable.

Now I want to be clear this is probably the most simplistic module I can think of, but it illustrates how to hit the ground running and create new modules, or even use existing modules in your code.

For more information, here’s a link to the HashiCorp learnsite, and here is a link to the TerraForm module registry, which is a collection of prebuilt modules that you can leverage in your code.

Top comments (0)