Skip to content

templatestring function allow for_each #35274

@reasland

Description

@reasland

Terraform Version

1.9.0-beta1

Use Cases

I am importing template files via the data HTTP resource, I give a list of files to import in a variable then use a for_each to import multiple items specified in configuration

Attempted Solutions

data "http" "function_data" {
  for_each = var.functions

  method = "GET"
  url = "https://gitlab.com/api/v4/projects/${each.value.project_id}/repository/files/${each.value.function}/raw?ref=${each.value.ref}"

  request_headers = {
    PRIVATE-TOKEN = var.gitlab_token
  }
}

# function being added in terraform 1.9

resource "aws_cloudfront_function" "functions" {
  for_each                     = var.functions
  name                         = each.key
  runtime                      = each.value.runtime
  comment                      = contains(keys(each.value), "comment") ? each.value.comment : ""
  code                         = templatestring(data.http.function_data[each.key].response_body, each.value.values)
}

above errors with this Invalid value for "template" parameter: invalid template expression: must be a direct reference to a single string from elsewhere, containing valid Terraform template syntax.

when I hardcode the each.key the issue is resolved, so I believe the current implementation of templatestring does not allow variables in the template section

I am able to use the hashicorp/template_file resource to bypass this issue, but that provider is depricated

Proposal

Allow variables to be used in templatestring() function in the template segment

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions