Getting Started with Terraform on IBM Cloud

Deepak Rai
4 min readJan 7, 2022

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

Terraform allows infrastructure to be expressed as code in a simple, human readable language called HCL (HashiCorp Configuration Language). It reads configuration files and provides an execution plan of changes, which can be reviewed for safety and then applied and provisioned.

Extensible providers allow Terraform to manage a broad range of resources, including IaaS, PaaS, SaaS, and hardware services.

In this demo, we will be trying to create and destroy a resource group using terraform.

To get started , you would need to install terraform in the local machine. The downloads can be found here https://www.terraform.io/downloads . There is an option of instaaling using the package manager and Binary download.

Pro Tip : Doing a manual install using Binary download is much easier than the package manager. This is the feedback we got from multiple users.

Once installed, it a user can check the version to verify the install. Users can chose to deploy Terraform to a specific folder or add it to the PATH.

terraform --version

Working with IBM Cloud

Another prerequisite is the make sure you install the IBM Cloud CLI using https://cloud.ibm.com/docs/cli?topic=cli-getting-started

Additionally, in this demo we will connect to our IBM cloud account with IBM Cloud API Key. From the portal (cloud.ibm.com) navigate to the “manage” tile on the top of the screen. Then, select the Access(IAM) option and eventually the IBM Cloud API Keys. You will need to create an IBM Cloud API Key and save the password !

Navigate to IBM Cloud API Keys
Create an IBM Cloud API Key

Resources to deploy

In this demo, we will be creating a resource group prod using terraform.

Building IaC

In a new folder, let us create a file ibm-resource-group.tf . This will be my only tf file where I will be defining the Settings block, Provider block and creating the resource group. Below is the code snippet

Code Snippet

Save this file .

Execute our Terraform

Before starting with the terraform, we need to export API credential tokens as the environment variables.

export IC_API_KEY="IBM Cloud API Key"

Before executing any terraform scripts, the “init” prepares the environment by ensuring the directory is properly configured. Then, the “plan” step compares the declared resources with the state file to print out the resources to be create, altered, and destroyed. This allows developers to see the impact of the main.tf file. Lastly, the “apply” command implements the changes declared during the “plan” step and deploys the resource to the IBM cloud account.

terraform init
output of terraform init
terraform plan
output of terraform plan
terraform apply
Output of terraform apply

You will get a prompt to approve this actions, please type yes and as you can see in the terminal the resource group will be created.

Verify on IBM Cloud

To verify the resource group created, you can login to IBM Cloud. Go to Manage->Account and select Resource groups under Account resources.

Terraform Destroy

Finally if you want to delete the resources created using terraform, you can using the terraform destroy command. It will ask for a validation if you really want to destroy the resources, you can type yes.

terraform destroy
output of terraform destroy

Thats all folks, you have created your first terraform template and created resources using terraform.

Links to refer :

https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/resources/resource_group

Terraform with IBM Cloud: A Tutorial with IBM Cloud Storage
The HashiCorp company develops a suite of open-source solutions specializing in cloud automation. In this blog, we are…medium.com

--

--