Removing duplication with modules

Modules in Terraform are used to group multiple resources. You can reuse this grouping multiple times. You can also configure modules with variables, and modules can return outputs that you can pass to other resources and modules.

To get started with modules, create a folder named modules in the same folder as the template.tf. Inside this folder, create another one, named application. In this folder, we will keep the module, responsible for creating all resources required by a single application, be it MightyTrousers, CrazyFoods, or anything else.

A module is a regular Terraform template, so just create the ./modules/application/application.tf file with the following contents:

resource "aws_security_group" "allow_http" ...

Get Getting Started with Terraform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.