Reuse Configurations with Modules
1. Reuse Configurations with Modules
Now that modules are created, the next step is to call the modules from the parent main.tf file. To call a module means to reuse it in your configuration. You can call the module to reference the code in the module block. In this example, main.tf is the calling module. The parent main.tf file uses the source argument to call the server and network modules. Run the terraform init command to download any modules referenced by a configuration. The source argument determines the location of the module source code. Every module you call within Terraform requires a mandatory source argument, which is a meta-argument within the module block. This value can either be a local path within the root directory or a remote path to a module source that Terraform downloads. Different source types supported by Terraform include Terraform Registry, GitHub, Bitbucket, HTTP URLs, and Cloud Storage buckets This course covers local paths, Terraform Registry, and GitHub. Let’s start with the local path. Local path is used to reference a module stored within the same directory as the calling module. For example, if the module that you want to call is stored in a directory named “servers”, located in the same directory as your root module, your root configuration will look like the example on screen. A local path starts with either . / or . ./. Local paths are unique when compared to other module sources, because they do not require any installation. The files are locally referenced from the child module to the parent module directly. Therefore no explicit update is required. Terraform Registry contains a directory of publicly available modules for various infrastructure components, such as load balancers, SQL instances, and so forth. These publicly available modules are incredibly useful for complex deployments. The registry source address has to be in the format //. To use the code published in a Terraform Registry for the Google Cloud provider, use the format terraform-google-modules/gcloud/google. To avoid any unwanted changes to your Terraform configuration, it’s recommended that you use version constraint. The version argument can be assigned a version string. The version string allows Terraform to automatically upgrade the module to new patch releases while still keeping a solid target. Only the modules installed from the Terraform Registry support this constraint. After Terraform Registry, the most commonly used remote source is the GitHub repository. Similar to Terraform Registry, you can directly enter the GitHub URL where the source code is located.2. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.