Modules in Action
Explore how to create and use Terraform modules in AWS by building reusable SQS queues with dead letter capabilities. Understand module variables, outputs, and how to instantiate multiple module instances for scalable infrastructure management. This lesson helps you organize Terraform code better and apply consistent changes across many resources easily.
Project example
Let’s go into an example of how a module works. For this example, the code and directory structure will be listed, as you need to have a nested folder for a module.
Folder structure
Code
variable "queue_name" {
description = "Name of queue"
}
variable "max_receive_count"{
description = "The maximum number of times that a message can be received by consumers"
default = 5
}
variable "visibility_timeout" {
default = 30
}
We have written quite a lot of code. Let’s dive into it piece by piece and explain what it is all doing.
-
In a module, you can take arguments.
This allows you to give the user a chance to specify things about this instance of a module.
-
The module that we have written creates two AWS SQS queues.
One of the queues is a dead letter queue of the other.
-
For our module, we allow the user to specify the name of the queue.
We do this by defining the variable
queue_name.
variables.tf
Variables have a special meaning when used with a module; they become the input values for your module. Note
that inside a module, Terraform does not care what the file names are as long as they end in .tf.
However, there is a convention where variables go in a file called variables.tf, so we will
stick with that. As the queue_name variable does not have a default, a value must be given when the module is used. Variables in modules act in the same way as ...