Deploy a Terraform IBM Module using Terraform CLI
In this tutorial, you will learn how to deploy a terraform-ibm-module (TIM) module using Terraform CLI. It will cover how variables are defined and passed, which CLI commands drive the process, and demonstrate the workflow with IBM Cloud Object Storage (COS) as an example.
Objectives
By the end of this tutorial, you will learn to deploy and manage resources on IBM Cloud by using a Terraform IBM Module.
While this tutorial uses the Cloud Object Storage module as an example, you can apply these techniques to any Terraform IBM Module.
Prerequisites
Before you get started, make sure you have:
- Terraform CLI installed.
- IBM Cloud apikey to access the IBM Cloud.
Analyze the code
The Terraform IBM module provides reusable infrastructure code for IBM Cloud resources. You can find the Cloud Object Storage module code in the terraform-ibm-cos repository.
The repository contains:
- Module source code: The core Terraform configuration files
- Examples directory: Reference implementations showing different use cases
- Documentation: Variable definitions, outputs, and usage guidelines
See Understanding the Terraform IBM Module Structure for more details.
Create your configuration
To deploy infrastructure using this module, you need to write your own Terraform configuration that references the module. Follow these steps:
Review the examples
Explore the examples/ directory to understand how the module can be used. Each example is a complete, runnable Terraform configuration. It demonstrates:
- Module configuration with specific parameters
- Required variable values
- Integration patterns with other resources
Write your code
Create a new Terraform project directory and write your own Terraform configuration file that invokes this module. For guidance, refer to the usage section.
Define variables
Once you have written your Terraform configuration, the next step is to set up your variables. The input variables are declared in variables.tf. You can set input variables by setting environment variables, passing them as command-line arguments, defining them directly in variables.tf or specifying them in terraform.tfvars.
This tutorial uses the terraform.tfvars file to provide values for the input variables. Create a new file named terraform.tfvars and add variables as shown below.
ibmcloud_api_key = "<your-api-key>"
prefix = "tf-demo" # you can choose a prefix of your choice
region = "us-south"
...
Review the input variables in your code and update terraform.tfvars to override any default values that don't align with your requirements.
Never commit this file if it contains your ibmcloud_api_key, as it exposes sensitive credentials. You can also set the value safely by using a TF_VAR_ibmcloud_api_key environment variable instead.
Initialize Terraform
The terraform init command initializes Terraform in the working directory. It reads the backend configuration from your files to download and install the required provider plugins and the modules.
terraform init
Generate execution plan
The terraform plan command generates an execution plan that provides a preview of the changes Terraform intends to make to your infrastructure based on your configuration files, allowing you to review and verify the proposed changes.
To generate this execution plan, run the following command:
terraform plan
Review the output of the plan and make any necessary changes to your configuration files before proceeding to the next step.
Deploy the resources
The terraform apply command deploys the resources specified in your configuration files to your IBM Cloud account. To deploy these resources, run the following command:
terraform apply
Approve the deployment when prompted, by typing yes. Alternatively, you may use terraform apply -auto-approve to automatically approve the changes.
If you confirm, Terraform starts provisioning all resources defined in the execution plan. This step may take some time depending on the configuration.
You can also save the execution plan in a file called tfplan by using terraform plan -out=tfplan and then run terraform apply tfplan to execute the infrastructure changes specified in the saved plan file.
Verify the deployed resources
After Terraform completes applying the configuration, verify that the resources have been successfully created in your IBM Cloud account. To verify if the resources are deployed properly,
- Go to the IBM Cloud dashboard.
- Navigate to Resource List in the left panel.
- Check under the appropriate category (Storage in this case), or you can search for the prefix used for the deployment.
The infrastructure has been deployed successfully, and the resources are now visible on the dashboard. You’re all set to start using them.
Clean-up the resources
When the resources deployed are no longer needed, you can clean up your environment by removing everything Terraform created. Cleaning up is important to avoid unnecessary charges for cloud resources and keeps your IBM Cloud account organized. Even small resources, like storage buckets, can accumulate costs over time. To delete the resources, run the following command:
terraform destroy
This displays a summary of the resources that will be removed. If you confirm, Terraform proceeds to delete all resources managed by the module.
Next steps
You have now completed the deployment of a Terraform module - Cloud Object Storage. You can now explore other modules to deploy more resources in your IBM Cloud account according to your business needs.