terraform

Modules are deprecated and will be removed in version 0.14. Please use action-based configuration instead. See the 0.12 to Bonsai migration guide for details.

Description

Resolves a Terraform stack and either applies it automatically (if autoApply: true) or warns when the stack resources are not up-to-date.

Note: It is not recommended to set autoApply to true for any production or shared environments, since this may result in accidental or conflicting changes to the stack. Instead, it is recommended to manually plan and apply using the provided plugin commands. Run garden plugins terraform for details.

Stack outputs are made available as service outputs, that can be referenced by other modules under ${runtime.services.<module-name>.outputs.<key>}. You can template in those values as e.g. command arguments or environment variables for other services.

Note that you can also declare a Terraform root in the terraform provider configuration by setting the initRoot parameter. This may be preferable if you need the outputs of the Terraform stack to be available to other provider configurations, e.g. if you spin up an environment with the Terraform provider, and then use outputs from that to configure another provider or other modules via ${providers.terraform.outputs.<key>} template strings.

See the Terraform guide for a high-level introduction to the terraform provider.

Below is the full schema reference. For an introduction to configuring Garden modules, please look at our Configuration guide.

The first section contains the complete YAML schema, and the second section describes each schema key.

terraform modules also export values that are available in template strings. See the Outputs section below for details.

Complete YAML Schema

The values in the schema below are the default values.

kind: Module

# The type of this module.
type:

# The name of this module.
name:

# Specify how to build the module. Note that plugins may define additional keys on this object.
build:
  # A list of modules that must be built before this module is built.
  dependencies:
    - # Module name to build ahead of this module.
      name:

      # Specify one or more files or directories to copy from the built dependency to this module.
      copy:
        - # POSIX-style path or filename of the directory or file(s) to copy to the target.
          source:

          # POSIX-style path or filename to copy the directory or file(s), relative to the build directory.
          # Defaults to the same as source path.
          target:

  # Maximum time in seconds to wait for build to finish.
  timeout: 600

# If set to true, Garden will run the build command, services, tests, and tasks in the module source directory,
# instead of in the Garden build directory (under .garden/build/<module-name>).
#
# Garden will therefore not stage the build for local modules. This means that include/exclude filters
# and ignore files are not applied to local modules, except to calculate the module/action versions.
#
# If you use use `build.dependencies[].copy` for one or more build dependencies of this module, the copied files
# will be copied to the module source directory (instead of the build directory, as is the default case when
# `local = false`).
#
# Note: This maps to the `buildAtSource` option in this module's generated Build action (if any).
local: false

# A description of the module.
description:

# Set this to `true` to disable the module. You can use this with conditional template strings to disable modules
# based on, for example, the current environment or other variables (e.g. `disabled: ${environment.name == "prod"}`).
# This can be handy when you only need certain modules for specific environments, e.g. only for development.
#
# Disabling a module means that any services, tasks and tests contained in it will not be build, deployed or run.
#
# If you disable the module, and its services, tasks or tests are referenced as _runtime_ dependencies, Garden will
# automatically ignore those dependency declarations. Note however that template strings referencing the module's
# service or task outputs (i.e. runtime outputs) will fail to resolve when the module is disabled, so you need to make
# sure to provide alternate values for those if you're using them, using conditional expressions.
disabled: false

# Specify a list of POSIX-style paths or globs that should be regarded as the source files for this module. Files that
# do *not* match these paths or globs are excluded when computing the version of the module, when responding to
# filesystem watch events, and when staging builds.
#
# Note that you can also _exclude_ files using the `exclude` field or by placing `.gardenignore` files in your source
# tree, which use the same format as `.gitignore` files. See the [Configuration Files
# guide](https://docs.garden.io/using-garden/configuration-overview#including-excluding-files-and-directories) for
# details.
#
# Also note that specifying an empty list here means _no sources_ should be included.
include:

# Specify a list of POSIX-style paths or glob patterns that should be excluded from the module. Files that match these
# paths or globs are excluded when computing the version of the module, when responding to filesystem watch events,
# and when staging builds.
#
# Note that you can also explicitly _include_ files using the `include` field. If you also specify the `include`
# field, the files/patterns specified here are filtered from the files matched by `include`. See the [Configuration
# Files guide](https://docs.garden.io/using-garden/configuration-overview#including-excluding-files-and-directories)
# for details.
#
# Unlike the `scan.exclude` field in the project config, the filters here have _no effect_ on which files and
# directories are watched for changes. Use the project `scan.exclude` field to affect those, if you have large
# directories that should not be watched for changes.
exclude:

# A remote repository URL. Currently only supports git servers. Must contain a hash suffix pointing to a specific
# branch or tag, with the format: <git remote url>#<branch|tag>
#
# Garden will import the repository source code into this module, but read the module's config from the local
# garden.yml file.
repositoryUrl:

# When false, disables pushing this module to remote registries via the publish command.
allowPublish: true

# A list of files to write to the module directory when resolving this module. This is useful to automatically
# generate (and template) any supporting files needed for the module.
generateFiles:
  - # POSIX-style filename to read the source file contents from, relative to the path of the module (or the
    # ConfigTemplate configuration file if one is being applied).
    # This file may contain template strings, much like any other field in the configuration.
    sourcePath:

    # POSIX-style filename to write the resolved file contents to, relative to the path of the module source directory
    # (for remote modules this means the root of the module repository, otherwise the directory of the module
    # configuration).
    #
    # Note that any existing file with the same name will be overwritten. If the path contains one or more
    # directories, they will be automatically created if missing.
    targetPath:

    # By default, Garden will attempt to resolve any Garden template strings in source files. Set this to false to
    # skip resolving template strings. Note that this does not apply when setting the `value` field, since that's
    # resolved earlier when parsing the configuration.
    resolveTemplates: true

    # The desired file contents as a string.
    value:

# A map of variables to use when applying the stack. You can define these here or you can place a
# `terraform.tfvars` file in the working directory root.
#
# If you specified `variables` in the `terraform` provider config, those will be included but the variables
# specified here take precedence.
variables:

# Specify a path (relative to the module root) to a file containing variables, that we apply on top of the
# module-level `variables` field.
#
# The format of the files is determined by the configured file's extension:
#
# * `.yaml`/`.yml` - YAML. The file must consist of a YAML document, which must be a map (dictionary). Keys may
# contain any value type. YAML format is used by default.
# * `.env` - Standard "dotenv" format, as defined by [dotenv](https://github.com/motdotla/dotenv#rules).
# * `.json` - JSON. Must contain a single JSON _object_ (not an array).
#
# _NOTE: The default varfile format was changed to YAML in Garden v0.13, since YAML allows for definition of nested
# objects and arrays._
#
# To use different module-level varfiles in different environments, you can template in the environment name
# to the varfile name, e.g. `varfile: "my-module.${environment.name}.env` (this assumes that the corresponding
# varfiles exist).
varfile:

# The names of any services that this service depends on at runtime, and the names of any tasks that should be
# executed before this service is deployed.
dependencies: []

# If set to true, Garden will run `terraform destroy` on the stack when calling `garden delete namespace` or `garden
# delete deploy <deploy name>`.
allowDestroy: false

# If set to true, Garden will automatically run `terraform apply -auto-approve` when the stack is not
# up-to-date. Otherwise, a warning is logged if the stack is out-of-date, and an error thrown if it is missing
# entirely.
#
# **NOTE: This is not recommended for production, or shared environments in general!**
#
# Defaults to the value set in the provider config.
autoApply: null

# Specify the path to the working directory root—i.e. where your Terraform files are—relative to the config directory.
root: .

# The version of Terraform to use. Defaults to the version set in the provider config.
# Set to `null` to use whichever version of `terraform` that is on your PATH.
version:

# Use the specified Terraform workspace.
workspace:

Configuration Keys

kind

Type
Allowed Values
Default
Required

string

"Module"

"Module"

Yes

type

The type of this module.

Type
Required

string

Yes

Example:

type: "container"

name

The name of this module.

Type
Required

string

Yes

Example:

name: "my-sweet-module"

build

Specify how to build the module. Note that plugins may define additional keys on this object.

Type
Default
Required

object

{"dependencies":[]}

No

build.dependencies[]

build > dependencies

A list of modules that must be built before this module is built.

Type
Default
Required

array[object]

[]

No

Example:

build:
  ...
  dependencies:
    - name: some-other-module-name

build.dependencies[].name

build > dependencies > name

Module name to build ahead of this module.

Type
Required

string

Yes

build.dependencies[].copy[]

build > dependencies > copy

Specify one or more files or directories to copy from the built dependency to this module.

Type
Default
Required

array[object]

[]

No

build.dependencies[].copy[].source

build > dependencies > copy > source

POSIX-style path or filename of the directory or file(s) to copy to the target.

Type
Required

posixPath

Yes

build.dependencies[].copy[].target

build > dependencies > copy > target

POSIX-style path or filename to copy the directory or file(s), relative to the build directory. Defaults to the same as source path.

Type
Required

posixPath

No

build.timeout

build > timeout

Maximum time in seconds to wait for build to finish.

Type
Default
Required

number

600

No

local

If set to true, Garden will run the build command, services, tests, and tasks in the module source directory, instead of in the Garden build directory (under .garden/build/).

Garden will therefore not stage the build for local modules. This means that include/exclude filters and ignore files are not applied to local modules, except to calculate the module/action versions.

If you use use build.dependencies[].copy for one or more build dependencies of this module, the copied files will be copied to the module source directory (instead of the build directory, as is the default case when local = false).

Note: This maps to the buildAtSource option in this module's generated Build action (if any).

Type
Default
Required

boolean

false

No

description

A description of the module.

Type
Required

string

No

disabled

Set this to true to disable the module. You can use this with conditional template strings to disable modules based on, for example, the current environment or other variables (e.g. disabled: ${environment.name == "prod"}). This can be handy when you only need certain modules for specific environments, e.g. only for development.

Disabling a module means that any services, tasks and tests contained in it will not be build, deployed or run.

If you disable the module, and its services, tasks or tests are referenced as runtime dependencies, Garden will automatically ignore those dependency declarations. Note however that template strings referencing the module's service or task outputs (i.e. runtime outputs) will fail to resolve when the module is disabled, so you need to make sure to provide alternate values for those if you're using them, using conditional expressions.

Type
Default
Required

boolean

false

No

include[]

Specify a list of POSIX-style paths or globs that should be regarded as the source files for this module. Files that do not match these paths or globs are excluded when computing the version of the module, when responding to filesystem watch events, and when staging builds.

Note that you can also exclude files using the exclude field or by placing .gardenignore files in your source tree, which use the same format as .gitignore files. See the Configuration Files guide for details.

Also note that specifying an empty list here means no sources should be included.

Type
Required

array[posixPath]

No

Example:

include:
  - Dockerfile
  - my-app.js

exclude[]

Specify a list of POSIX-style paths or glob patterns that should be excluded from the module. Files that match these paths or globs are excluded when computing the version of the module, when responding to filesystem watch events, and when staging builds.

Note that you can also explicitly include files using the include field. If you also specify the include field, the files/patterns specified here are filtered from the files matched by include. See the Configuration Files guide for details.

Unlike the scan.exclude field in the project config, the filters here have no effect on which files and directories are watched for changes. Use the project scan.exclude field to affect those, if you have large directories that should not be watched for changes.

Type
Required

array[posixPath]

No

Example:

exclude:
  - tmp/**/*
  - '*.log'

repositoryUrl

A remote repository URL. Currently only supports git servers. Must contain a hash suffix pointing to a specific branch or tag, with the format: #<branch|tag>

Garden will import the repository source code into this module, but read the module's config from the local garden.yml file.

Type
Required

gitUrl | string

No

Example:

repositoryUrl: "git+https://github.com/org/repo.git#v2.0"

allowPublish

When false, disables pushing this module to remote registries via the publish command.

Type
Default
Required

boolean

true

No

generateFiles[]

A list of files to write to the module directory when resolving this module. This is useful to automatically generate (and template) any supporting files needed for the module.

Type
Default
Required

array[object]

[]

No

generateFiles[].sourcePath

generateFiles > sourcePath

POSIX-style filename to read the source file contents from, relative to the path of the module (or the ConfigTemplate configuration file if one is being applied). This file may contain template strings, much like any other field in the configuration.

Type
Required

posixPath

No

generateFiles[].targetPath

generateFiles > targetPath

POSIX-style filename to write the resolved file contents to, relative to the path of the module source directory (for remote modules this means the root of the module repository, otherwise the directory of the module configuration).

Note that any existing file with the same name will be overwritten. If the path contains one or more directories, they will be automatically created if missing.

Type
Required

posixPath

Yes

generateFiles[].resolveTemplates

generateFiles > resolveTemplates

By default, Garden will attempt to resolve any Garden template strings in source files. Set this to false to skip resolving template strings. Note that this does not apply when setting the value field, since that's resolved earlier when parsing the configuration.

Type
Default
Required

boolean

true

No

generateFiles[].value

generateFiles > value

The desired file contents as a string.

Type
Required

string

No

variables

A map of variables to use when applying the stack. You can define these here or you can place a terraform.tfvars file in the working directory root.

If you specified variables in the terraform provider config, those will be included but the variables specified here take precedence.

Type
Required

object

No

varfile

Specify a path (relative to the module root) to a file containing variables, that we apply on top of the module-level variables field.

The format of the files is determined by the configured file's extension:

  • .yaml/.yml - YAML. The file must consist of a YAML document, which must be a map (dictionary). Keys may contain any value type. YAML format is used by default.

  • .env - Standard "dotenv" format, as defined by dotenv.

  • .json - JSON. Must contain a single JSON object (not an array).

NOTE: The default varfile format was changed to YAML in Garden v0.13, since YAML allows for definition of nested objects and arrays.

To use different module-level varfiles in different environments, you can template in the environment name to the varfile name, e.g. varfile: "my-module.${environment.name}.env (this assumes that the corresponding varfiles exist).

Type
Required

posixPath

No

Example:

varfile: "my-module.env"

dependencies[]

The names of any services that this service depends on at runtime, and the names of any tasks that should be executed before this service is deployed.

Type
Default
Required

array[string]

[]

No

allowDestroy

If set to true, Garden will run terraform destroy on the stack when calling garden delete namespace or garden delete deploy <deploy name>.

Type
Default
Required

boolean

false

No

autoApply

If set to true, Garden will automatically run terraform apply -auto-approve when the stack is not up-to-date. Otherwise, a warning is logged if the stack is out-of-date, and an error thrown if it is missing entirely.

NOTE: This is not recommended for production, or shared environments in general!

Defaults to the value set in the provider config.

Type
Default
Required

boolean

null

No

root

Specify the path to the working directory root—i.e. where your Terraform files are—relative to the config directory.

Type
Default
Required

posixPath

"."

No

version

The version of Terraform to use. Defaults to the version set in the provider config. Set to null to use whichever version of terraform that is on your PATH.

Type
Required

string

No

workspace

Use the specified Terraform workspace.

Type
Required

string

No

Outputs

Module Outputs

The following keys are available via the ${modules.<module-name>} template string key for terraform modules.

${modules.<module-name>.buildPath}

The build path of the module.

Type

string

Example:

my-variable: ${modules.my-module.buildPath}

${modules.<module-name>.name}

The name of the module.

Type

string

${modules.<module-name>.path}

The source path of the module.

Type

string

Example:

my-variable: ${modules.my-module.path}

${modules.<module-name>.var.*}

A map of all variables defined in the module.

Type
Default

object

{}

${modules.<module-name>.var.<variable-name>}

Type

string | number | boolean | link | array[link]

${modules.<module-name>.version}

The current version of the module.

Type

string

Example:

my-variable: ${modules.my-module.version}

Service Outputs

The following keys are available via the ${runtime.services.<service-name>} template string key for terraform module services. Note that these are only resolved when deploying/running dependants of the service, so they are not usable for every field.

${runtime.services.<service-name>.version}

The current version of the service.

Type

string

Example:

my-variable: ${runtime.services.my-service.version}

Task Outputs

The following keys are available via the ${runtime.tasks.<task-name>} template string key for terraform module tasks. Note that these are only resolved when deploying/running dependants of the task, so they are not usable for every field.

${runtime.tasks.<task-name>.version}

The current version of the task.

Type

string

Example:

my-variable: ${runtime.tasks.my-tasks.version}

Last updated