Skip to content

Commit 8c5529c

Browse files
authored
Merge pull request #14 from terraform-google-modules/aaron-lane/submodules
Redesign to use submodules
2 parents 0568eb4 + ceb0142 commit 8c5529c

File tree

40 files changed

+1138
-273
lines changed

40 files changed

+1138
-273
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ crash.log
4545

4646
credentials.json
4747

48-
examples/automatic_labelling/function_source.zip
48+
examples/automatic-labelling-from-localhost/function_source.zip
49+
examples/automatic-labelling-from-repository/function_source_copy

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ SHELL := /usr/bin/env bash
2121
# Docker build config variables
2222
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
2323
DOCKER_ORG := gcr.io/cloud-foundation-cicd
24-
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 0.11.10_216.0.0_1.19.1_0.1.10
24+
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 1.0.0
2525
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
2626

2727
all: check generate_docs

README.md

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,83 @@
1-
# terraform-google-event-function
1+
# Event Function
22

3-
This module configures a system which responds to filtered Stackdriver
4-
Logging events by invoking a Cloud Functions function.
3+
This module configures a system which responds to events by invoking a
4+
Cloud Functions function.
55

6-
A project-level Stackdriver Logging export uses a provided filter to
7-
identify events of interest and publish them to a dedicated Pub/Sub
8-
topic. A Cloud Functions function subscribes to the topic and uses
9-
provided source code to process each event. The source code is
10-
retrieved from an archive which is created locally and stored in a
11-
Storage bucket.
6+
The root module configures a function sourced from a directory on
7+
localhost to respond to a given event trigger. The source directory is
8+
compressed and uploaded as a Cloud Storage bucket object which will be
9+
leveraged by the function.
10+
11+
Alternatively, the
12+
[repository-function submodule][repository-function-submodule]
13+
configures a function sourced from a Cloud Source Repositories
14+
repository.
1215

1316
## Usage
1417

15-
The [examples directory](examples) contains tested references of how to
16-
use this module.
18+
The
19+
[automatic-labelling-from-localhost example][automatic-labelling-from-localhost-example]
20+
is a tested reference of how to use the root module with the
21+
[event-project-log-entry submodule][event-project-log-entry-submodule].
1722

1823
[^]: (autogen_docs_start)
1924

2025
## Inputs
2126

2227
| Name | Description | Type | Default | Required |
2328
|------|-------------|:----:|:-----:|:-----:|
24-
| function\_available\_memory\_mb | The amount of memory in megabytes allotted for the function to use. | string | `"256"` | no |
25-
| function\_description | The description of the function. | string | `"Processes log export events provided through a Pub/Sub topic subscription."` | no |
26-
| function\_entry\_point | The name of a method in the function source which will be invoked when the function is executed. | string | n/a | yes |
27-
| function\_environment\_variables | A set of key/value environment variable pairs to assign to the function. | map | `<map>` | no |
28-
| function\_event\_trigger\_failure\_policy\_retry | A toggle to determine if the function should be retried on failure. | string | `"false"` | no |
29-
| function\_labels | A set of key/value label pairs to assign to the function. | map | `<map>` | no |
30-
| function\_runtime | The runtime in which the function will be executed. | string | `"nodejs6"` | no |
31-
| function\_source\_archive\_bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map | `<map>` | no |
32-
| function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes |
33-
| function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no |
34-
| log\_export\_filter | The filter to apply when exporting logs to the Pub/Sub topic. | string | n/a | yes |
29+
| available\_memory\_mb | The amount of memory in megabytes allotted for the function to use. | string | `"256"` | no |
30+
| description | The description of the function. | string | `"Processes events."` | no |
31+
| entry\_point | The name of a method in the function source which will be invoked when the function is executed. | string | n/a | yes |
32+
| environment\_variables | A set of key/value environment variable pairs to assign to the function. | map | `<map>` | no |
33+
| event\_trigger | A source that fires events in response to a condition in another service. | map | n/a | yes |
34+
| labels | A set of key/value label pairs to assign to any lableable resources. | map | `<map>` | no |
3535
| name | The name to apply to any nameable resources. | string | n/a | yes |
3636
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
3737
| region | The region in which resources will be applied. | string | n/a | yes |
38+
| runtime | The runtime in which the function will be executed. | string | `"nodejs6"` | no |
39+
| source\_directory | The pathname of the directory which contains the function source code. | string | n/a | yes |
40+
| timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no |
41+
42+
## Outputs
43+
44+
| Name | Description |
45+
|------|-------------|
46+
| name | The name of the function. |
3847

3948
[^]: (autogen_docs_end)
4049

4150
## Requirements
4251

43-
The following requirements must be met in order to invoke this module:
44-
45-
1. [Software dependencies](#software-dependencies).
46-
2. [IAM roles](#iam-roles).
47-
3. [APIs](#apis).
52+
The following sections describe the requirements which must be met in
53+
order to invoke this module.
4854

4955
### Software Dependencies
5056

5157
The following software dependencies must be installed on the system
5258
from which this module will be invoked:
5359

54-
- [Terraform][terraform-site] v0.11.x
55-
- [Google Terraform provider][terraform-provider-google-site] v1.20.0
60+
- [Terraform][terraform-site] v0.11.Z
61+
- [Terraform Provider for Archive][terraform-provider-archive-site]
62+
v1.2.Z
63+
- [Terraform Provider for Google Cloud Platform][terraform-provider-gcp-site]
64+
v2.1.Z
5665

5766
### IAM Roles
5867

5968
The Service Account which will be used to invoke this module must have
6069
the following IAM roles:
6170

62-
- Cloud Functions Developer
63-
- Compute Viewer
64-
- Logs Configuration Writer
65-
- Pub/Sub Admin
66-
- Service Account User
67-
- Storage Admin
71+
- Cloud Functions Developer: `roles/cloudfunctions.developer`
72+
- Storage Admin: `roles/storage.admin`
6873

6974
### APIs
7075

7176
The project against which this module will be invoked must have the
7277
following APIs enabled:
7378

74-
- Cloud Functions API
75-
- Cloud Pub/Sub API
76-
- Google Cloud Storage
79+
- Cloud Functions API: `cloudfunctions.googleapis.com`
80+
- Cloud Storage API: `storage-component.googleapis.com`
7781

7882
The [Project Factory module][project-factory-module-site] can be used to
7983
provision projects with specific APIs activated.
@@ -176,7 +180,10 @@ from which the documentation will be generated:
176180

177181
Run `make generate_docs` to update the documentation.
178182

183+
[automatic-labelling-from-localhost-example]: examples/automatic-labelling-from-localhost
179184
[bundler-site]: https://bundler.io/
185+
[event-project-log-entry-submodule]: modules/event-project-log-entry
186+
[repository-function-submodule]: modules/repository-function
180187
[flake8-site]: https://pypi.org/project/flake8/
181188
[gofmt-site]: https://golang.org/cmd/gofmt/
182189
[hadolint-site]: https://github.com/hadolint/hadolint/
@@ -188,6 +195,6 @@ Run `make generate_docs` to update the documentation.
188195
[sample-variable-file]: test/fixtures/shared/terraform.tfvars.sample
189196
[shellcheck-site]: https://www.shellcheck.net/
190197
[terraform-docs-site]: https://github.com/segmentio/terraform-docs/releases/
191-
[terraform-provider-google-site]: https://github.com/terraform-providers/terraform-provider-google/
198+
[terraform-provider-gcp-site]: https://github.com/terraform-providers/terraform-provider-google/
192199
[terraform-site]: https://www.terraform.io/
193200
[terraform-validate-site]: https://www.terraform.io/docs/commands/validate.html
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Automatic Labelling from Localhost
2+
3+
This example demonstrates how to use the
4+
[root module][root-module] and the
5+
[event-project-log-entry submodule][event-project-log-entry-submodule]
6+
to configure a system
7+
which responds to Compute VM creation events by labelling them with the
8+
principal email address of the account responsible for causing the events.
9+
10+
## Usage
11+
12+
To provision this example, populate `terraform.tfvars` with the [required variables][#inputs] and run the following commands within
13+
this directory:
14+
15+
- `terraform init` to initialize the directory
16+
- `terraform plan` to generate the execution plan
17+
- `terraform apply` to apply the execution plan
18+
- `terraform destroy` to destroy the infrastructure
19+
20+
[^]: (autogen_docs_start)
21+
22+
## Inputs
23+
24+
| Name | Description | Type | Default | Required |
25+
|------|-------------|:----:|:-----:|:-----:|
26+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
27+
| region | The region in which resources will be applied. | string | n/a | yes |
28+
| zone | The zone in which resources will be applied. | string | n/a | yes |
29+
30+
[^]: (autogen_docs_end)
31+
32+
## Requirements
33+
34+
The following sections describe the requirements which must be met in
35+
order to invoke this module. The requirements of the
36+
[root module][root-module-requirements] and the
37+
[event-project-log-entry submodule][event-project-log-entry-submodule-requirements]
38+
must also be met.
39+
40+
### Software Dependencies
41+
42+
The following software dependencies must be installed on the system
43+
from which this module will be invoked:
44+
45+
- [Terraform][terraform-site] v0.11.Z
46+
47+
### IAM Roles
48+
49+
The Service Account which will be used to invoke this module must have
50+
the following IAM roles:
51+
52+
- Compute Instance Admin (v1): `roles/compute.instanceAdmin.v1`
53+
54+
### APIs
55+
56+
The project against which this module will be invoked must have the
57+
following APIs enabled:
58+
59+
- Compute Engine API: `compute.googleapis.com`
60+
61+
[event-project-log-entry-submodule-requirements]: ../../modules/event-project-log-entry/README.md#requirements
62+
[event-project-log-entry-submodule]: ../../modules/event-project-log-entry
63+
[root-module-requirements]: ../../README.md#requirements
64+
[root-module]: ../..

test/fixtures/automatic_labelling/main.tf renamed to examples/automatic-labelling-from-localhost/main.tf

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,61 @@
1414
* limitations under the License.
1515
*/
1616

17+
terraform {
18+
required_version = "~> 0.11.0"
19+
}
20+
1721
provider "archive" {
18-
version = "~> 1.1"
22+
version = "~> 1.0"
1923
}
2024

2125
provider "google" {
22-
version = "~> 1.20"
26+
version = "~> 2.1"
2327
}
2428

2529
provider "random" {
2630
version = "~> 2.0"
2731
}
2832

2933
provider "null" {
30-
version = "~> 2.0"
34+
version = "~> 1.0"
3135
}
3236

3337
resource "random_pet" "main" {
3438
separator = "-"
3539
}
3640

37-
module "automatic_labelling" {
38-
source = "../../../examples/automatic_labelling"
41+
module "event_project_log_entry" {
42+
source = "../../modules/event-project-log-entry"
3943

44+
filter = "protoPayload.@type=\"type.googleapis.com/google.cloud.audit.AuditLog\" protoPayload.methodName:insert operation.first=true"
45+
name = "${random_pet.main.id}"
4046
project_id = "${var.project_id}"
41-
name = "automatic-labelling-${random_pet.main.id}"
42-
region = "${var.region}"
4347
}
4448

45-
resource "null_resource" "wait_for_cloud_functions_function" {
49+
module "localhost_function" {
50+
source = "../.."
51+
52+
description = "Labels resource with owner information."
53+
entry_point = "labelResource"
54+
55+
environment_variables = {
56+
LABEL_KEY = "principal-email"
57+
}
58+
59+
event_trigger = "${module.event_project_log_entry.function_event_trigger}"
60+
name = "${random_pet.main.id}"
61+
project_id = "${var.project_id}"
62+
region = "${var.region}"
63+
source_directory = "${path.module}/function_source"
64+
}
65+
66+
resource "null_resource" "wait_for_function" {
4667
provisioner "local-exec" {
4768
command = "sleep 60"
4869
}
4970

50-
depends_on = ["module.automatic_labelling"]
71+
depends_on = ["module.localhost_function"]
5172
}
5273

5374
resource "google_compute_instance" "main" {
@@ -58,7 +79,7 @@ resource "google_compute_instance" "main" {
5879
}
5980

6081
machine_type = "f1-micro"
61-
name = "unlabelled"
82+
name = "unlabelled-${random_pet.main.id}"
6283
zone = "${var.zone}"
6384

6485
network_interface = {
@@ -67,5 +88,5 @@ resource "google_compute_instance" "main" {
6788

6889
project = "${var.project_id}"
6990

70-
depends_on = ["null_resource.wait_for_cloud_functions_function"]
91+
depends_on = ["null_resource.wait_for_function"]
7192
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
output "compute_instance_name" {
18+
value = "${google_compute_instance.main.name}"
19+
description = "The name of the unlabelled Compute instance."
20+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Automatic Labelling from Repository
2+
3+
This example demonstrates how to use the
4+
[repository-function submodule][repository-function-submodule] and the
5+
[event-project-log-entry submodule][event-project-log-entry-submodule]
6+
to configure a system
7+
which responds to Compute VM creation events by labelling them with the
8+
principal email address of the account responsible for causing the events.
9+
10+
## Usage
11+
12+
To provision this example, populate `terraform.tfvars` with the [required variables][#inputs] and run the following commands within
13+
this directory:
14+
15+
- `terraform init` to initialize the directory
16+
- `terraform plan` to generate the execution plan
17+
- `terraform apply` to apply the execution plan
18+
- `terraform destroy` to destroy the infrastructure
19+
20+
[^]: (autogen_docs_start)
21+
22+
## Inputs
23+
24+
| Name | Description | Type | Default | Required |
25+
|------|-------------|:----:|:-----:|:-----:|
26+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
27+
| region | The region in which resources will be applied. | string | n/a | yes |
28+
| zone | The zone in which resources will be applied. | string | n/a | yes |
29+
30+
[^]: (autogen_docs_end)
31+
32+
## Requirements
33+
34+
The following sections describe the requirements which must be met in
35+
order to invoke this module. The
36+
[repository-function submodule requirements][repository-function-submodule-requirements]
37+
and the
38+
[event-project-log-entry submodule requirements][event-project-log-entry-submodule-requirements]
39+
must also be met.
40+
41+
### Software Dependencies
42+
43+
The following software dependencies must be installed on the system
44+
from which this module will be invoked:
45+
46+
- [Terraform][terraform-site] v0.11.Z
47+
48+
### IAM Roles
49+
50+
The Service Account which will be used to invoke this module must have
51+
the following IAM roles:
52+
53+
- Compute Instance Admin (v1): `roles/compute.instanceAdmin.v1`
54+
- Source Repository Admin: `roles/source.admin`
55+
56+
### APIs
57+
58+
The project against which this module will be invoked must have the
59+
following APIs enabled:
60+
61+
- Cloud Source Repositories API: `sourcerepo.googleapis.com`
62+
- Compute Engine API: `compute.googleapis.com`
63+
64+
[event-project-log-entry-submodule-requirements]: ../../modules/event-project-log-entry/README.md#requirements
65+
[event-project-log-entry-submodule]: ../../modules/event-project-log-entry
66+
[repository-function-submodule-requirements]: ../../modules/repository-function/README.md#requirements
67+
[repository-function-submodule]: ../../modules/repository-function

0 commit comments

Comments
 (0)