Search code examples
terraformhcl

File listing in Terraform


I am trying to first generate in Terraform some files in a folder and then list those files. Unfortunately, I am not able to get the file list from the generation command since it is a shell script which does not outputs anything. As I understand Terraform tries to list files during the plan command. Is there a way to list files after they have been generated from the same terraform code?

resource "terraform_data" "files" {
  depends_on = [null_resource.generate_files]

  # Input argument is reflected to output
  input = [
    for f in fileset(local.root_path, "*.txt") : f
  ]
}


output "text_one" {
  value = terraform_data.files.output[0]
}

The output text_one value gives:

The given key does not identify an element in this collection value: the collection has no elements.

Solution

  • The fileset function, like all of Terraform's built-in functions that interact with the filesystem, expects the files its interacting with to already be present on disk when you first run Terraform and to remain unmodified throughout the plan and apply process.

    In any situation where you need to react to side-effects that occur during the apply process you will need to use either a managed resource (resource block) or a data resource (data block), and it must be the resource itself that is accessing the filesystem. In your example, although you've used resource "terraform_data" that resource type doesn't actually access the filesystem itself.

    The hashicorp/local provider is the typical home for resource types that interact with the local system, including the local filesystem. However, at the time I'm writing this answer it only has resource types for interacting with individual files, and doesn't have any resource types with equivalent functionality to fileset. Therefore to solve this with Terraform you'll either need to find another provider that can perform this function or write your own provider that does so.


    In the Terraform Registry I can see the community provider olidacombe/dirtree which has a data source dirtree_files that is documented to read files from disk. I've not used this provider myself and so I cannot vouch for it; if you intend to try it, you will need to study it yourself to make sure it doesn't include any unwanted or malicious behavior.