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:

  • Contains the template block with the source, arguments and version
  • Contains a list of variables with the definitions
  • Contains a provider block
  • 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 file

The file contains the template block with the source path to download the modules configuration, arguments and version:

3. Configuring the file

The 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?

More from Cloud

The history of the central processing unit (CPU)

10 min read - The central processing unit (CPU) is the computer’s brain. It handles the assignment and processing of tasks, in addition to functions that make a computer run. There’s no way to overstate the importance of the CPU to computing. Virtually all computer systems contain, at the least, some type of basic CPU. Regardless of whether they’re used in personal computers (PCs), laptops, tablets, smartphones or even in supercomputers whose output is so strong it must be measured in floating-point operations per…

A clear path to value: Overcome challenges on your FinOps journey 

3 min read - In recent years, cloud adoption services have accelerated, with companies increasingly moving from traditional on-premises hosting to public cloud solutions. However, the rise of hybrid and multi-cloud patterns has led to challenges in optimizing value and controlling cloud expenditure, resulting in a shift from capital to operational expenses.   According to a Gartner report, cloud operational expenses are expected to surpass traditional IT spending, reflecting the ongoing transformation in expenditure patterns by 2025. FinOps is an evolving cloud financial management discipline…

IBM Power8 end of service: What are my options?

3 min read - IBM Power8® generation of IBM Power Systems was introduced ten years ago and it is now time to retire that generation. The end-of-service (EoS) support for the entire IBM Power8 server line is scheduled for this year, commencing in March 2024 and concluding in October 2024. EoS dates vary by model: 31 March 2024: maintenance expires for Power Systems S812LC, S822, S822L, 822LC, 824 and 824L. 31 May 2024: maintenance expires for Power Systems S812L, S814 and 822LC. 31 October…

IBM Newsletters

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