Terraform

Description

This plugin enables you to run Terraform against providers in a Vela pipeline.

Source Code: https://github.com/go-vela/vela-terraform

Registry: https://hub.docker.com/r/target/vela-terraform

Usage

NOTE:

Users should refrain from using latest as the tag for the Docker image.

It is recommended to use a semantically versioned tag instead.

Sample of adding installing terraform version:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: apply
      auto_approve: true # Required for versions of Terraform 0.12.x
      version: 0.11.7

Sample of adding init options to Terraform configuration:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: apply
      auto_approve: true # Required for versions of Terraform 0.12.x
      init_options:
        get_plugins: true

Sample of applying Terraform configuration:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: apply
      auto_approve: true # Required for versions of Terraform 0.12.x

Sample of destroying Terraform configuration:

steps:
  - name: destroy
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: destroy
      auto_approve: true # Required for versions of Terraform 0.12.x

Sample of formatting Terraform configuration files:

steps:
  - name: fmt
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: fmt

Sample of planning Terraform configuration:

steps:
  - name: plan
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: plan

Sample of validating Terraform configuration:

steps:
  - name: validate
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: validate

Secrets

NOTE: Users should refrain from configuring sensitive information in your pipeline in plain text.

Internal

Users can use Vela internal secrets to substitute these sensitive values at runtime:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
+   secrets: [ terraform_username, terraform_password ]
    parameters:
      action: apply
      auto_approve: true # Required for versions of Terraform 0.12.x
-     username: octocat
-     password: superSecretPassword

This example will add the secrets to the apply step as environment variables:

  • TERRAFORM_USERNAME=<value>
  • TERRAFORM_PASSWORD=<value>

External

The plugin accepts the following files for authentication:

ParameterVolume Configuration
password/vela/parameters/terraform/password, /vela/secrets/terraform/password
username/vela/parameters/terraform/username, /vela/secrets/terraform/username

Users can use Vela external secrets to substitute these sensitive values at runtime:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: apply
      auto_approve: true # Required for versions of Terraform 0.12.x
-     username: octocat
-     password: superSecretPassword

This example will read the secret values in the volume stored at /vela/secrets/

Parameters

NOTE:

The plugin supports reading all parameters via environment variables or files.

Any values set from a file take precedence over values set from the environment.

Terraform commands will be invoked in the current directory by default.

The following parameters are used to configure the image:

NameDescriptionRequiredDefaultEnvironment Variables
actionaction to perform with TerraformtrueN/APARAMETER_ACTION
TERRAFORM_ACTION
init_optionsoptions to use for Terraform init operationfalseN/APARAMETER_INIT_OPTIONS
TERRAFORM_INIT_OPTIONS
log_levelset the log level for the plugintrueinfoPARAMETER_LOG_LEVEL
TERRAFORM_LOG_LEVEL
machinenetrc machine name to communicate withtruegithub.comPARAMETER_MACHINE
TERRAFORM_MACHINE
VELA_NETRC_MACHINE
passwordnetrc password for authenticationtrueset by VelaPARAMETER_PASSWORD
TERRAFORM_PASSWORD
VELA_NETRC_PASSWORD
usernamenetrc user name for authenticationtrueset by VelaPARAMETER_USERNAME
TERRAFORM_USERNAME
VELA_NETRC_USERNAME
versionset the Terraform CLI versiontrue1.2.7PARAMETER_VERSION
TERRAFORM_VERSION

The following parameters can be used within the init_options to configure the image:

NameDescriptionRequiredDefault
backendconfigure the backend for this configurationtrueN/A
backend_configsthis is merged with what is in the configuration filetrueN/A
force_copysuppress prompts about copying state datatrueN/A
from_modulecopy the contents of the given module into the target directory before initializationtrueN/A
getdownload any modules for this configurationtrueN/A
get_pluginsdownload any missing plugins for this configurationtrueN/A
inputask for input for variables if not directly settrueN/A
locklock the state file when locking is supportedfalseN/A
lock_timeoutduration to retry a state lockfalseN/A
no_colordisables colors in outputfalseN/A
plugin_dirsdirectory containing plugin binaries; overrides all default search paths for pluginsfalseN/A
reconfigurereconfigure the backend, ignoring any saved configurationfalseN/A
upgradeinstall the latest version allowed within configured constraintsfalseN/A
verify_pluginsverify the authenticity and integrity of automatically downloaded pluginsfalseN/A

Apply

The following parameters are used to configure the apply action:

Command uses Terraform CLI command defaults if not overridden in config.

NameDescriptionRequiredDefaultEnvironment Variables
auto_approveskip interactive approval of applying resourcesfalsefalsePARAMETER_AUTO_APPROVE
TERRAFORM_AUTO_APPROVE
backuppath to backup the existing state filefalseN/APARAMETER_BACKUP
TERRAFORM_BACKUP
directorythe directory containing Terraform files to applyfalse.PARAMETER_DIRECTORY
TERRAFORM_DIRECTORY
locklock the state file when locking is supportedfalsefalsePARAMETER_LOCK
TERRAFORM_LOCK
lock_timeoutduration to retry a state lockfalseN/APARAMETER_LOCK_TIMEOUT
TERRAFORM_LOCK_TIMEOUT
no_colordisables colors in outputfalsefalsePARAMETER_NO_COLOR
TERRAFORM_NO_COLOR
parallelismnumber of concurrent operations as Terraform walks its graphfalseN/APARAMETER_PARALLELISM
TERRAFORM_PARALLELISM
refreshupdate state prior to checking for differencesfalsefalsePARAMETER_REFRESH
TERRAFORM_REFRESH
statepath to read and save statefalseN/APARAMETER_STATE
TERRAFORM_STATE
state_outpath to write updated state filefalseN/APARAMETER_STATE_OUT
TERRAFORM_STATE_OUT
targetresource to targetfalseN/APARAMETER_TARGET
TERRAFORM_TARGET
varsa map of variables to pass to the Terraform (<key>=<value>)falseN/APARAMETER_VARS
TERRAFORM_VARS
var_filesa list of var files to usefalseN/APARAMETER_VAR_FILES
TERRAFORM_VAR_FILES

Destroy

The following parameters are used to configure the destroy action:

Command uses Terraform CLI command defaults if not overridden in config.

NameDescriptionRequiredDefaultEnvironment Variables
auto_approveskip interactive approval of destroying resourcesfalsefalsePARAMETER_AUTO_APPROVE
TERRAFORM_AUTO_APPROVE
backuppath to backup the existing state filefalseN/APARAMETER_BACKUP
TERRAFORM_BACKUP
directorythe directory containing Terraform files to destroyfalse.PARAMETER_DIRECTORY
TERRAFORM_DIRECTORY
locklock the state file when locking is supportedfalsefalsePARAMETER_LOCK
TERRAFORM_LOCK
lock_timeoutduration to retry a state lockfalseN/APARAMETER_LOCK_TIMEOUT
TERRAFORM_LOCK_TIMEOUT
no_colordisables colors in outputfalsefalsePARAMETER_NO_COLOR
TERRAFORM_NO_COLOR
parallelismnumber of concurrent operations as Terraform walks its graphfalseN/APARAMETER_PARALLELISM
TERRAFORM_PARALLELISM
refreshupdate state prior to checking for differencesfalsefalsePARAMETER_REFRESH
TERRAFORM_REFRESH
statepath to read and save statefalseN/APARAMETER_STATE
TERRAFORM_STATE
state_outpath to write updated state filefalseN/APARAMETER_STATE_OUT
TERRAFORM_STATE_OUT
targetresource to targetfalseN/APARAMETER_TARGET
TERRAFORM_TARGET
varsa map of variables to pass to the Terraform (<key>=<value>)falseN/APARAMETER_VARS
TERRAFORM_VARS
var_filesa list of var files to usefalseN/APARAMETER_VAR_FILES
TERRAFORM_VAR_FILES

Format

The following parameters are used to configure the fmt action:

Command uses Terraform CLI command defaults if not overridden in config.

NameDescriptionRequiredDefaultEnvironment Variables
checkvalidate if the input is formattedfalsefalsePARAMETER_CHECK
TERRAFORM_CHECK
diffdiffs of formatting changesfalsefalsePARAMETER_DIFF
TERRAFORM_DIFF
directorythe directory containing Terraform files to formatfalse.PARAMETER_DIRECTORY
TERRAFORM_DIRECTORY
listlist files whose formatting differsfalsefalsePARAMETER_LIST
TERRAFORM_LIST
writewrite result to source file instead of STDOUTfalsefalsePARAMETER_WRITE
TERRAFORM_WRITE

Plan

The following parameters are used to configure the plan action:

Command uses Terraform CLI command defaults if not overridden in config.

NameDescriptionRequiredDefaultEnvironment Variables
destroydestroy all resources managed by the given configuration and statefalsefalsePARAMETER_DESTROY
TERRAFORM_DESTROY
detailed_exit_codereturn detailed exit codes when the command exitsfalsefalsePARAMETER_DETAILED_EXIT_CODE
TERRAFORM_DETAILED_EXIT_CODE
directorythe directory containing Terraform files to planfalse.PARAMETER_DIRECTORY
TERRAFORM_DIRECTORY
inputask for input for variables if not directly setfalsefalsePARAMETER_INPUT
TERRAFORM_INPUT
locklock the state file when locking is supportedfalsefalsePARAMETER_LOCK
TERRAFORM_LOCK
lock_timeoutduration to retry a state lockfalseN/APARAMETER_LOCK_TIMEOUT
TERRAFORM_LOCK_TIMEOUT
module_depthspecifies the depth of modules to show in the outputfalseN/APARAMETER_MODULE_DEPTH
TERRAFORM_MODULE_DEPTH
no_colordisables colors in outputfalsefalsePARAMETER_NO_COLOR
TERRAFORM_NO_COLOR
parallelismnumber of concurrent operations as Terraform walks its graphfalseN/APARAMETER_PARALLELISM
TERRAFORM_PARALLELISM
refreshupdate state prior to checking for differencesfalsefalsePARAMETER_REFRESH
TERRAFORM_REFRESH
statepath to read and save statefalseN/APARAMETER_STATE
TERRAFORM_STATE
targetresource to targetfalseN/APARAMETER_TARGET
TERRAFORM_TARGET
varsa map of variables to pass to the Terraform (<key>=<value>)falseN/APARAMETER_VARS
TERRAFORM_VARS
var_filesa list of var files to usefalseN/APARAMETER_VAR_FILES
TERRAFORM_VAR_FILES

Validate

The following parameters are used to configure the validate action:

Command uses Terraform CLI command defaults if not overridden in config.

NameDescriptionRequiredDefaultEnvironment Variables
check_variablescommand will check whether all required variables have been specifiedfalsefalsePARAMETER_CHECK_VARIABLES
TERRAFORM_CHECK_VARIABLES
directorythe directory containing Terraform files to validatefalse.PARAMETER_DIRECTORY
TERRAFORM_DIRECTORY
no_colordisables colors in outputfalsefalsePARAMETER_NO_COLOR
TERRAFORM_NO_COLOR
varsa map of variables to pass to the Terraform (<key>=<value>)falseN/APARAMETER_VARS
TERRAFORM_VARS
var_filesa list of var files to usefalseN/APARAMETER_VAR_FILES
TERRAFORM_VAR_FILES

Template

COMING SOON!

Troubleshooting

You can start troubleshooting this plugin by tuning the level of logs being displayed:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
    parameters:
      action: apply
      auto_approve: true
+     log_level: trace

You can also instruct the Terraform CLI to output verbose logging:

steps:
  - name: apply
    image: target/vela-terraform:latest
    pull: always
+   environment:
+     TF_LOG: TRACE
    parameters:
      action: apply
      auto_approve: true

Below are a list of common problems and how to solve them: