Skip to content

Commit 841c5fa

Browse files
authored
Merge pull request #38 from taylorludwig/feature/37-terraform-created-files-in-archive
Feature/37 terraform created files in archive
2 parents 78e3559 + e62e4c5 commit 841c5fa

File tree

25 files changed

+500
-42
lines changed

25 files changed

+500
-42
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ credentials.json
5151
examples/automatic-labelling-folder/function_source.zip
5252
examples/automatic-labelling-from-localhost/function_source.zip
5353
examples/automatic-labelling-from-repository/function_source_copy
54+
examples/dynamic-files/function_source.zip
55+
examples/dynamic-files/function_source/terraform_created_file.txt
5456

5557
node_modules
5658
yarn.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- The `source_dependent_files` variable. If used `archive` won't be created until Terraform created `local_file`s are finished. [#38]
13+
1014
### Fixed
1115

1216
- Updating the source for a local event-function doesn't update the function. [#32]
@@ -36,6 +40,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
3640
[1.1.0]: https://github.com/terraform-google-modules/terraform-google-event-function/compare/v1.0.0...v1.1.0
3741

3842

43+
[#38]: https://github.com/terraform-google-modules/terraform-google-event-function/issues/38
3944
[#32]: https://github.com/terraform-google-modules/terraform-google-event-function/issues/32
4045
[#28]: https://github.com/terraform-google-modules/terraform-google-event-function/pull/28
4146
[#23]: https://github.com/terraform-google-modules/terraform-google-event-function/pull/23

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# Make will use bash instead of sh
1919
SHELL := /usr/bin/env bash
2020

21-
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.4.3
21+
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0
2222
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
2323
REGISTRY_URL := gcr.io/cloud-foundation-cicd
2424

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

build/int.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ tags:
3838
- 'integration'
3939
substitutions:
4040
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
41-
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.3'
41+
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0'

build/lint.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ tags:
2121
- 'lint'
2222
substitutions:
2323
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
24-
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.4.3'
24+
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0'

examples/dynamic-files/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Automatic Labelling for folder projects
2+
3+
This example demonstrates how to use the
4+
[root module][root-module] that will contain source
5+
code files generated from Terraform itself.
6+
7+
## Usage
8+
9+
To provision this example, populate `terraform.tfvars` with the [required variables](#inputs) and run the following commands within
10+
this directory:
11+
12+
- `terraform init` to initialize the directory
13+
- `terraform plan` to generate the execution plan
14+
- `terraform apply` to apply the execution plan
15+
- `terraform destroy` to destroy the infrastructure
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Inputs
19+
20+
| Name | Description | Type | Default | Required |
21+
|------|-------------|:----:|:-----:|:-----:|
22+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
23+
| region | The region in which resources will be applied. | string | n/a | yes |
24+
25+
## Outputs
26+
27+
| Name | Description |
28+
|------|-------------|
29+
| function\_name | The name of the function created |
30+
| random\_file\_string | The content of the terraform created file in the source directory. |
31+
| region | The region in which resources are applied. |
32+
33+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
34+
35+
## Requirements
36+
37+
The following sections describe the requirements which must be met in
38+
order to invoke this module. The requirements of the
39+
[root module][root-module-requirements] and the
40+
[event-folder-log-entry submodule][event-folder-log-entry-submodule-requirements]
41+
must also be met.
42+
43+
### Software Dependencies
44+
45+
The following software dependencies must be installed on the system
46+
from which this module will be invoked:
47+
48+
- [Terraform][terraform-site] v0.12
49+
50+
### IAM Roles
51+
52+
The Service Account which will be used to invoke this module must have
53+
the following IAM roles:
54+
55+
- Logs Configuration Writer: `roles/logging.configWriter`
56+
- Pub/Sub Admin: `roles/pubsub.admin`
57+
- Service Account User: `roles/iam.serviceAccountUser`
58+
59+
- Default AppSpot user: `roles/owner`
60+
- Your user: `roles/resourcemanager.projectCreator`
61+
62+
### APIs
63+
64+
The project against which this module will be invoked must have the
65+
following APIs enabled:
66+
67+
- Cloud Pub/Sub API: `pubsub.googleapis.com`
68+
- Stackdriver Logging API: `logging.googleapis.com`
69+
70+
[event-folder-log-entry-submodule-requirements]: ../../modules/event-folder-log-entry/README.md#requirements
71+
[event-folder-log-entry-submodule]: ../../modules/event-folder-log-entry
72+
[root-module-requirements]: ../../README.md#requirements
73+
[root-module]: ../..
74+
[terraform-site]: https://terraform.io/
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
const fs = require("fs");
18+
const path = require("path");
19+
const filePath = path.join(__dirname, "terraform_created_file.txt");
20+
21+
/**
22+
* Cloud function entrypoint
23+
*
24+
* @param {!Object} event Event payload and metadata.
25+
* @param {!Function} callback Callback function to signal completion.
26+
*/
27+
exports.fileContent = (data, context, callback) => {
28+
console.log("Received event");
29+
30+
fs.readFile(filePath, { encoding: "utf-8" }, function(err, data) {
31+
if (!err) {
32+
callback(null, data);
33+
} else {
34+
callback(err);
35+
}
36+
});
37+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "dynamic-files",
3+
"version": "0.0.1"
4+
}

examples/dynamic-files/main.tf

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
resource "random_pet" "main" {
18+
length = 2
19+
separator = "-"
20+
}
21+
22+
resource "google_storage_bucket" "trigger_bucket" {
23+
name = "${random_pet.main.id}-trigger"
24+
force_destroy = true
25+
location = var.region
26+
project = var.project_id
27+
storage_class = "REGIONAL"
28+
}
29+
30+
resource "random_string" "random" {
31+
length = 16
32+
special = false
33+
}
34+
35+
resource "local_file" "file" {
36+
content = random_string.random.result
37+
filename = "${path.module}/function_source/terraform_created_file.txt"
38+
}
39+
40+
module "localhost_function" {
41+
source = "../.."
42+
43+
description = "Returns back the random file content"
44+
entry_point = "fileContent"
45+
46+
event_trigger = {
47+
event_type = "google.storage.object.finalize"
48+
resource = google_storage_bucket.trigger_bucket.name
49+
}
50+
51+
name = random_pet.main.id
52+
project_id = var.project_id
53+
region = var.region
54+
source_directory = "${path.module}/function_source"
55+
runtime = "nodejs8"
56+
57+
source_dependent_files = [local_file.file]
58+
}
59+
60+
resource "null_resource" "wait_for_function" {
61+
provisioner "local-exec" {
62+
command = "sleep 60"
63+
}
64+
65+
depends_on = [module.localhost_function]
66+
}

0 commit comments

Comments
 (0)