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" { name = "allow_http" description = "Allow HTTP traffic" vpc_id = "${aws_vpc.my_vpc.id}" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0...