Running Multiple Terraform Modules from the Same Blueprint

Terraform is a powerful product for deploying and managing a Cloud infrastructure which supports hundreds of different providers and services. Managing multiple modules and multiple versions of modules can get overwhelming without some type of a central system for managing deployment.

In this tutorial, we will discuss one way to do it simply with Cloudify.

For information on installing and configuring a Cloudify manager, please visit docs.cloudify.co.

There are two components when using Terraform in a Cloudify Blueprint. The first is a node template for managing the installation or location of the Terraform binary.

It looks like this:

terraform:
 type: cloudify.nodes.terraform

Configured like this, Cloudify will install one of the latest versions of Terraform in a special location on the Cloudify manager and use it for any Terraform module in this deployment.

The second component that’s needed is a node template for the Terraform module:

tf_module:
 type: cloudify.nodes.terraform.Module
 relationships:
   - target: terraform
     type: cloudify.terraform.relationships.run_on_host
 properties:
   resource_config:
     source:
       location: { get_input: location }
     source_path: { get_input: source_path }

This is a minimal Terraform module node template, and we will expand it in a little bit, but first let’s talk about what we have here.

We have the relationship cloudify.terraform.relationships.run_on_host, with our previous Terraform node template as a dependency. This ensures the use of the Terraform binary. 

The next thing to pay attention to is the resource_config property. Here we define a source and a source_path. The first component, source, refers to the URL or file path, where the Terraform template source code can be retrieved. The location key is the actual URL. Here you may also provide a username and password if your repository requires basic auth. The second component, source_path, refers to the file path inside of the source. So if you have a multi-level file structure, you’ll provide the path to the folder containing the main.tf file in the folder structure. In short, the path to the directory is where you would typically initialize Terraform.

Now you know how to deploy a Terraform template.

Now let’s discuss a simple way for you to deploy multiple modules from a single repository, or module versions of the same repository.

In the example above, we configured the module with the source path parameter:

source_path:
 type: string
 default: template/modules/public_vm

This example repository has a number of modules:

  • template/modules/azure/public_vm
  • template/modules/gcp/public_vm
  • template/modules/ibm
  • template/modules/openstack/public_vm
  • template/modules/public_vm (AWS)
  • template/modules/private_vm (AWS)

We can deploy any of these modules simply by toggling the value in the source_path parameter in our inputs.

A private VM:

source_path:
 type: string
 default: template/modules/private_vm

A GCP VM:

source_path:
 type: string
 default: template/modules/gcp/public_vm

Seems simple enough. However, maybe we want to manage different versions of these modules. We can do this by playing with the location parameter.

In our example above, we retrieved the source.location parameter from the location input. We could define that input like this:

location:
 type: string
 default: https://github.com/cloudify-community/tf-source/archive/refs/heads/main.zip

We can simply change the value of the URL:

location:
 type: string
 default: https://github.com/cloudify-community/tf-source/archive/refs/heads/main-2.zip

Or we can use a concatenation:

module_version:
 type: string
 default: main

location:
 type: string
 default: { concat: [ 'https://github.com/cloudify-community/tf-source/archive/refs/heads/', { get_input: module_version }, '.zip' ] }

Now we can simply toggle the value of the module_version parameter for this version of the Terraform module.

I hope this quick tutorial has been simple, informative, and fun for you!

Want to learn more? Schedule a quick demo here.

comments

    Leave a Reply

    Your email address will not be published.

    Back to top