2019-08-20 10:50:01 +02:00
|
|
|
---
|
2021-11-23 00:57:25 +01:00
|
|
|
layout: "language"
|
|
|
|
page_title: "fileset - Functions - Configuration Language"
|
|
|
|
sidebar_current: "docs-funcs-file-file-set"
|
|
|
|
description: |-
|
|
|
|
The fileset function enumerates a set of regular file names given a pattern.
|
2019-08-20 10:50:01 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# `fileset` Function
|
|
|
|
|
2019-08-28 17:54:04 +02:00
|
|
|
`fileset` enumerates a set of regular file names given a path and pattern.
|
|
|
|
The path is automatically removed from the resulting set of file names and any
|
|
|
|
result still containing path separators always returns forward slash (`/`) as
|
|
|
|
the path separator for cross-system compatibility.
|
2019-08-20 10:50:01 +02:00
|
|
|
|
|
|
|
```hcl
|
2019-08-28 17:54:04 +02:00
|
|
|
fileset(path, pattern)
|
2019-08-20 10:50:01 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
Supported pattern matches:
|
|
|
|
|
|
|
|
- `*` - matches any sequence of non-separator characters
|
2019-08-31 02:14:38 +02:00
|
|
|
- `**` - matches any sequence of characters, including separator characters
|
2019-08-20 10:50:01 +02:00
|
|
|
- `?` - matches any single non-separator character
|
2019-08-31 02:14:38 +02:00
|
|
|
- `{alternative1,...}` - matches a sequence of characters if one of the comma-separated alternatives matches
|
|
|
|
- `[CLASS]` - matches any single non-separator character inside a class of characters (see below)
|
|
|
|
- `[^CLASS]` - matches any single non-separator character outside a class of characters (see below)
|
|
|
|
|
2021-07-15 18:22:17 +02:00
|
|
|
Note that the doublestar (`**`) must appear as a path component by itself. A
|
2021-11-23 00:57:25 +01:00
|
|
|
pattern such as /path** is invalid and will be treated the same as /path*, but
|
|
|
|
/path*/** should achieve the desired result.
|
2021-07-15 18:22:17 +02:00
|
|
|
|
2019-08-31 02:14:38 +02:00
|
|
|
Character classes support the following:
|
|
|
|
|
|
|
|
- `[abc]` - matches any single character within the set
|
|
|
|
- `[a-z]` - matches any single character within the range
|
2019-08-20 10:50:01 +02:00
|
|
|
|
|
|
|
Functions are evaluated during configuration parsing rather than at apply time,
|
|
|
|
so this function can only be used with files that are already present on disk
|
|
|
|
before Terraform takes any actions.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
```
|
2019-08-28 17:54:04 +02:00
|
|
|
> fileset(path.module, "files/*.txt")
|
2019-08-20 10:50:01 +02:00
|
|
|
[
|
2019-08-28 17:54:04 +02:00
|
|
|
"files/hello.txt",
|
|
|
|
"files/world.txt",
|
|
|
|
]
|
|
|
|
|
2019-08-31 02:14:38 +02:00
|
|
|
> fileset(path.module, "files/{hello,world}.txt")
|
|
|
|
[
|
|
|
|
"files/hello.txt",
|
|
|
|
"files/world.txt",
|
|
|
|
]
|
|
|
|
|
|
|
|
> fileset("${path.module}/files", "*")
|
|
|
|
[
|
|
|
|
"hello.txt",
|
|
|
|
"world.txt",
|
|
|
|
]
|
|
|
|
|
|
|
|
> fileset("${path.module}/files", "**")
|
2019-08-28 17:54:04 +02:00
|
|
|
[
|
|
|
|
"hello.txt",
|
|
|
|
"world.txt",
|
2019-08-31 02:14:38 +02:00
|
|
|
"subdirectory/anotherfile.txt",
|
2019-08-20 10:50:01 +02:00
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2019-08-28 17:54:04 +02:00
|
|
|
A common use of `fileset` is to create one resource instance per matched file, using
|
2021-11-23 00:57:25 +01:00
|
|
|
[the `for_each` meta-argument](/docs/language/meta-arguments/for_each.html):
|
2019-08-28 17:54:04 +02:00
|
|
|
|
2019-08-20 10:50:01 +02:00
|
|
|
```hcl
|
|
|
|
resource "example_thing" "example" {
|
2019-08-28 17:54:04 +02:00
|
|
|
for_each = fileset(path.module, "files/*")
|
2019-08-20 10:50:01 +02:00
|
|
|
|
|
|
|
# other configuration using each.value
|
|
|
|
}
|
|
|
|
```
|