0

During the creation of the new TF module, I met the issue with templatefile functionality.

In the triggers.tf I declared the module with an argument which is templated config. The module ./modules/trigger is using provided argument (filename).

During the TF plan I'm receiving:

 Error: Unsupported argument
│
│   on triggers.tf line 43, in module "trigger":
│   43:   filename = templatefile("${path.module}/templates/trigger.yaml.tpl", {
│
│ An argument named "filename" is not expected here.

./modules/trigger/main.tf

resource "null_resource" "trigger" {

  filename = var.filename

  provisioner "local-exec" {
    command = <<EOT
      gcloud alpha ... --trigger_config ${self.filename}  \
EOT
  }
}

triggers.tf

locals {
  repos = yamldecode(file("${path.module}/repositories/repos.yaml"))
  repos = {
    for pair in setproduct([local.repos["project"]], local.repos["repos"]) : "${pair[0]}/${pair[1]}" => {
      project = pair[0]
      repo    = pair[1]
    }
  }
}

module "trigger" {
  source   = "./modules/trigger"
  for_each = local.repos

  filename = templatefile("${path.module}/templates/trigger.yaml.tpl", {
    trigger_event           = "push"
    bitbucket_project       = each.value["project"]
    bitbucket_repo          = each.value["repo"]
  })
}

/repositories/repos.yaml

project: test
repos:
  - cloud-test

/templates/trigger.yaml.tpl

name: "${trigger_event}"
project: "${bitbucket_project}"
repo: "${bitbucket_repo}"
3
  • You need to provide only the filename it seems, not the entire file, so you probably need two steps: create a file using a local_file resource and providing the path to the variable filename. Commented Aug 29, 2022 at 13:18
  • @MarkoE thanks for your answer! I tried the solution with yamlencode -yamlencode(templatefile("${path.module}/templates/trigger.yaml and also I received the same issue. Commented Aug 29, 2022 at 13:23
  • Yeah, I don't think null_resource has the filename argument as well. Commented Aug 29, 2022 at 13:25

1 Answer 1

1

Here are the changes that are required for this to work:

  1. Create a file using local_file resource
  2. Update the null_resource in the module to use only the variable and not the non-existent argument (i.e., filename)

Note that renaming local variables is not allowed, so having two repos definitions will cause errors. I suggest changing it to something like:

locals {
  repositories = yamldecode(file("${path.module}/repositories/repos.yaml"))
  repos = {
    for pair in setproduct([local.repositories["project"]], local.repositories["repos"]) : "${pair[0]}/${pair[1]}" => {
      project = pair[0]
      repo    = pair[1]
    }
  }
}

So in the root of the module, here is what part of code you need:

resource "local_file" "trigger_file" {
  for_each = local.repos
  content = templatefile("${path.module}/templates/trigger.yaml.tpl", {
    trigger_event     = "push"
    bitbucket_project = each.value["project"]
    bitbucket_repo    = each.value["repo"]
  })
  filename = "${path.module}/trigger-${each.value["project"]}.yaml"
}

The local_file resource will create as many files as you need, where the names of the files will be different depending on the project name. So for your current example this will crate a file with name trigger-test.yaml in the same directory. Then, in the module you are calling, the null_resource would change to:

resource "null_resource" "trigger" {

  provisioner "local-exec" {
    command = <<EOT
      gcloud alpha ... --trigger_config ${var.filename}  \
EOT
  }
}

This should work if you have defined the variable filename in the module. Lastly, when calling the module, you can use the following code:

module "trigger" {
  source   = "./modules/trigger"
  for_each = local.repos

  filename = "${path.module}/trigger-${each.value["project"]}.yaml"
}
Sign up to request clarification or add additional context in comments.

5 Comments

Invalid value for "vars" parameter: vars map does not contain key "bitbucket_project" How should I fix it?
Where exactly does that happen?
Above configuration getting me Invalid value for "vars" parameter: vars map does not contain key "bitbucket_project"
Which part of the configuration? Have you changed anything in the template file?
I just tested this locally and it works. Note that there wasn't a single change compared to the code from the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.