This guide explains how to leverage the Terraform modules on IBM Cloud to rapidly develop your Terraform templates.
Terraform provides modules as a language construct that allow you to abstract away re-usable parts —essentially, configure once and use everywhere. Terraform modules allow you to group resources together to define the following:
- Input variables that are used to change required resource configuration parameters
- Output variables that other resources or modules can use
We recently announced Terraform modules for IBM Cloud, which are highly reusable, sharable, inheritable and can assemble the configuration faster in a more concise manner. You can also invoke modules multiple times, either within the same configuration or in child module configurations.
IBM Cloud Terraform templates
IBM Cloud Terraform templates are a collection of HashiCorp Language configuration files that are built for a purpose. The templates wrap and consume one or more modules to provision a stack of IBM Cloud resources. You can build your cloud infrastructure by using an assembly of resources, data sources and module blocks.
To create a template, create a Terraform working directory in your local machine or Git repository and a module block as shown in the image. You can provide a unique name for the module parameter, and to dynamically download the module configuration, use source parameter. Allowed values are Terraform registry, GitHub repository or local file path:
terraform init command to dynamically download the Terraform providers and Terraform modules from the source location. Terraform modules from remote location gets installed in the .terraform/modules directory of your working directory. For local modules, Terraform will create a symbolic link in the module's directory, as shown in the image:
Note: In order to download the latest addition, removal or modification to the modules, you need to initialise in the working directory by using the
terraform init command.
The following is a sample
terraform init command image with an output:
You can specify the version of Terraform provider you need to download. The image shows the IBM Cloud provider requirements in the
Note: Only one provider can be specified for each block:
The following image shows a sample module and the parameters defined for the IBM Cloud Log Analysis with LogDNA resource:
In addition to these parameters, you can add the
version parameter to specify the module version. The version parameter has more control to incorporate the latest configuration when using with the third-party modules. It also helps in testing changes of different versions in environments other than production. You can gradually roll out new versions on production once the testing and validation are complete.
Using IBM Cloud Terraform modules to build Terraform templates
Here is a sample use case to provision an IBM Cloud Virtual Private Cloud (VPC) cluster in both production and stage environments. Without the usage of IBM Cloud Terraform templates and modules, you would have to copy all the configuration files in both the production and stage environments. If you apply the Terraform template, you need to register the VPC cluster with the Terraform registry or copy the module to a common folder and refer to it from both the environments, as shown in the image. Therefore, one of the advantages of using Terraform templates is to create reusable components and to treat pieces of infrastructure as black boxes:
Provisioning the IBM Cloud Virtual Private Cloud cluster
Use the IBM Cloud Terraform template and Terraform v0.13 to provision the IBM Cloud Virtual Private Cloud cluster resource. Follow the steps to provision the resource:
1. Create the directory and files
Create your working directory or Git repository and configuration files, as shown in the following image:
This list describes of the importance of the configuration files:
main.tf: Contains the template block with the source, arguments and version
variables.tf: Contains a list of variables with the definitions
versions.tf: Contains a provider block
README.md: Contains a description about the infrastructure that you can provision by using the template
input.tfvars: Contains the complex input variable definitions, such as map, list, etc.
2. Configuring the
main.tf file contains the template block with the source path to download the modules configuration, arguments and version:
3. Configuring the
provider.tf file shown in the image contains the parameters to download the right Terraform provider version for the resource:
4. Run the
terraform init command from your working directory
5. Run the
terraform apply -var-file=input.tfvars command to execute the Terraform plan and apply
yes to provision your resource, and observe as your apply completes successfully
7. Verify that your IBM Cloud VPC cluster is provisioned in the IBM Cloud console
With relatively little effort and very few lines of code, you can configure your resources by using IBM Cloud Terraform modules.