This is a short example/cheat sheet about, how to use the new module called terraform-ibm-observability-instances to create your service instances on IBM Cloud with Terraform. You can find the source code for the example of the blog post in this GitHub repository Example to use the IBM Observability Module. In this example we plan to create an Activity Tracker, a Log Analysis, and a Monitoring service instance on IBM Cloud with Terraform.

Terraform helps to realize an automated Infrastructure As Code approach. If you want to get started with Terraform and IBM Cloud, you can use the Getting started with Terraform on IBM Cloud.
Content of the example GitHub project
The diagram shows simplified the Terraform (.tf
) files are organized in the example GitHub project.

A main element in Terraform
is a module
, which is a collection of .tf
and/or .tf.json
files kept together in a directory. The files do contain the HashiCorp Configuration Language (HCL)
In Terraform you can define as many files (.tf
) as you want. A configuration for Terraform has at least one module, that module is known as root module.
The following table contains the file names with the main language elements we are using in our example.
File name | Usage | Langage elements |
---|---|---|
provider.tf | Here we have defined only IBM Cloud as a provider. | Providers |
variables.tf | Here we have defined the names and the plans for our service instance on IBM Cloud and the basic IBM Cloud elements like region , resource group , and API key . | Variables and Outputs |
outputs.tf | Here we providing (in case of the reuse of that module) the access keys for our created IBM Cloud services. | Variables and Outputs |
main.tf | This file contains the usage of the included modules form IBM Cloud and it contains our terraform root module. | Module |
version.tf | Here we set constraints for the Terraform version and IBM Cloud provider version. | Version constraints |
plan-observability-environment.sh | Here we implement an automation of the Terraform usage. | bash scripting |
The image above shows the simplified dependencies of the Terraform files and we see that we are sourcing
(including) modules.
Here are the links to the modules from IBM Cloud:
- The terraform-ibm-observability-instances module. That module contains all submodules to create the service instances we want to create.
- The terraform-ibm-resource-group module. With this module we are building an own temporary module for
resource-group
to the the resource group id.
The bash script automates the creation of the needed environment variables for Terraform by sourcing the .env
file to set the right values. Inside the bash automation script we execute the commands terraform init
and terraform plan
. But, we don’t execute the terraform apply
command. Here is the content of the plan-observability-environment.sh
bash script:
#!/bin/bash
# **************** Global variables
source ./.env
export TF_VAR_ibmcloud_api_key=$IBMCLOUD_APIKEY
export TF_VAR_ibm_region=$REGION
export TF_VAR_resource_group=$RESOURCE_GROUP
echo "*********************************"
echo ""
echo "Initialize Terraform"
terraform init
echo ""
echo "Press any key"
read
# **************** plan ****************
echo "*********************************"
echo ""
echo "Generate a Terraform plan for the IBM Cloud resources"
terraform plan
Example setup
These are the steps you can follow along to verify the plan of the setup for an Activity Tracker, a Log Analysis, and a Monitoring service instance on IBM Cloud with Terraform.
Step 1: Clone the project to your local computer
git clone https://github.com/thomassuedbroecker/terraform-example-to-use-ibm-observability-instance.git
cd terraform-example-to-use-ibm-observability-instance/code
Step 2: Configure the environment variables
cat .env-template > .env
# used as 'environment' variables
RESOURCE_GROUP="default"
REGION="us-south"
IBMCLOUD_APIKEY="YOUR_IBMCLOUD_APIKEY"
Step 2: Execute bash script
In the bash script plan-observability-environment.sh
we use the definition TF_VAR_
for input variables to set the variables based on our .env
.
sh plan-observability-environment.sh
- Example output:
- Init
*********************************
Initialize Terraform
Initializing modules...
Downloading git::https://github.com/terraform-ibm-modules/terraform-ibm-observability-instances?ref=main for observability_instances...
- observability_instances in .terraform/modules/observability_instances
Downloading git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group?ref=main for resource_group...
- resource_group in .terraform/modules/resource_group
Initializing the backend...
Initializing provider plugins...
- Finding ibm-cloud/ibm versions matching ">= 1.38.1, >= 1.48.0"...
- Installing ibm-cloud/ibm v1.48.0...
- Installed ibm-cloud/ibm v1.48.0 (self-signed, key ID AAD3B791C49CC253)
...
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
Press any key
- Plan
*********************************
Generate a Terraform plan for the IBM Cloud resources
Terraform used the selected providers to generate the following execution plan.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
...
Plan: 6 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ activity_tracker_resource_key = (sensitive value)
+ logdna_resource_key = (sensitive value)
+ sysdig_resource_key = (sensitive value)
────────────────────────────────────────────────────────────────────────────────
Note: You didn't use the -out option to save this plan, so Terraform can't
guarantee to take exactly these actions if you run "terraform apply" now.
Here is one of my related blog posts to terraform usage: Use Terraform to create a VPC and a Kubernetes Cluster on IBM Cloud
I hope this was useful for you and let’s see what’s next?
Greetings,
Thomas
#Terrafrom, #ibmcloud, #bashscripting, #cheatsheet
Leave a Reply