Terraform with IBM Cloud Event Notifications

Pradeep Gopalgowda
AWS Tip
Published in
5 min readMar 1, 2022

The HashiCorp company develops a suite of open-source solutions specializing in cloud automation. In this blog, we are going to explore using Terraform with IBM Cloud. Terraform, part of the HashiCorp family, is an Infrastructure-as-Code product allowing users to automate deployment and manage cloud resources. Then, IBM Cloud is a cloud computing provider that offers a series of resources ranging from analytics services to infrastructure and more to clients. These resources include both Platform-as-a-Service (PaaS) and Infrastructure-as-a-Service (IaaS) solutions (https://cloud.ibm.com/catalog)

Working from a local computer, a user can install Terraform, create an IBM cloud account, and link the two via a plugin. The latest version of Terraform v1.1.6 which can found from the Terraform webpage (https://www.terraform.io/downloads.html) and used via the CLI. 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

Brief Terraform Background

While written in GO, you will notice Terraform has its own unique language/syntax, HashiCorp Congifuration Language (hcl), which is very similar to JSON. This makes it user friendly and easy to pick up.

Then, The backbone of Terraform is the “main.tf” (it can hold any name the user desires) file which contains the resources to be deployed. This central file dictates resources to be provisioned or destroyed.

Working with the IBM Cloud

To work with Terraform and the IBM Cloud, a plugin is required to extend the current functionality. Documentation to install and configure the IBM Cloud plugin can be found here: https://github.com/IBM-Cloud/terraform-provider-ibm. Still, I will take you step by step through the process from the command line. In this tutorial we will use IBM Cloud Provider 1.38.2. First, we will create a hidden directory from the home directory.

mkdir $HOME/.terraform.d/plugins

Next, we need to download and move the plugin to the directory we created previously (https://github.com/IBM-Cloud/terraform-provider-ibm/releases).

mv $HOME/Downloads/terraform-provider-ibm_v1.38.2 $HOME/.terraform.d/plugins/

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 !

Locate the IAM tab
Navigate to IBM Cloud API Keys
Create an IBM Cloud API Key

Resources to Deploy

In this demo we are going to create the IBM Cloud Event Notifications resource. All IBM specific documentation can be found here (https://cloud.ibm.com/docs)

Building Infrastructure as Code

There are a few key files that are associated with Terraform. They are illustrated in the directory pictured below.

First is the provider.tf file. This file highlights the cloud of choice for development, region, and how the user will connect.

terraform {
required_providers {
ibm = {
source = "IBM-Cloud/ibm"
version = "1.38.2"
}
}
}

Next is the configuration file (variables.tf) which stores passwords or any values that are required for Terraform at runtime.

variable "instance_id" {
type = string
default = "235b109c-0a2c-438b-8758-1a4b9db044db"
description = "Event Notifications service instanceId."
}

variable "destination_name" {
type = string
default = "Smart Home Android app"
description = "Destination name."
}

variable "destination_description" {
type = string
default = "Smart House device status"
description = "Description for the destination."
}

variable "webhook_type" {
default = "webhook"
type = string
description = "Type of the destination"
}

variable "webhook_verb" {
default = "POST"
type = string
description = "Type of verb fro the destination"
}

variable "webhook_URL" {
type = string
description = "URL of the webhook"
default = "https://hello.demo.com/hello"
}

The remaining files includes the bulk of details and is where the user declares the resources to be deployed.

The file (instance.tf) is used to provision IBM Cloud Event Notifications instance.

resource "ibm_resource_instance" "terraform_demo" {
plan = "lite"
location = "us-south"
name = "terraform_demo"
service = "event-notifications"
}

The file (destinations.tf) is used to create a destination by using IBM Cloud™ Event Notifications.

resource "ibm_en_destination" "destination1" {
type = var.webhook_type
name = var.destination_name
description = var.destination_description
instance_guid = ibm_resource_instance.terraform_demo.guid
config {
params {
url = var.webhook_URL
verb = var.webhook_verb
custom_headers = {
"authorization" = "secure"
}
sensitive_headers = ["authorization"]
}
}
}

The file (topics.tf) is used to create a topics by using IBM Cloud™ Event Notifications.

resource "ibm_en_topic" "topic1" {
instance_guid = ibm_resource_instance.terraform_demo.guid
description = "Check the status of front door"
name = "Front Door Status"
}

The file (subscriptions.tf) is used to create a subscriptions by using IBM Cloud™ Event Notifications.

resource "ibm_en_subscription" "subscription1" {
instance_guid = ibm_resource_instance.terraform_demo.guid
name = "Android app Subscription"
description = "Subscribe to Android app"
topic_id = ibm_en_topic.topic1.topic_id
destination_id = ibm_en_destination.destination1.destination_id
attributes {
add_notification_payload = true
signing_enabled = true
}
}

Execute our Terraform

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 subscription.

terraform init
terraform plan

Export your IBM Cloud API Key before running the terrform apply command.

export IBMCLOUD_API_KEY={Your IBM Cloud API Key}terraform apply

The resources can be validated within the IBM cloud by selecting the resource list.

Resources:

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

What are your thoughts?