Terraform Modules
Terraform Module Basics
- Terraform Modules are containers for multiple resources that are used together.
- A module consists of a collection of .tf and/or .tf.json files kept together in a directory.
- Modules are the main way to package and reuse resource configurations with Terraform.
Root and Child Modules
- Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.
- A Terraform module can "call" other modules to include their resources into the configuration.
- A module that has been called by another module is often referred to as a child module.
- Child modules can be called multiple times within the same configuration, and multiple configurations can use the same child module.
Published Modules
- Terraform can load modules from a public or private registry
- The Terraform Registry hosts a broad collection of publicly available Terraform modules for configuring many kinds of common infrastructure. These modules are free to use, and Terraform can download them automatically if you specify the appropriate source and version in a module call block.
- Here's an examplefrom aws.
These examples are not easy to read from the documentation, but there's a good example in the next section.
aws vpc module - There are many sources for modules.
Each one has its own syntax:
Calling a module
- To call a module means to include the contents of that module into the configuration with specific values for its input variables.
- Modules are called from within other modules using module blocks:
1module "servers" {
2 source = "./app-cluster"
3
4 servers = 5
5}
- A module that includes a module block like this is the calling module
- The label immediately after the module keyword is a local name, which the calling module can use to refer to this instance of the module.
- Within the block body (between { and }) are the arguments for the module. Module calls use the following kinds of arguments:
- The source argument is mandatory for all modules.
- The version argument is recommended for modules from a registry.
- Most other arguments correspond to input variables defined by the module. (The servers argument in the example above is one of these.)
- See more details here
Try this out: Using a module from the registry
- Try the Use registry modules in configuration example.
- This example will create a VPC and ec2 instances in your configured account.
- There are multiple ways to run this example:
- Interactive lab that is available to you. You don't have to install anything (so no terraform installation and no aws account), but it takes some time to bootstrap
- Terraform Community Edition which will rely on you having a (community edition) Terraform installation, an aws account with a configured cli. It will then instruct you to clone the source into your computer.
THIS IS THE VERSION I'D CHOOSE - Terraform Cloud, which relies on their cloud offerings.
- Follow the instructions to clone, view configuration, plan and apply this configuration.
- Note that you should find where everything is going to be deployed (hint: review the variables file)
- DON'T FORGET TO DESTROY EVERYTHING, as you'll be paying for those instances and nat-gateways.
Try this out: Writing your own module
Try out the Build and use a local module.