Sitemap

Bring your existing resources on IBM Cloud under Terraform management

2 min readOct 27, 2017

--

In my earlier post I discussed how you can leverage Terraform to manage your resources on IBM Cloud. You might ask -

I already have a gigantic infrastructure that I manage via my custom solution using scripts or otherwise. I didn’t use Terraform to start with. How can I continue using Terraform to manage those resources as well?

Terraform has a concept of importing the existing resources. Suppose you have created a VSI and want to bring it under Terraform management.

Steps to import using Terraform v0.10.8

Preparation

  1. Note the ID of the VSI. You could go to the SoftLayer portal to note the ID or use some scripts that you wrote to find all the IDs.
  2. Create a directory to store terraform configuration file.
  3. In the directory so created , create a file call it import_vm.tf with the following content

Actual Import

Get Ashish K Thakur’s stories in your inbox

Join Medium for free to get updates from this writer.

Issue terraform import command in the directory where you put import_vm.tf. You should replace 123456 with the actual ID of your VSI.

$ terraform import ibm_compute_vm_instance.my_vm 1234

Read the output carefully.

Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next `terraform plan` will mark
them for destruction.

You should edit the import_vm.tf file based on the output of the below command

$ terraform show

Eventually you should have an import_vm.tf file which is populated with various fields of that particular VM. Something like

Congratulations !! You have your VM under terraform management. The last step of populating the configuration file yourself by looking into the output of terraform show is a pain and is a known issue in the Hashicorp community.

As an exercise you can try to automate the last step by scripting it. This would require understanding the syntax of the terraform show. Any thing in output that ends with .# is a list/set and the ones ending with .% is a map.

There are few catches with this approach of parsing the result of terraform show. There are few fields which are computed (ex- ip_address_id)so you don’t need to put them in the configuration file. Of course you will know them when you actually run the terraform plan or terraform apply .

Another solution is to look into the schema and filter out those which are computed. This would require the provider code to be available during compilation step.

--

--