|
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 |
0 commit comments