Cloud Native Apps

 View Only

Getting Started with Terraform and IBM Cloud

By Stephan Bester posted Thu June 16, 2022 02:25 AM

  
IBM Cloud + Terraform

In my previous blog post, Deploy a .NET 6.0 Web App to IBM Cloud Kubernetes Service, the IBM Cloud resources were created manually through the IBM Cloud Console.
In this blog post, we will explore how to automate creation of these resources using Terraform.

Prerequisites

The following items are required, but installation and configuration instructions will be included in this post.

  • Text editor, such as Visual Studio Code
  • Terraform
  • IBM Cloud Account (Free tier is sufficient)
  • IBM Cloud CLI

Environment

I will be using the Ubuntu 20.04 LTS distro running in Windows Subsystem for Linux (WSL) as my development environment. If you are using Windows 10 version 2004 or later, you can also follow along in WSL.
You can install a WSL distribution from the command line using a simple command.

wsl --install -d Ubuntu

For more information on how to install and get started with WSL, refer to the official documentation.

If you have already installed Visual Studio Code on your Windows machine, there is no need to install it into the WSL distribution as well, you can simply launch it normally by running the code command.

Install Terraform

Terraform is an automation tool from HashiCorp using Infrastructure-as-Code (IaC) to create and deploy infrastructure using declarative scripts.

You can find instructions on how to download Terraform for your operating system on the Terraform Downloads page.
For Ubuntu and Debian distributions the following can be used.

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform

Verify your installation using the help command.

terraform -help

Install IBM Cloud CLI

For this section you will need an IBM Cloud account - sign up for free if you don't have one already.
See Getting started with the IBM Cloud CLI  for installation instructions.

curl -fsSL https://clis.cloud.ibm.com/install/linux | sh

After installation you can verify that the CLI is successfully installed by running the following from the command line.

ibmcloud version

Sign into IBM Cloud, then select the user icon in the top-right corner, and click on the Log in to CLI and API option. A one time passcode is displayed.

IBM Cloud One time passcode

Copy the command for the IBM Cloud CLI to sign in on the command line and select your region when prompted.

ibmcloud login -a https://cloud.ibm.com -u passcode -p <xxxxxxxxx>

Generate an API Key to use with Terraform and save it to a local file.

ibmcloud iam api-key-create learn-terraform -d "Get started with Terraform and IBM Cloud"

Export the API Key to the IC_API_KEY environment variable. Terraform will use this environment variable to authenticate with IBM Cloud. You will find the API Key value in the output from the previous command.

export IC_API_KEY="<YOUR_API_KEY>"

Install the IBM Cloud Container Registry and Container Service plugins.

ibmcloud plugin install container-registry -r 'IBM Cloud'
ibmcloud plugin install container-service

Getting started with Terraform

Create a directory to hold our Terraform scripts and add a main.tf file to get started.

mkdir learn-terraform-ibmcloud
cd learn-terraform-ibmcloud/
touch main.tf
code .

We need to add the necessary instructions to our Terraform script:

  • Add the IBM Cloud Provider, which allows us to use IBM Cloud resources in our script. At the time of writing, the latest version is 1.42.0.
  • Create a Container Registry namespace by configuring the ibm_cr_namespace resource.
  • Create a Kubernetes Service cluster by configuring the ibm_container_cluster resource.
    • The "datacenter" argument is required, run ibmcloud ks zones --provider classic to see a list of possible values.

The complete main.tf is provided below.

# Configure the IBM Cloud provider
terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "~> 1.42.0"
}
}
}

provider "ibm" {
region = "eu-de"
}

# Configure the IBM Cloud Container Registry
resource "ibm_cr_namespace" "cr_ibm4free" {
name = "ibm4free"
}

# Configure the IBM Cloud Kubernetes Service
resource "ibm_container_cluster" "ks_ibm4free" {
name = "ibm4free"
datacenter = "mil01"
machine_type = "free"
hardware = "shared"
}

From the command line, initialize Terraform. This will install the required providers.

terraform init

Use the following command to format your file for consistency.

terraform fmt

Validate your file.

terraform validate

If validation passes, you are ready to create your resources. You can preview what will happen with terraform plan, or alternatively go ahead and create the resources. Answer "yes" when prompted.

terraform apply

This will take around 10min to complete deployment of your cluster. 
Verify that your resources were created successfully. 

ibmcloud cr namespaces
ibmcloud ks cluster ls

Create another file and call it variables.tf.

touch variables.tf

Define two variables in this file, one for the name of the Container Registry namespace and another for the Kubernetes Service cluster.

variable "cr_namespace_name" {
default = "ibm4free"
}

variable "ks_cluster_name" {
default = "ibm4free"
}

Update main.tf to use the variables defined in variables.tf.

resource "ibm_cr_namespace" "cr_ibm4free" {
  name =
var.cr_namespace_name
resource "ibm_container_cluster" "ks_ibm4free" {
  name =
var.ks_cluster_name

Apply the changes.

terraform apply

Terraform will detect that no changes to the infrastructure is required, since the resource names were only extracted to variables.

Terraform Apply

Congratulations! You have successfully created the 2 resources required to deploy the .NET 6.0 Web App from my previous post.
Delete the resources when you are done.

terraform destroy

Conclusion

In this blog post we explored the creation of resources in IBM Cloud using Terraform automation. Because the infrastructure is now declared in a file, it can become part of your source control, review process and deployment workflows, to name just a few benefits. When scripting infrastructure, the process becomes transparent and repeatable, which has contributed to the popularity of Infrastructure-as-Code (IaC).

Want to learn more? Check out some Terraform Tutorials.

References

#ibmcloud​​​#terraform #Kubernetes​​
0 comments
14 views

Permalink