5.1. Modules
Preparation
Create a new directory for this exercise:
mkdir -p $LAB_ROOT/advanced/modules
cd $LAB_ROOT/advanced/modules
Optional: Create empty file:
touch main.tf
Step 5.1.1: Define the module
A local module resides in its own directory, lets create one be running:
mkdir random_file
Create a new file named random_file/variables.tf and add the following content:
variable "extension" {}
variable "size" {}
Create a new file named random_file/main.tf and add the following content:
resource "random_pet" "filename" { }
resource "random_password" "content" {
length = var.size
}
resource "local_file" "this" {
filename = "${random_pet.filename.id}.${var.extension}"
content = random_password.content.result
}
Create a new file named random_file/outputs.tf and add the following content:
output "filename" {
value = local_file.this.filename
}
Explanation
It is common practice implementing a module with these three files:
main.tfvariables.tfoutputs.tf
For modules with many resouces (10+), it is advised to split main.tf into groups of resources.
Step 5.1.2: Create two instances of the module
Create a new file named main.tf and add the following content:
module "first" {
source = "./random_file"
extension = "txt"
size = 1337
}
module "second" {
source = "./random_file"
extension = "txt"
size = 42
}
output "filenames" {
value = [
module.first.filename,
module.second.filename
]
}
Now run
terraform init
terraform apply
Explanation
We instantiate the random_file module two times and specify different parameters. The output filenames prints
the randomly generated filenames.