Skip to content

Commit 47ec5a2

Browse files
authored
Merge pull request #10 from terraform-google-modules/aaron-lane/root-module-redux
Define root module
2 parents e7924c3 + e0dbe7f commit 47ec5a2

File tree

9 files changed

+425
-2
lines changed

9 files changed

+425
-2
lines changed

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.5.3

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this
6+
project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
## [Unreleased]
9+
10+
## [0.1.0] - 2019-02-15
11+
12+
### Added
13+
14+
- Initial release
15+
16+
[Unreleased]: https://github.com/terraform-google-modules/terraform-google-event-function/compare/v0.1.0...HEAD
17+
[0.1.0]

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @morgante @aaron-lane @adrienthebo

Gemfile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2019 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
ruby '2.5.3'
16+
17+
source 'https://rubygems.org/' do
18+
gem 'kitchen-terraform', '~> 4.2'
19+
end

README.md

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,194 @@
1-
# terraform-google-event-function
1+
# terraform-google-event-function
2+
3+
This module configures a system which responds to filtered Stackdriver
4+
Logging events by invoking a Cloud Functions function.
5+
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.
12+
13+
## Usage
14+
15+
The [examples directory](examples) contains tested references of how to
16+
use this module.
17+
18+
[^]: (autogen_docs_start)
19+
20+
## Inputs
21+
22+
| Name | Description | Type | Default | Required |
23+
|------|-------------|:----:|:-----:|:-----:|
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\_archive\_bucket\_location | The Google Cloud Storage location in which to create the function source archive bucket. | string | `"US"` | no |
33+
| function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes |
34+
| function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no |
35+
| log\_export\_filter | The filter to apply when exporting logs to the Pub/Sub topic. | string | n/a | yes |
36+
| name | The name to apply to any nameable resources. | string | `"event-function"` | no |
37+
| project\_id | The ID of the project to which resources will be applied. | string | n/a | yes |
38+
| region | The region in which resources will be applied. | string | n/a | yes |
39+
40+
[^]: (autogen_docs_end)
41+
42+
## Requirements
43+
44+
The following requirements must be met in order to invoke this module:
45+
46+
1. [Software dependencies](#software-dependencies).
47+
2. [IAM roles](#iam-roles).
48+
3. [APIs](#apis).
49+
50+
### Software Dependencies
51+
52+
The following software dependencies must be installed on the system
53+
from which this module will be invoked:
54+
55+
- [Terraform][terraform-site] v0.11.x
56+
- [Google Terraform provider][terraform-provider-google-site] v1.20.0
57+
58+
### IAM Roles
59+
60+
The Service Account which will be used to invoke this module must have
61+
the following IAM roles:
62+
63+
- Cloud Functions Developer
64+
- Compute Viewer
65+
- Logs Configuration Writer
66+
- Pub/Sub Admin
67+
- Service Account User
68+
- Storage Admin
69+
70+
### APIs
71+
72+
The project against which this module will be invoked must have the
73+
following APIs enabled:
74+
75+
- Cloud Functions API
76+
- Cloud Pub/Sub API
77+
- Google Cloud Storage
78+
79+
The [Project Factory module][project-factory-module-site] can be used to
80+
provision projects with specific APIs activated.
81+
82+
## Testing
83+
84+
The [fixtures directory](test/fixtures) and
85+
[integration directory](test/integration) comprise Terraform
86+
modules and InSpec tests used to verify the behaviour of this module.
87+
88+
### Testing Software Dependencies
89+
90+
The following software dependencies must be installed on the system
91+
from which the tests will be invoked:
92+
93+
- [Ruby][ruby-site] v2.5
94+
- [Bundler][bundler-site] v1.17
95+
96+
### Integration Tests
97+
98+
Integration tests are invoked using [Kitchen][kitchen-site],
99+
[Kitchen-Terraform][kitchen-terraform-site], and [InSpec][inspec-site].
100+
101+
Kitchen instances are configured in the
102+
[Kitchen configuration file](kitchen.yml). The instances use the modules
103+
in [fixtures directory](test/fixtures) to invoke identically named
104+
modules in the [examples directory](examples) and test this module.
105+
106+
#### Integration Tests Configuration
107+
108+
Each Kitchen instance requires a variable file named `terraform.tfvars`
109+
to be created and populated in the associated test fixture. For
110+
convenience, a [sample variable file][sameple-variable-file] is
111+
available.
112+
113+
A key file for a Service Account with the required
114+
[IAM roles](#iam-roles) must be downloaded from the GCP console and
115+
placed in the root directory of this repository. The key file must be
116+
renamed to `credentials.json`.
117+
118+
#### Integration Tests Execution
119+
120+
Run `make test_integration_docker` to execute all of the Kitchen
121+
instances in a non-interactive manner within a Docker container.
122+
123+
Alternatively, the Kitchen instances can be invoked interactively:
124+
125+
1. Run `make docker_run` to start the Docker container. The root
126+
directory of this repository will be mounted in the Docker container
127+
at `/cft/workdir/`.
128+
1. Run `kitchen create` to initialize all Kitchen instances, or run
129+
`kitchen create <INSTANCE_NAME>` to initialize a specific Kitchen
130+
instance.
131+
1. Run `kitchen converge` to apply all Kitchen instances, or run
132+
`kitchen converge <INSTANCE_NAME>` to apply a specific Kitchen
133+
instance.
134+
1. Run `kitchen verify` to test all Kitchen instances, or run
135+
`kitchen verify <INSTANCE_NAME>` to test a specific Kitchen instance.
136+
1. Run `kitchen destroy` to destroy all Kitchen instances, or run
137+
`kitchen destroy <INSTANCE_NAME>` to destroy a specific Kitchen
138+
instance.
139+
140+
## Linting
141+
142+
Linters are available for most of the filetypes in this repository.
143+
144+
### Linting Software Dependencies
145+
146+
The following software dependencies must be installed on the system
147+
from which the linting will be invoked:
148+
149+
- [flake8][flake8-site].
150+
- [ShellCheck][shellcheck-site].
151+
- [terrafom validate][terraform-validate-site].
152+
153+
### Linting Execution
154+
155+
Run `make check --silent` to execute all of the linters.
156+
157+
Alternatively, the linters can be invoked individually.
158+
159+
- Run `make check_python` to lint Python files.
160+
- Run `make check_shell` to lint Shell files.
161+
- Run `make check_terraform` to lint Terraform files.
162+
163+
## Documentation
164+
165+
The documentation of inputs and outputs for modules in this repository
166+
is automatically generated in each module's `README.md` based on the
167+
contents of the relevant `.tf` files.
168+
169+
### Documentation Software Dependencies
170+
171+
The following software dependencies must be installed on the system
172+
from which the documentation will be generated:
173+
174+
- [terraform-docs][terraform-docs-site] v0.6.0
175+
176+
### Generation
177+
178+
Run `make generate_docs` to update the documentation.
179+
180+
[bundler-site]: https://bundler.io/
181+
[flake8-site]: https://pypi.org/project/flake8/
182+
[gofmt-site]: https://golang.org/cmd/gofmt/
183+
[hadolint-site]: https://github.com/hadolint/hadolint/
184+
[inspec-site]: https://inspec.io/
185+
[kitchen-site]: https://kitchen.ci/
186+
[kitchen-terraform-site]: https://github.com/newcontext-oss/kitchen-terraform/
187+
[project-factory-module-site]: https://github.com/terraform-google-modules/terraform-google-project-factory/
188+
[ruby-site]: https://ruby-lang.org/
189+
[sample-variable-file]: test/fixtures/shared/terraform.tfvars.sample
190+
[shellcheck-site]: https://www.shellcheck.net/
191+
[terraform-docs-site]: https://github.com/segmentio/terraform-docs/releases/
192+
[terraform-provider-google-site]: https://github.com/terraform-providers/terraform-provider-google/
193+
[terraform-site]: https://www.terraform.io/
194+
[terraform-validate-site]: https://www.terraform.io/docs/commands/validate.html

main.tf

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 "google_pubsub_topic" "main" {
18+
name = "${var.name}"
19+
project = "${var.project_id}"
20+
}
21+
22+
resource "google_logging_project_sink" "main" {
23+
name = "${var.name}"
24+
destination = "pubsub.googleapis.com/${google_pubsub_topic.main.id}"
25+
filter = "${var.log_export_filter}"
26+
project = "${var.project_id}"
27+
unique_writer_identity = true
28+
}
29+
30+
resource "google_pubsub_topic_iam_member" "main" {
31+
topic = "${google_pubsub_topic.main.name}"
32+
role = "roles/pubsub.publisher"
33+
member = "${google_logging_project_sink.main.writer_identity}"
34+
project = "${var.project_id}"
35+
}
36+
37+
resource "google_cloudfunctions_function" "main" {
38+
name = "${var.name}"
39+
source_archive_bucket = "${google_storage_bucket.main.name}"
40+
source_archive_object = "${google_storage_bucket_object.main.name}"
41+
description = "${var.function_description}"
42+
available_memory_mb = "${var.function_available_memory_mb}"
43+
timeout = "${var.function_timeout_s}"
44+
entry_point = "${var.function_entry_point}"
45+
46+
event_trigger {
47+
event_type = "google.pubsub.topic.publish"
48+
resource = "${google_pubsub_topic.main.name}"
49+
50+
failure_policy {
51+
retry = "${var.function_event_trigger_failure_policy_retry}"
52+
}
53+
}
54+
55+
labels = "${var.function_labels}"
56+
runtime = "${var.function_runtime}"
57+
environment_variables = "${var.function_environment_variables}"
58+
project = "${var.project_id}"
59+
region = "${var.region}"
60+
}
61+
62+
data "archive_file" "main" {
63+
type = "zip"
64+
output_path = "${pathexpand("${var.function_source_directory}.zip")}"
65+
source_dir = "${pathexpand("${var.function_source_directory}")}"
66+
}
67+
68+
resource "google_storage_bucket" "main" {
69+
name = "${var.name}"
70+
force_destroy = "true"
71+
location = "${var.region}"
72+
project = "${var.project_id}"
73+
storage_class = "REGIONAL"
74+
labels = "${var.function_source_archive_bucket_labels}"
75+
}
76+
77+
resource "google_storage_bucket_object" "main" {
78+
name = "event_function.zip"
79+
bucket = "${google_storage_bucket.main.name}"
80+
source = "${data.archive_file.main.output_path}"
81+
content_disposition = "attachment"
82+
content_encoding = "gzip"
83+
content_type = "application/zip"
84+
}

outputs.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+

test/integration/automatic_labelling/inspec.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,3 @@ attributes:
2828
type: string
2929
required: true
3030
description: "The zone in which resources are applied."
31-

0 commit comments

Comments
 (0)