Skip to content

Commit 41a3a13

Browse files
authored
Merge pull request #28 from nick4fake/fix/27
Fixes #27: Add support for folder log entries
2 parents f61e9c3 + 1443a9d commit 41a3a13

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Event Folder Log Entry
2+
3+
This submodule configures a folder-level Stackdriver Logging export to
4+
act as an event which will trigger a Cloud Functions function configured
5+
by the [root module][root-module] or the
6+
[repository-function submodule][repository-function].
7+
8+
The export uses a provided filter to identify events of interest and
9+
publishes them to a dedicated Pub/Sub topic. The target function
10+
must be configured to subscribe to the topic in order to process each
11+
export event.
12+
13+
## Usage
14+
15+
The
16+
[automatic-labelling-from-localhost example][a7c-l7g-from-l7t-example]
17+
is a tested reference of how to use this submodule with the
18+
[root module].
19+
20+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
21+
## Inputs
22+
23+
| Name | Description | Type | Default | Required |
24+
|------|-------------|:----:|:-----:|:-----:|
25+
| filter | The filter to apply when exporting logs. | string | n/a | yes |
26+
| folder\_id | The ID of the folder to look for changes. | string | n/a | yes |
27+
| labels | A set of key/value label pairs to assign to any labelable resources. | map(string) | `<map>` | no |
28+
| name | The name to apply to any nameable resources. | string | n/a | yes |
29+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
30+
31+
## Outputs
32+
33+
| Name | Description |
34+
|------|-------------|
35+
| function\_event\_trigger | The information used to trigger the function when a log entry is exported to the topic. |
36+
37+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
38+
39+
## Requirements
40+
41+
The following sections describe the requirements which must be met in
42+
order to invoke this module.
43+
44+
### Software Dependencies
45+
46+
The following software dependencies must be installed on the system
47+
from which this module will be invoked:
48+
49+
- [Terraform][terraform-site] v0.11.Z
50+
- [Terraform Provider for Google Cloud Platform][t7m-provider-gcp-site]
51+
v2.1.Z
52+
53+
### IAM Roles
54+
55+
The Service Account which will be used to invoke this module must have
56+
the following IAM roles:
57+
58+
- Logs Configuration Writer: `roles/logging.configWriter`
59+
- Pub/Sub Admin: `roles/pubsub.admin`
60+
- Service Account User: `roles/iam.serviceAccountUser`
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+
[automatic-labelling-example]: ../../examples/automatic_labelling
71+
[repository-function]: ../repository-function
72+
[root-module]: ../..
73+
[terraform-site]: https://www.terraform.io/
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
locals {
18+
destination_uri = "pubsub.googleapis.com/projects/${var.project_id}/topics/${local.topic_name}"
19+
topic_name = element(concat(google_pubsub_topic.main.*.name, [""]), 0)
20+
}
21+
22+
module "log_export" {
23+
source = "terraform-google-modules/log-export/google"
24+
version = "3.0.0"
25+
26+
destination_uri = local.destination_uri
27+
filter = var.filter
28+
log_sink_name = var.name
29+
parent_resource_id = var.folder_id
30+
parent_resource_type = "folder"
31+
unique_writer_identity = "true"
32+
}
33+
34+
resource "google_pubsub_topic" "main" {
35+
name = var.name
36+
labels = var.labels
37+
project = var.project_id
38+
}
39+
40+
resource "google_pubsub_topic_iam_member" "main" {
41+
topic = google_pubsub_topic.main.name
42+
project = var.project_id
43+
member = module.log_export.writer_identity
44+
role = "roles/pubsub.publisher"
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 "function_event_trigger" {
18+
description = "The information used to trigger the function when a log entry is exported to the topic."
19+
value = {
20+
"event_type" = "google.pubsub.topic.publish"
21+
"resource" = google_pubsub_topic.main.name
22+
}
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
variable "filter" {
18+
type = string
19+
description = "The filter to apply when exporting logs."
20+
}
21+
22+
variable "labels" {
23+
type = map(string)
24+
default = {}
25+
description = "A set of key/value label pairs to assign to any labelable resources."
26+
}
27+
28+
variable "name" {
29+
type = string
30+
description = "The name to apply to any nameable resources."
31+
}
32+
33+
variable "project_id" {
34+
type = string
35+
description = "The ID of the project to which resources will be applied."
36+
}
37+
38+
variable "folder_id" {
39+
type = string
40+
description = "The ID of the folder to look for changes."
41+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
terraform {
18+
required_version = ">= 0.12"
19+
}

0 commit comments

Comments
 (0)