Terraform automation on IBM Cloud (#½): Provisioning a Kubernetes Cluster on IBM Cloud using Terraform

IBM Cloud goes with a user friendly interface for either provisioning new services or managing them. However sometime (very often actually :D) automation is needed.

In one of my recent assignments, I was asked to provide a Terraform code which is to be used inside a DevSecOps toolchain on IBM Cloud, so that users can provision automatically an IBM Kubernetes Service (a.k.a IKS: managed Kubernetes cluster on IBM Cloud).

What is going to be described below, is the code I wrote to test the automatic provisioning locally (from my laptop), before putting everything in the DevSecOps toolchain.

First of all, the variables should be set, either locally for the test purpose, or on the server side. The example below is to set the variables locally;



export TF_VAR_ibmcloud_api_key=”ibmcloud_api_key_xxxxxxx”

export TF_VAR_iaas_classic_username=”the_account_name_xxxxxxx”

export TF_VAR_iaas_classic_api_key=”ibmcloud_iaas_api_key_xxxxxxx”

export TF_VAR_ibmcloud_region=”ibmcloud_region_xxxxxxx”


A list of IBM cloud regions to be found here: https://cloud.ibm.com/docs/containers?topic=containers-regions-and-zones

Otherwise it could retrieved on command line (assuming the ibmcloud CLI is present);

Note: In order to initialize the variables with the script above on your local machine, it should be run with “source” option instead of execution of a standard bash file.

On server side though, the variables are set in the “providers.tf” file.


The variables on local machine or server side could be set by the “variable.tf” file as shown below.



For IKS, if your target is the classic infrastructure (as in my case), retrieve the zones with the following ibmcloud CLI;

ibmcloud ks zone ls --provider classic

Once the zone is known, we need to have the vlans information so the “private_vlan_id” and the “private_vlan_id” could be set;

The example expected output is something like:

Versions file

The “versions.tf” file should be something like the example below.



Main Script

After the steps above, the cluster creation is very straight forward (and the case shown here is quite simple).

All is done through the main Terraform script as provided below;

As you see, some parameters are hard coded, because it was intended to be this way. They could easily be set as parameters as well;

The Terraform script execution is done through the classic steps of;

To set the cluster parameters;

Useful information on IBM Cloud Terraform for clusters could be fined here: https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/container_cluster

IT guy for a long time, looking for technical challenges everyday!