Forge Home

terraform

A task to generate Bolt inventory from Terraform statefiles

64,814 downloads

3,733 latest version

5.0 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 0.7.1 (latest)
  • 0.7.0
  • 0.6.1
  • 0.5.0
  • 0.4.0
  • 0.3.0
  • 0.2.0
  • 0.1.0
released Nov 27th 2023
This version is compatible with:
  • Puppet Enterprise 2023.8.x, 2023.7.x, 2023.6.x, 2023.5.x, 2023.4.x, 2023.3.x, 2023.2.x, 2023.1.x, 2023.0.x, 2021.7.x, 2021.6.x, 2021.5.x, 2021.4.x, 2021.3.x, 2021.2.x, 2021.1.x, 2021.0.x, 2019.8.x
  • Puppet >= 6.27.0 < 9.0.0
  • , , , , , , , , ,
Tasks:
  • apply
  • destroy
  • initialize
  • output
  • refresh
Plans:
  • apply
  • destroy
  • refresh

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'puppetlabs-terraform', '0.7.1'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add puppetlabs-terraform
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install puppetlabs-terraform --version 0.7.1

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Download

Documentation

puppetlabs/terraform — version 0.7.1 Nov 27th 2023

terraform

Table of Contents

  1. Description
  2. Requirements
  3. Usage

Description

The Terraform plugin module supports looking up target objects from a Terraform state file, applying, destroying and querying outputs from Terraform project directories.

Requirements

You will need to have installed Terraform on the system you wish to run bolt from. The executable must be on the system $PATH.

Inventory plugin usage

The resolve_reference task supports looking up target objects from a Terraform state file. It accepts several fields:

  • dir: The directory containing either a local Terraform state file or Terraform configuration to read remote state from. Relative to the active Boltdir unless absolute path is specified.
  • resource_type: The Terraform resources to match, as a regular expression.
  • state: (Optional) The name of the local Terraform state file to load, relative to dir (defaults to terraform.tfstate).
  • backend: (Optional) The type of backend to load the state form, either remote or local (defaults to local).
  • target_mapping: A hash of target attributes to populate with resource values (e.g. target_mapping: { name: 'id' }).

The target_mapping parameter requires either a uri or name field. If only uri is set, the value of uri is used as the name.

Examples

groups:
  - name: cloud-webs
    targets:
      - _plugin: terraform
        dir: /path/to/terraform/project1
        resource_type: google_compute_instance.web
        target_mapping:
          uri: network_interface.0.access_config.0.nat_ip
      - _plugin: terraform
        dir: /path/to/terraform/project2
        resource_type: aws_instance.web
        target_mapping:
          uri: public_ip

Multiple resources with the same name are identified as .0, .1, etc.

The path to nested properties must be separated with .: for example, network_interface.0.access_config.0.nat_ip.

For example, the following truncated output creates two targets, named 34.83.150.52 and 34.83.16.240. These targets are created by matching the resources google_compute_instance.web.0 and google_compute_instance.web.1. The uri for each target is the value of their network_interface.0.access_config.0.nat_ip property, which corresponds to the externally routable IP address in Google Cloud.

google_compute_instance.web.0:
  id = web-0
  cpu_platform = Intel Broadwell
  machine_type = f1-micro
  name = web-0
  network_interface.# = 1
  network_interface.0.access_config.# = 1
  network_interface.0.access_config.0.assigned_nat_ip =
  network_interface.0.access_config.0.nat_ip = 34.83.150.52
  network_interface.0.address =
  network_interface.0.name = nic0
  network_interface.0.network = https://www.googleapis.com/compute/v1/projects/cloud-app1/global/networks/default
  network_interface.0.network_ip = 10.138.0.22
  project = cloud-app1
  self_link = https://www.googleapis.com/compute/v1/projects/cloud-app1/zones/us-west1-a/instances/web-0
  zone = us-west1-a
google_compute_instance.web.1:
  id = web-1
  cpu_platform = Intel Broadwell
  machine_type = f1-micro
  name = web-1
  network_interface.# = 1
  network_interface.0.access_config.# = 1
  network_interface.0.access_config.0.assigned_nat_ip =
  network_interface.0.access_config.0.nat_ip = 34.83.16.240
  network_interface.0.address =
  network_interface.0.name = nic0
  network_interface.0.network = https://www.googleapis.com/compute/v1/projects/cloud-app1/global/networks/default
  network_interface.0.network_ip = 10.138.0.21
  project = cloud-app1
  self_link = https://www.googleapis.com/compute/v1/projects/cloud-app1/zones/us-west1-a/instances/web-1
  zone = us-west1-a
google_compute_instance.app.1:
  id = app-1
  cpu_platform = Intel Broadwell
  machine_type = f1-micro
  name = app-1
  network_interface.# = 1
  network_interface.0.access_config.# = 1
  network_interface.0.access_config.0.assigned_nat_ip =
  network_interface.0.access_config.0.nat_ip = 35.197.93.137
  network_interface.0.address =
  network_interface.0.name = nic0
  network_interface.0.network = https://www.googleapis.com/compute/v1/projects/cloud-app1/global/networks/default
  network_interface.0.network_ip = 10.138.0.23
  project = cloud-app1
  self_link = https://www.googleapis.com/compute/v1/projects/cloud-app1/zones/us-west1-a/instances/app-1
  zone = us-west1-a

Setting up Terraform project directories

The initialize task will setup a Terraform project directory with all the appropriate modules and providers needed to execute your configuration. It accepts a single field:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.

Provisioning resources

The apply task will apply resources and return the logs printed to stdout. It accepts several fields:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.
  • state: (Optional) Path to read and save state. Defaults to terraform.tfstate. Path is relative to dir.
  • state_out: (Optional) Path to write state to that is different than state. This can be used to preserve the old state. Path is relative to dir.
  • target: (Optional) Resource to target. Operation will be limited to this resource and its dependencies. Accepts a single resource string or an array of resources.
  • var: (Optional) Set Terraform variables, expects a hash with key value pairs representing variables and values (NOTE: single quotes ' are incompatible).
  • var_file: (Optional) Set variables in the Terraform configuration from a file. Path is relative to dir.

The apply plan will run the apply task against the localhost target and optionally return the result of the output task. It accepts several fields:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.
  • state: (Optional) Path to read and save state. Defaults to terraform.tfstate. Path is relative to dir.
  • state_out: (Optional) Path to write state to that is different than state. This can be used to preserve the old state. Path is relative to dir.
  • target: (Optional) Resource to target. Operation will be limited to this resource and its dependencies. Accepts a single resource string or an array of resources.
  • var: (Optional) Set Terraform variables, expects a hash with key value pairs representing variables and values (NOTE: single quotes ' are incompatible).
  • var_file: (Optional) Set variables in the Terraform configuration from a file. Path is relative to dir.
  • return_output: (Optional) Return the result of the output task (defualts to false).

The output task will return the result of executing terraform output. It accepts several fields:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.
  • state: (Optional) Path to read and save state. Defaults to terraform.tfstate. Path is relative to dir.

Destroying resources

The destroy task will destroy resources and return the logs printed to stdout. It accepts several fields:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.
  • state: (Optional) Path to read and save state. Defaults to terraform.tfstate. Path is relative to dir.
  • state_out: (Optional) Path to write state to that is different than state. This can be used to preserve the old state. Path is relative to dir.
  • target: (Optional) Resource to target. Operation will be limited to this resource and its dependencies. Accepts a single resource string or an array of resources.
  • var: (Optional) Set Terraform variables, expects a hash with key value pairs representing variables and values (NOTE: single quotes ' are incompatible).
  • var_file: (Optional) Set variables in the Terraform configuration from a file. Path is relative to dir.

The destroy plan will run the destroy task against the localhost and return it's result. It accepts several fields:

  • dir: (Optional) Path to Terraform project directory. Path is relative to CWD, unless an absolute path is specified.
  • state: (Optional) Path to read and save state. Defaults to terraform.tfstate. Path is relative to dir.
  • state_out: (Optional) Path to write state to that is different than state. This can be used to preserve the old state. Path is relative to dir.
  • target: (Optional) Resource to target. Operation will be limited to this resource and its dependencies. Accepts a single resource string or an array of resources.
  • var: (Optional) Set Terraform variables, expects a hash with key value pairs representing variables and values (NOTE: single quotes ' are incompatible).
  • var_file: (Optional) Set variables in the Terraform configuration from a file. Path is relative to dir.

Example

In this example plan, resources are applied and then destroyed during plan execution. The outputs from the terraform::apply plan are used to pass as data to a task.

plan example(TargetSpec $targets){
  run_task('terraform::initialize', 'dir' => '/home/cas/working_dir/dynamic-inventory-demo')
  $apply_result = run_plan('terraform::apply', 'dir' => '/home/cas/working_dir/dynamic-inventory-demo', 'return_output' => true)
  run_task('important::stuff', $targets, 'task_var' => $apply_result)
  run_plan('destroy', 'dir' => '/home/cas/working_dir/dynamic-inventory-demo')
}