Conductor Documentation

Terraform Plugin

The Terraform plugin enables you to maintain Terraform Plan state from Studio Conductor and also to use Terraform resources in your Studio Conductor blueprints.

Requirements

Node Types

cloudify.nodes.terraform

This is the base node type, which represents a Terraform installation.

Properties

Example

In the following example, we deploy a Terraform installation, the Terraform executable saved under the deployment directory:

  inputs:
    
    terraform_plugins:
      default:
        registry.terraform.io/hashicorp/azurerm/2.52.0/linux_amd64/: 'https://releases.hashicorp.com/terraform-provider-azurerm/2.52.0/terraform-provider-azurerm_2.52.0_linux_amd64.zip'

  node_templates:
    terraform:
      type: cloudify.nodes.terraform
      properties:
        resource_config:
          plugins: { get_input: terraform_plugins }

cloudify.nodes.terraform.Module

This refers to a Terraform module.

Properties

cloud_resources: type: cloudify.nodes.terraform.Module properties: resource_config: source: location: https://github.com/cloudify-community/blueprint-examples/archive/master.zip source_path: virtual-machine/resources/terraform/template variables: access_key: { get_secret: aws_access_key_id } secret_key: { get_secret: aws_secret_access_key } aws_region: { get_input: aws_region_name } aws_zone: { get_input: aws_zone_name } admin_user: { get_input: agent_user } admin_key_public: { get_attribute: [agent_key, public_key_export] } tflint_config: installation_source: https://github.com/terraform-linters/tflint/releases/download/v0.34.1/tflint_linux_amd64.zip config: - type_name: config option_value: module: “true” - type_name: plugin option_name: aws option_value: enabled: “true” - type_name: rule option_name: terraform_unused_declarations option_value: enabled: “true” relationships: - target: terraform type: cloudify.terraform.relationships.run_on_host

# Workflows

## refresh_terraform_resources

The refresh_terraform_resources workflow pulls the remote state and updates the `cloudify.nodes.terraform.Module` node instance `resources` runtime property with the remote state.

  * `node_instance_ids`: The IDs of `cloudify.nodes.terraform.Module` node_instances, which should have refresh run on them. (One of `node_instance_ids` or `node_ids` should be provided.)
  * `node_ids`: The IDs of `cloudify.nodes.terraform.Module` nodes, which should have refresh run on them. (One of `node_instance_ids` or `node_ids` should be provided.)

To execute refresh terraform resources workflow on node instances of a specific node template:

Example command:

bash [user@c540aa7d0efd /]# cfy executions start refresh_terraform_resources -d tf -p node_instance_ids=cloud_resources_j9l2y3 2021-10-10 16:24:32.278 CFY Starting ‘refresh_terraform_resources’ workflow execution Executing workflow refresh_terraform_resources on deployment tf [timeout=900 seconds]

## terraform_plan

The Terraform plan workflow enables to you run the Terraform plan command against your Terraform module and to store the results in the node instances' `plan` runtime property.

__NOTE: Remember that if your Terraform module depends on runtime data, then that data must exist. For example, if it requires a zip file created by a different node template, then the Terraform plan cannot run unless the zip node has already been installed. For this reason, the terraform_plan workflow is executed primarily for day two operations (after install).__

**Parameters**

  * `node_instance_ids`: The IDs of `cloudify.nodes.terraform.Module` node_instances, which should have plan run on them. (One of `node_instance_ids` or `node_ids` should be provided.)
  * `node_ids`: The IDs of `cloudify.nodes.terraform.Module` nodes, which should have plan run on them. (One of `node_instance_ids` or `node_ids` should be provided.)
  * `source`: URL or path to a ZIP/tar.gz file, or a Git repository to obtain new module source from. If omitted, then the module is reloaded from its last location. This is useful if the source contains changes that will impact the plan.
  * `source_path`: The path within the source property, where the terraform files may be found.  This is useful if the source path contains changes that will impact the plan.

Example command:

bash

list the node instances in a deployment:

[user@c540aa7d0efd /]# cfy node-inst list -d tf Listing instances for deployment tf…

Node-instances: +————————+—————+———+—————–+———+————+—————-+————+ | id | deployment_id | host_id | node_id | state | visibility | tenant_name | created_by | +————————+—————+———+—————–+———+————+—————-+————+ | agent_key_cp18tq | tf | | agent_key | started | tenant | default_tenant | admin | | cloud_resources_j9l2y3 | tf | | cloud_resources | started | tenant | default_tenant | admin | | terraform_p4e4zy | tf | | terraform | started | tenant | default_tenant | admin | +————————+—————+———+—————–+———+————+—————-+————+

Execute the workflow for the cloud resources node instance:

[user@c540aa7d0efd /]# cfy exec start terraform_plan -d tf -p node_instance_ids=cloud_resources_j9l2y3 Executing workflow terraform_plan on deployment tf [timeout=900 seconds] 2021-10-10 16:18:30.155 CFY Starting ‘terraform_plan’ workflow execution…

Execute the workflow for a new source path (different module in the same zip.

[user@c540aa7d0efd /]# cfy exec start terraform_plan -d tf -p node_instance_ids=cloud_resources_j9l2y3 -p source_path=template/modules/private_vm Executing workflow terraform_plan on deployment tf [timeout=900 seconds] 2021-10-10 16:21:03.689 CFY Starting ‘terraform_plan’ workflow execution

## reload_terraform_template

The reload_terraform_template workflow updates the remote state with new changes in `source` and/or `source_path`, or attempts resets the remote state to the original state if `source` or `source_path` are not provided.

  * `node_instance_ids`: The IDs of `cloudify.nodes.terraform.Module` node_instances, which should have reload run on them. (One of `node_instance_ids` or `node_ids` should be provided.)
  * `node_ids`: The IDs of `cloudify.nodes.terraform.Module` nodes, which should have reload run on them. (One of `node_instance_ids` or `node_ids` should be provided.)
  * `source`: URL or path to a ZIP/tar.gz file, or a Git repository to obtain new module source from. If omitted, then the module is reloaded from its last location.
  * `source_path`: The path within the source property, where the terraform files may be found.

To execute refresh terraform resources workflow on node instances of a specific node template:

Example command:

To execute terraform reload operation:

bash [user@c540aa7d0efd /]# cfy executions start reload_terraform_template -d tf -p node_instance_ids=cloud_resources_j9l2y3 -p source_path=template/modules/private_vm Executing workflow reload_terraform_template on deployment tf [timeout=900 seconds] 2021-10-10 16:30:34.523 CFY Starting ‘reload_terraform_template’ workflow execution

## Terraform Outputs

You can expose outputs from your Terraform template to the node instance runtime properties.

For example, you can expose a simple message by adding the outputs block to your main.tf:

output “foo” { value = “bar” }

You can also expose meaningful information like IP addresses, Subnets, and ports.

output “ip” { value = aws_instance.example_vm.id

This information will be stored during the install workflow, or the reload_terraform_template workflow.

[user@cloudify-manager ~]# cfy node-instances get cloud_resources_02mhg1 –json | jq -r ‘.runtime_properties.outputs’ { “foo”: { “sensitive”: false, “type”: “string”, “value”: “bar” } }

You can then use these outputs in blueprint, for example as deployment capabilities:

yaml capabilities: ip: value: { get_attribute: [ cloud_resources , outputs , ip , value ] } ```

NOTE: You must expose the output in the main terraform file in the source_path provided in your template or in your reload_terraform_template workflow parameters.