O'Reilly logo

Getting Started with Terraform - Second Edition by Kirill Shirinkin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 output 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 the 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" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required