Link Search Menu Expand Document

Terraform supports reusable resource definition code as modules

Fogg encourages using modules to reduce code duplication. The top-level fogg.yml parameter, modules will generate a module directory with some boilerplate code to kickstart module management.

This fogg.yml snippet:

<snip>
modules:
  webserver: {}
  database: {}

Will produce the following directory structure:

└── terraform
    <snip>
    └── modules
        ├── database
        │   ├── Makefile
        │   ├── README.md
        │   ├── fogg.tf
        │   ├── main.tf
        │   ├── outputs.tf
        │   └── variables.tf
        └── webserver
            ├── Makefile
            ├── README.md
            ├── fogg.tf
            ├── main.tf
            ├── outputs.tf
            └── variables.tf

To reference these modules in a fogg-managed workspace, invoke the module with a module resource and a source parameter that refers to the appropriate module directory:

module webserver {
  source   = "../../../modules/webserver"
}

Fogg can also keep these module invocations up to date if a module_source is specified as part of a workspace configuration in fogg.yml:

<snip>
envs:
  development:
    components:
      vpc:
        module_source: "github.com/scholzj/terraform-aws-vpc"

Fogg will write a complete invocation of that module to the workspace’s main.tf file:

# Auto-generated by fogg. Do not edit
# Make improvements in fogg, so that everyone can benefit.

module terraform-aws-vpc {
  source          = "github.com/scholzj/terraform-aws-vpc"
  aws_region      = local.aws_region
  aws_zones       = local.aws_zones
  private_subnets = local.private_subnets
  tags            = local.tags
  vpc_cidr        = local.vpc_cidr
  vpc_name        = local.vpc_name
}

Tip

Terraform can also invoke remote module sources

# Module in a public GitHub repo
module public_module_call {
   source = github.com/publicproject/publicrepo//path/to/module?ref=v0.31.1}
}

# Module in a private GitHub repo
module private_module_call {
   source = git@github.com:privateproject/privaterepo//path/to/module?ref=v0.31.1}
}