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.

Terraform modules on IBM Cloud are published in the HashiCorp Language modules registry and are available for your usage. 

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:

Run the 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 required_providers block.

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

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

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

6. Enter 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

What’s next?

With relatively little effort and very few lines of code, you can configure your resources by using IBM Cloud Terraform modules. 

We hope you’ll explore more about the IBM Cloud Terraform modules. To contribute to an existing IBM Cloud Terraform modules or to create modules, see the IBM Cloud getting started repository.

Was this article helpful?
YesNo

More from Cloud

How a US bank modernized its mainframe applications with IBM Consulting and Microsoft Azure

9 min read - As organizations strive to stay ahead of the curve in today's fast-paced digital landscape, mainframe application modernization has emerged as a critical component of any digital transformation strategy. In this blog, we'll discuss the example of a US bank which embarked on a journey to modernize its mainframe applications. This strategic project has helped it to transform into a more modern, flexible and agile business. In looking at the ways in which it approached the problem, you’ll gain insights into…

The power of the mainframe and cloud-native applications 

4 min read - Mainframe modernization refers to the process of transforming legacy mainframe systems, applications and infrastructure to align with modern technology and business standards. This process unlocks the power of mainframe systems, enabling organizations to use their existing investments in mainframe technology and capitalize on the benefits of modernization. By modernizing mainframe systems, organizations can improve agility, increase efficiency, reduce costs, and enhance customer experience.  Mainframe modernization empowers organizations to harness the latest technologies and tools, such as cloud computing, artificial intelligence,…

Modernize your mainframe applications with Azure

4 min read - Mainframes continue to play a vital role in many businesses' core operations. According to new research from IBM's Institute for Business Value, a significant 7 out of 10 IT executives believe that mainframe-based applications are crucial to their business and technology strategies. However, the rapid pace of digital transformation is forcing companies to modernize across their IT landscape, and as the pace of innovation continuously accelerates, organizations must react and adapt to these changes or risk being left behind. Mainframe…

IBM Newsletters

Get our newsletters and topic updates that deliver the latest thought leadership and insights on emerging trends.
Subscribe now More newsletters