Provisioning multiple instances of a Terraform module
We learned in Chapter 3, Scaling Your Infrastructure with Terraform, that some Terraform language features, such as count
and for_each
, provision multiple instances of the same resource.
Before the release of version 0.13 of Terraform, it was not possible to create multiple instances of the same module. If we wanted to create several instances of resources that were in a module, we had to apply the for_each
or count
expression to each resource that was referenced in the module, which could complicate the maintenance of such a module.
One of the new features of the release of Terraform 0.13 is that we can now use count
and for_each
directly on the module block. For more information about this release, read this blog announcement: https://www.hashicorp.com/blog/announcing-hashicorp-terraform-0-13.
In this recipe, we will learn how to use for_each
on Terraform modules.
Let’s get started!