Modules in Action

Learn how to work with modules in detail.

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

Press + to interact
sqs-with-backoff/
main.tf
variables.tf
output.tf
main.tf

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
}
Terraform module code example

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