Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

templatefile doesn't wait for dependency described in resource block #25145

Closed
d-helios opened this issue Jun 5, 2020 · 2 comments
Closed

templatefile doesn't wait for dependency described in resource block #25145

d-helios opened this issue Jun 5, 2020 · 2 comments
Labels
enhancement v0.12 Issues (primarily bugs) reported against v0.12 releases

Comments

@d-helios
Copy link

d-helios commented Jun 5, 2020

Terraform Version

Terraform v0.12.26
+ provider.local v1.4.0

Terraform Configuration Files

main.tf

variable "templates" {
  type = list(object({
    name = string
    value = string
  }))
}

resource "local_file" "origin" {
  count = length(var.templates)
  filename = "${path.module}/${var.templates[count.index].name}.json"
  content = var.templates[count.index].value
}

resource "local_file" "rendered" {
  count = length(var.templates)
  filename = "${path.module}/${var.templates[count.index].name}.rendered.json"
  content = templatefile(local_file.origin[count.index].filename, {tf_version = "v0.12.26"})
  depends_on = [
    local_file.origin
  ]
}

input.tfvars

templates = [
  {
    name = "my_template_1"
    value = "tf_version = $${tf_version}"
  },
  {
    name = "my_template_2"
    value = "tf_version = $${tf_version}"
  }
]

Expected Behavior

  1. resource local_file.origin create a couple of files that are used as input for resource local_file.rendered

Actual Behavior

  1. terraform ignore depends_on block which leads to the following error.
    Call to function "templatefile" failed: no file exists at my_template_1.json

Steps to Reproduce

  1. terraform init
  2. terraform apply -auto-approve -var-file=input.tfvars
@danieldreier
Copy link
Contributor

Hello! I appreciate you reporting this, and I'm sorry this behavior isn't more obvious.

I think that what you're running into here is that functions always run at the very beginning of a terraform run, before any resources are created at all. I totally understand what you're trying to do here, but it won't work like this because files referenced by Terraform functions must exist before the terraform run starts.

I'm going to close this because I created a meta-issue tracking this broader category of issue - I probably triage about an issue a day relate to this, and it's time to start gathering data for a refactor, better documentation, better error messages, something to make this behavior more obvious.

If you want more guidance on how to work around this, your best bet is to seek support on the community forum.

@danieldreier danieldreier added enhancement v0.12 Issues (primarily bugs) reported against v0.12 releases labels Jun 5, 2020
@ghost
Copy link

ghost commented Jul 6, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement v0.12 Issues (primarily bugs) reported against v0.12 releases
Projects
None yet
Development

No branches or pull requests

2 participants