@c6fc/terraform
v1.3.0
Published
Download/Use Terraform via Node
Downloads
327
Readme
@c6fc/terraform
Link and use the Terraform binary with NPM.
Usage
npm install -p @c6fc/terraformSet the tf_version config element in your package.json:
"config": {
"tf_version": "0.15.4"
}const terraform = require('@c6fc/terraform');
// Importing does nothing on its own -- no disk reads, no download. exec()
// installs the binary on first use, once however many callers ask for it.
await terraform.exec(['apply'], { cwd: './render', stdio: 'inherit' });That is the whole API for running Terraform. You do not need to await
anything first; exec does it for you.
Spawning the binary yourself
isReady exists for the one case exec doesn't cover — when you want the
executable and intend to run it your own way. Reading it installs the binary if
it isn't cached, and resolves once it's on disk:
const { spawn } = require('child_process');
await terraform.isReady;
spawn(terraform.executablePath, ['plan'], { stdio: 'inherit' });This package's own bin/index.js is exactly this. Reading executablePath
without awaiting isReady first gives you a path that may not exist yet.
Laziness
cacheDir, version and executablePath are accessors: reading any of them
resolves tf_version from the nearest package.json, and reading isReady — or
calling exec — downloads and hash-verifies the binary if it isn't cached.
Destructuring the module (const { isReady } = require('@c6fc/terraform'))
therefore starts the download immediately; reach through the module object
instead to stay lazy.
Platforms
Linux, on arm, arm64, x32 and x64. Anything else throws when the binary is
first needed, naming itself and what is supported, rather than at import.
Exit codes
exec() exits the process with Terraform's own status when Terraform fails, and
with 1 when it could not be started at all — a missing binary, or a cwd that
does not exist. spawnSync reports status: null in that case, which used to be
passed straight to process.exit(), where null means success.
I created this package specifically for use with Sonnetry (@c6fc/sonnetry), but perhaps someone else will find it useful.
