Skip to main content

Module: config

Terraform module to convert local and remote YAML configuration templates into Terraform lists and maps.

Introduction

The module accepts paths to local and remote YAML configuration template files and converts the templates into Terraform lists and maps for consumption in other Terraform modules.

The module can accept a map of parameters for interpolation within the YAML config templates.

The module also supports a top-level import attribute in map configuration templates, which will include the file and perform a deep merge. Up to 10 levels of imports hierarchy are supported, and all imported maps are deep merged into a final configuration map.

For example, if you have a config file like this (e.g. myconfig.yaml):

  import:
- file1
- file2

Then, this module will deep merge file1.yaml and file2.yaml into myconfig.yaml.

Note: Do not include the extensions (e.g. .yaml) in the imports.

Attributions

Big thanks to Imperative Systems Inc. for the excellent deepmerge Terraform module to perform a deep map merge of standard Terraform maps and objects.

Usage

For a complete example, see examples/complete.

For automated tests of the complete example using bats and Terratest (which tests and deploys the example on Datadog), see test.

For an example of using local config maps with import and deep merging into a final configuration map, see examples/imports-local.

For an example of using remote config maps with import and deep merging into a final configuration map, see examples/imports-remote.

Examples

Example of local and remote maps and lists configurations with interpolation parameters

module "yaml_config" {
source = "cloudposse/config/yaml"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

map_config_local_base_path = "./config"

map_config_paths = [
"map-configs/*.yaml",
"https://raw.githubusercontent.com/cloudposse/terraform-opsgenie-incident-management/master/examples/config/resources/services.yaml",
"https://raw.githubusercontent.com/cloudposse/terraform-opsgenie-incident-management/master/examples/config/resources/team_routing_rules.yaml"
]

list_config_local_base_path = "./config"

list_config_paths = [
"list-configs/*.yaml",
"https://raw.githubusercontent.com/cloudposse/terraform-aws-service-control-policies/master/examples/complete/policies/organization-policies.yaml"
]

parameters = {
param1 = "1"
param2 = "2"
}

context = module.this.context
}

Example of local maps configurations with import and deep merging

In the example, we use two levels of imports, and the module deep merges the local config files imports-level-3.yaml, imports-level-2.yaml, and imports-level-1.yaml into a final config map.

See examples/imports-local for more details.

module "yaml_config" {
source = "cloudposse/config/yaml"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

map_config_local_base_path = "./config"

map_config_paths = [
"imports-level-1.yaml"
]

context = module.this.context
}

Example of remote maps configurations with with import and deep merging

In the example, we use two levels of imports, and the module deep merges the remote config files globals.yaml, ue2-globals.yaml, and ue2-prod.yaml into a final config map.

See examples/imports-remote for more details.

module "yaml_config" {
source = "cloudposse/config/yaml"
# Cloud Posse recommends pinning every module to a specific version
# version = "x.x.x"

map_config_remote_base_path = "https://raw.githubusercontent.com/cloudposse/atmos/master/example/stacks"

map_config_paths = [
"https://raw.githubusercontent.com/cloudposse/atmos/master/example/stacks/ue2-prod.yaml"
]

context = module.this.context
}