terraformhcl

How can I get the Terraform module name programmatically?


I have defined the following Terraform module:

module "lambda" {
  source                = "../lambda"
  region                =  "us-west-1"
  account               = "${var.account}"
}

How can I take advantage from the module name to set the source parameter with an interpolation? I wish something like:

module "lambda" {
  source                = "../${this.name}"
  region                =  "us-west-1"
  account               = "${var.account}"
}

Solution

  • I think is not possible. There's a self that allows you to reference attributes within your resource, but the identifier is not an attribute. Also, self is only allowed within provisioners.

    I guess the only way to accomplish what you want is templating the .tf files, like:

    module {{ my-module}} {
      source                = "../{{ my-module }}"
      region                =  "us-west-1"
      account               = "${var.account}"
    

    but you should render the templates before terraform init. It's straightforward to setup in a CI pipeline, but I find it cumbersome when working locally.