FormidableLabs / terraform-aws-serverless

Infrastructure support for Serverless framework apps, done the right way
https://registry.terraform.io/modules/FormidableLabs/serverless/aws
MIT License
144 stars 19 forks source link

Feature: Autoload submodules. #28

Open ryan-roemer opened 5 years ago

ryan-roemer commented 5 years ago

Idea:

module "serverless" {
  source = "FormidableLabs/serverless/aws"

  region       = "us-east-1"
  service_name = "sparklepants"
  stage        = "${var.stage}"

  modules = [
    "xray" # submodules/xray/
  ]

  plugins = [
    "serverless-s3-sync" # submodules/plugin-serverless-s3-sync/
  ]
}
ryan-roemer commented 5 years ago

Research

I’ve been trying to wrangle a seemingly simple idea: I want to conditionally include a sub-module based on a variable.

Things I’ve tried that failed:

count: Idea - set count to 0 if not using.

module "foo" {
  count  = "${var.use_foo ? 1 : 0}"
  source = "./modules/foo"
}

source: Idea - swap real submodule for a completely empty one.

module "foo" {
  source = "${var.use_foo ? "./modules/foo" : "./modules/empty"}"
}
CumpsD commented 4 years ago

These things seem related to this:

ryan-roemer commented 4 years ago

0.12 Update

module.count is now a reserved word. When implemented in the future, will presumably get us there. See https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/#module-count-and-for_each

For a long time, users have wished to be able to use the count meta-argument within module blocks, allowing multiple instances of the same module to be created more easily.

Again, we have been laying the groundwork for this during Terraform 0.12 development and expect to complete this work in a later release. Along with count, module blocks will also accept the new for_each argument described for resources above, with similar results.

This feature is particularly complicated to implement within Terraform's existing architecture, so some more work will certainly be required before we can support this. To avoid further breaking changes in later releases, 0.12 will reserve the module input variable names count and for_each in preparation for the completion of this feature.