Skip to content

Directory equivalent of fileexists and fileset functions #34694

@bryanhonof

Description

@bryanhonof

Terraform Version

Terraform 1.7.3
on darwin_arm64

Use Cases

I'd like to be able to have a directory structure of pure data encoded in some formats like YAML, or even HCL.
E.g. a monorepo that contains the configuration of git repositories.

$ ls -lh
total 8
-rw-r--r--  1 bryanhonof  staff    63B Feb 17 16:55 main.tf
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo1.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo10.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo2.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo3.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo4.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo5.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo6.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo7.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo8.git
drwxr-xr-x  2 bryanhonof  staff    64B Feb 17 16:55 repo9.git

Then, I'd like to be able to do the following in HCL/Terraform.

# main.tf
locals {
  repositories = directoryset(path.module, "*.git")
}

# Some logic using modules and `for_each` to create resources
# The module would then internally use `fileset` and `file` to do more complicated operations
# ...

output "scanned_repositories" {
  value = local.repositories
}
$ terraform init
...
$ terraform plan
...
$ terraform apply
...
$ terraform output
scanned_repositories = toset([
  "repo1.git",
  "repo10.git",
  "repo2.git",
  "repo3.git",
  "repo4.git",
  "repo5.git",
  "repo6.git",
  "repo7.git",
  "repo8.git",
  "repo9.git",
])

Attempted Solutions

Using fileset in many ways.
Some other hacky solutions to figure out if something's a directory or not.

Proposal

Implement the directory equivalent of fileexists and fileset.

References

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