May 15, 2024
Get featured on Geeklore.io
Geeklore is in very early stages of development. If you want to help us grow, consider letting us sponsor you by featuring your tool / platform / company here instead of this text. 😊
Hi there, users! I recently made a blog post focused on Terraform for beginners where I went over what IAC (infrastructure as code) is, what Terraform is, and what Variables & Outputs are. In this post, we will dive a little deeper and turn you into a Terraform master!
I will be using DigitalOcean as a cloud provider to test my Terraform code and input screenshots. You can support me by using my referral to register - here. Thank you and I hope you enjoy the post & learn something!
Variables are pretty self-explanatory. They are like children who always ask questions. They are always like "What is the name of this; What is the name of that?". And once you tell them what the name of the stuff they are asking for is, they can continue on with their day.
We usually use variables in modules when we want to re-use certain pieces of code but for N-number of resources. We define the variables IN the folder/directory our module is:
We are defining variable with the following block:
variable "some_variable" {
default = ""
}
The default is the default value of the variable. Usually, when I am using variables, I am leaving the default a blank string and work it from there.
Outputs are basically information being exported once your script finishes (or after). You can export all kinds of data and for the sake of this tutorial, I will export the IPv4 address of my newly created droplet:
The syntax for writing outputs is the following:
output "output_name" {
value = output.value
}
Locals are (in my opinion) variables but when you don't need to change the variable (local) VALUE, however when you still need to re-use it. If we were to use variables, we are would be changing the value constantly to match our new resource. With locals, we understand we will have to use the same value more than an N-number of times, so we just create a local.
We are defining a local with the following block:
locals {
local_name = "local_value"
}
Okay, I think you got it:
Oh, my. What a great topic - MODULES! Well, imagine you have to provision infrastructure with 10 VMs, each of which being paired with a load balancer. Oh god, your main.tf file's lines would be in the HUNDREDS, even in the thousands if you need to provision more. Well, user, there's a solution! It's called creating a module.
Modules are reusable pieces of code, where you make use of variables. To create a module, we first need to create a new folder in which we store our main.tf file for the module:
Now, we have to call the module in our MAIN main.tf file. We do so by inserting the following block:
module "module_name" {
source = "/path/to/module"
}
Okay, so if we want to create 2 different droplets, we will create a module, and we will create an N-number of variables in it. Then we will call it on our MAIN main.tf file. It goes like this:
module "droplet_01" {
source = "/path/to/module"
droplet_name = "droplet-name-1"
droplet_region = "nyc1"
}
module "droplet_02" {
source = "/path/to/module"
droplet_name = "droplet-name-2"
droplet_region = "nyc2"
}
What we did with the following code was to:
And that's basically it. That's modules.
Take a look at a couple of screenshots to get to understand more about how they are being used:
Module creation - it happens in a different directory than the MAIN main.tf file:
Module variables:
Module definition:
In the last section of my post, I want to give you a cheat-sheet so if you ever forget something, you will always be backed:
variable "variable_name" {
default = ""
}
locals {
local_name = "local_value"
}
output "output_name" {
value = output.value
}
module "module_name" {
source = "/path/to/module"
}
Right, that's it, user. Once again, I hope you learned something today and if you want to know more about Terraform and IaC (Infrastructure as Code), make sure to check my other post
Stay safe!
Latest Comments