Skip to content

Commit 5939de7

Browse files
committed
support for terraform created local_files in archive
1 parent 4c09bdd commit 5939de7

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,26 @@ The
2626
is a tested reference of how to use the root module with the
2727
[event-project-log-entry submodule][event-project-log-entry-submodule].
2828

29+
## Terraform Created Source Files
30+
31+
If you have `local_file` terraform resources that need to be included in the function's archive include them in the optional `source_dependent_files`.
32+
33+
This will tell the module to wait until those files exist before creating the archive.
34+
35+
Example can also be seen in `examples/dynamic-files`
36+
37+
```hcl
38+
resource "local_file" "file" {
39+
content = "some content"
40+
filename = "${path.module}/function_source/terraform_created_file.txt"
41+
}
42+
43+
module "localhost_function" {
44+
...
45+
46+
source_dependent_files = [local_file.file]
47+
}
48+
```
2949
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
3050
## Inputs
3151

@@ -46,6 +66,7 @@ is a tested reference of how to use the root module with the
4666
| region | The region in which resources will be applied. | string | n/a | yes |
4767
| runtime | The runtime in which the function will be executed. | string | n/a | yes |
4868
| service\_account\_email | The service account to run the function as. | string | `""` | no |
69+
| source\_dependent\_files | A list of any terraform created `local_file`s that the module will wait for before creating the archive. | object | `<list>` | no |
4970
| source\_directory | The pathname of the directory which contains the function source code. | string | n/a | yes |
5071
| timeout\_s | The amount of time in seconds allotted for the execution of the function. | number | `"60"` | no |
5172

main.tf

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,37 @@
1414
* limitations under the License.
1515
*/
1616

17+
18+
/**
19+
* This allows users of the module to pass in any local_file resources
20+
* that we'll wait to exist before creating the archive.
21+
* This allows for us to delay the archive creation when terraform itself
22+
* is creating files. See this issue for more details
23+
* https://github.com/terraform-providers/terraform-provider-archive/issues/11
24+
*/
25+
resource "null_resource" "dependent_files" {
26+
triggers = {
27+
for file in var.source_dependent_files :
28+
pathexpand(file.filename) => file.id
29+
}
30+
}
31+
32+
data "null_data_source" "wait_for_files" {
33+
inputs = {
34+
# This ensures that this data resource will not be evaluated until
35+
# after the null_resource has been created.
36+
dependent_files_id = "${null_resource.dependent_files.id}"
37+
38+
# This value gives us something to implicitly depend on
39+
# in the archive_file below.
40+
source_dir = pathexpand(var.source_directory)
41+
}
42+
}
43+
1744
data "archive_file" "main" {
1845
type = "zip"
1946
output_path = pathexpand("${var.source_directory}.zip")
20-
source_dir = pathexpand(var.source_directory)
47+
source_dir = "${data.null_data_source.wait_for_files.outputs["source_dir"]}"
2148
}
2249

2350
resource "google_storage_bucket" "main" {

variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ variable "source_directory" {
7373
description = "The pathname of the directory which contains the function source code."
7474
}
7575

76+
variable "source_dependent_files" {
77+
type = list(object({
78+
filename = string
79+
id = string
80+
}))
81+
description = "A list of any terraform created `local_file`s that the module will wait for before creating the archive."
82+
default = []
83+
}
84+
7685
variable "timeout_s" {
7786
type = number
7887
default = 60

0 commit comments

Comments
 (0)