├── .github └── workflows │ └── ci.yaml ├── .gitignore ├── .terraform-docs.yml ├── .terraform.lock.hcl ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── images ├── logging_export_ops_dashboard.png └── logging_export_to_splunk.png ├── main.tf ├── monitoring.tf ├── network.tf ├── outputs.tf ├── permissions.tf ├── pipeline.tf ├── replay.tf ├── sample.tfvars ├── variables.tf └── versions.tf /.github/workflows/ci.yaml: -------------------------------------------------------------------------------- 1 | name: Check quality gates 2 | on: 3 | push: 4 | branches: [ main ] 5 | pull_request: 6 | branches: [ main ] 7 | 8 | jobs: 9 | tflint: 10 | name: Run TFlint 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v2 15 | name: Checkout source code 16 | 17 | - uses: actions/cache@v2 18 | name: Cache plugin dir 19 | with: 20 | path: ~/.tflint.d/plugins 21 | key: ubuntu-latest-tflint-${{ hashFiles('.tflint.hcl') }} 22 | 23 | - uses: terraform-linters/setup-tflint@v2 24 | name: Setup TFLint 25 | with: 26 | tflint_version: v0.42.2 27 | 28 | - name: Show version 29 | run: tflint --version 30 | 31 | - name: Init TFLint 32 | run: tflint --init 33 | 34 | - name: Run TFLint 35 | run: tflint -f compact 36 | fmt: 37 | name: Run Terraform format 38 | runs-on: ubuntu-latest 39 | 40 | steps: 41 | - uses: actions/checkout@v2 42 | name: Checkout source code 43 | 44 | - uses: hashicorp/setup-terraform@v2 45 | with: 46 | terraform_version: 1.1.7 47 | name: Install Terraform 48 | 49 | - name: Run terraform fmt 50 | run: terraform fmt -check=true -write=false 51 | docs: 52 | name: Validate docs 53 | runs-on: ubuntu-latest 54 | steps: 55 | - uses: actions/checkout@v2 56 | name: Checkout source code 57 | 58 | - name: Render terraform docs inside the README.md and fail if changes are presented 59 | uses: terraform-docs/gh-actions@v1.0.0 60 | with: 61 | working-dir: . 62 | config-file: .terraform-docs.yml 63 | fail-on-diff: "true" 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .terraform 2 | terraform.tfvars* 3 | terraform.tfstate* 4 | 5 | *.swp 6 | -------------------------------------------------------------------------------- /.terraform-docs.yml: -------------------------------------------------------------------------------- 1 | formatter: markdown table 2 | output: 3 | file: README.md 4 | mode: inject 5 | template: |- 6 | 7 | {{ .Content }} 8 | 9 | sort: 10 | enabled: true 11 | by: required 12 | content: |- 13 | {{ .Inputs }} 14 | {{ .Outputs }} 15 | settings: 16 | indent: 4 17 | escape: false 18 | default: true 19 | required: true 20 | html: true 21 | type: true 22 | -------------------------------------------------------------------------------- /.terraform.lock.hcl: -------------------------------------------------------------------------------- 1 | # This file is maintained automatically by "terraform init". 2 | # Manual edits may be lost in future updates. 3 | 4 | provider "registry.terraform.io/hashicorp/google" { 5 | version = "4.52.0" 6 | constraints = ">= 3.54.0" 7 | hashes = [ 8 | "h1:Nlb8C/liQ7fnT4sNXSjTGxTlwzw/w28K96kp3TI7DWo=", 9 | "zh:1e36ac7c1be43cbb1d05fe1e5497dd058d1d87e03f848fd200bb043b1d6883f9", 10 | "zh:40a023e6e2f4f3137a156ba368815de5294be1c9d8049945768211d1868a5e0c", 11 | "zh:4bb0e3e97c95b32de5ccba13c3092fd929b95a646245ebe6829bb014863d8da0", 12 | "zh:59d11a98a80105273511343321b1d1af7a9e39c8474a6563761c8e7e8075dbb3", 13 | "zh:5c080d5368e898625f640a3397e8ab5f565a2a0c965642ecc124098f42c343a8", 14 | "zh:72fd8356a90941ec6e2ec1e838b00c1946eba7aabfe9fb25e32258e49d42e368", 15 | "zh:750f28b778dfecb0bed1d0b4df40a5e91fff16eb3ca4de64bed7c9945fea4e36", 16 | "zh:7e28085c105b96687bf6491f72e9ca915496352749a7af1a65013f7e5e89452b", 17 | "zh:83c8ecd116387032f7c4a1d9d4d921529746ce6e81d0766c9a0780c0b2e12993", 18 | "zh:bfd2d4cb2daba199db49a33ff507c808a3c94a657e4597b8bd6f2de51dc7a729", 19 | "zh:d9fd24b3d492013bca0021f29bfb670b76f01df5fd45f62c61eec53adac1b47d", 20 | "zh:f569b65999264a9416862bca5cd2a6177d94ccb0424f3a4ef424428912b9cb3c", 21 | ] 22 | } 23 | 24 | provider "registry.terraform.io/hashicorp/random" { 25 | version = "3.4.3" 26 | constraints = ">= 2.1.0" 27 | hashes = [ 28 | "h1:xZGZf18JjMS06pFa4NErzANI98qi59SEcBsOcS2P2yQ=", 29 | "zh:41c53ba47085d8261590990f8633c8906696fa0a3c4b384ff6a7ecbf84339752", 30 | "zh:59d98081c4475f2ad77d881c4412c5129c56214892f490adf11c7e7a5a47de9b", 31 | "zh:686ad1ee40b812b9e016317e7f34c0d63ef837e084dea4a1f578f64a6314ad53", 32 | "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", 33 | "zh:84103eae7251384c0d995f5a257c72b0096605048f757b749b7b62107a5dccb3", 34 | "zh:8ee974b110adb78c7cd18aae82b2729e5124d8f115d484215fd5199451053de5", 35 | "zh:9dd4561e3c847e45de603f17fa0c01ae14cae8c4b7b4e6423c9ef3904b308dda", 36 | "zh:bb07bb3c2c0296beba0beec629ebc6474c70732387477a65966483b5efabdbc6", 37 | "zh:e891339e96c9e5a888727b45b2e1bb3fcbdfe0fd7c5b4396e4695459b38c8cb1", 38 | "zh:ea4739860c24dfeaac6c100b2a2e357106a89d18751f7693f3c31ecf6a996f8d", 39 | "zh:f0c76ac303fd0ab59146c39bc121c5d7d86f878e9a69294e29444d4c653786f8", 40 | "zh:f143a9a5af42b38fed328a161279906759ff39ac428ebcfe55606e05e1518b93", 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | The project is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and [Semantic Versioning](https://semver.org/). All notable changes to this project will be documented in this file. 4 | 5 | 6 | ## [Unreleased] - 202X-XX-XX 7 | 8 | ### Added 9 | 10 | - Option `use_externally_managed_dataflow_sa` to be able to use pre-existing externally-managed service account as Dataflow worker service account. User is expected to apply and manage IAM permissions over external resources (e.g. Cloud KMS key or Secret version) outside of this module. 11 | - Enable uniform bucket-level access for the temporary Cloud Storage bucket used by Dataflow pipeline. This change disables ACLs for individual objects in this bucket, in favor for a uniform access control over entire bucket and IAM-based security features (e.g. Domain Restricted Sharing, IAM Conditions). 12 | 13 | ### Removed 14 | 15 | - `providers` configuration because it prevents usage of this module with `count` and `for_each`. This module now inherits the default provider configuration from the caller, thereby simplifying module re-usability. 16 | 17 | ### Fixed 18 | 19 | - Removed hardcoded project numbers in log panels in dashboard's JSON description 20 | 21 | 22 | ## [v1.0.0] - 2022-12-20 23 | 24 | - Initial version 25 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to Contribute 2 | 3 | We'd love to accept your patches and contributions to this project. There are 4 | just a few small guidelines you need to follow. 5 | 6 | ## Contributor License Agreement 7 | 8 | Contributions to this project must be accompanied by a Contributor License 9 | Agreement. You (or your employer) retain the copyright to your contribution; 10 | this simply gives us permission to use and redistribute your contributions as 11 | part of the project. Head over to to see 12 | your current agreements on file or to sign a new one. 13 | 14 | You generally only need to submit a CLA once, so if you've already submitted one 15 | (even if it was for a different project), you probably don't need to do it 16 | again. 17 | 18 | ## Code Reviews 19 | 20 | All submissions, including submissions by project members, require review. We 21 | use GitHub pull requests for this purpose. Consult 22 | [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more 23 | information on using pull requests. 24 | 25 | ## Quality gates 26 | The project has several Quality gates. 27 | Make sure to run them localy before raising PR. 28 | 1. Run [tflint](https://github.com/terraform-linters/tflint) with default settings 29 | ``` 30 | tflint . 31 | ``` 32 | 2. Run terraform format 33 | ``` 34 | terraform fmt . 35 | ``` 36 | 3. Generate documentation with help of [terraform-docs](https://terraform-docs.io/) 37 | ``` 38 | terraform-docs . 39 | ``` 40 | 41 | ## Community Guidelines 42 | 43 | This project follows [Google's Open Source Community 44 | Guidelines](https://opensource.google/conduct/). -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Terraform templates for Google Cloud log export to Splunk 2 | 3 | Terraform scripts for deploying log export to Splunk per Google Cloud reference guide:
4 | [Deploying production-ready log exports to Splunk using Dataflow](https://cloud.google.com/architecture/deploying-production-ready-log-exports-to-splunk-using-dataflow) 5 | . 6 | 7 | Resources created include an optional [Cloud Monitoring custom dashboard](#monitoring-dashboard-batteries-included) to monitor your log export operations. For more details on custom metrics in Splunk Dataflow template, see [New observability features for your Splunk Dataflow streaming pipelines](https://cloud.google.com/blog/products/data-analytics/simplify-your-splunk-dataflow-ops-with-improved-pipeline-observability). 8 | 9 | These deployment templates are provided as is, without warranty. See [Copyright & License](#copyright-&-license) below. 10 | 11 | ### Architecture Diagram 12 | 13 | ![Architecture Diagram of Log Export to Splunk](./images/logging_export_to_splunk.png) 14 | 15 | ### Terraform Module 16 | 17 | #### Inputs 18 | 19 | | Name | Description | Type | Default | Required | 20 | |------|-------------|------|---------|:--------:| 21 | | [dataflow_job_name](#input_dataflow_job_name) | Dataflow job name. No spaces | `string` | n/a | yes | 22 | | [log_filter](#input_log_filter) | Log filter to use when exporting logs | `string` | n/a | yes | 23 | | [network](#input_network) | Network to deploy into | `string` | n/a | yes | 24 | | [project](#input_project) | Project ID to deploy resources in | `string` | n/a | yes | 25 | | [region](#input_region) | Region to deploy regional-resources into. This must match subnet's region if deploying into existing network (e.g. Shared VPC). See `subnet` parameter below | `string` | n/a | yes | 26 | | [splunk_hec_url](#input_splunk_hec_url) | Splunk HEC URL to write data to. Example: https://[MY_SPLUNK_IP_OR_FQDN]:8088 | `string` | n/a | yes | 27 | | [create_network](#input_create_network) | Boolean value specifying if a new network needs to be created. | `bool` | `false` | no | 28 | | [dataflow_job_batch_count](#input_dataflow_job_batch_count) | Batch count of messages in single request to Splunk | `number` | `50` | no | 29 | | [dataflow_job_disable_certificate_validation](#input_dataflow_job_disable_certificate_validation) | Boolean to disable SSL certificate validation | `bool` | `false` | no | 30 | | [dataflow_job_machine_count](#input_dataflow_job_machine_count) | Dataflow job max worker count | `number` | `2` | no | 31 | | [dataflow_job_machine_type](#input_dataflow_job_machine_type) | Dataflow job worker machine type | `string` | `"n1-standard-4"` | no | 32 | | [dataflow_job_parallelism](#input_dataflow_job_parallelism) | Maximum parallel requests to Splunk | `number` | `8` | no | 33 | | [dataflow_job_udf_function_name](#input_dataflow_job_udf_function_name) | Name of JavaScript function to be called | `string` | `""` | no | 34 | | [dataflow_job_udf_gcs_path](#input_dataflow_job_udf_gcs_path) | GCS path for JavaScript file | `string` | `""` | no | 35 | | [dataflow_template_version](#input_dataflow_template_version) | Dataflow template release version (default 'latest'). Override this for version pinning e.g. '2021-08-02-00_RC00'. Must specify version only since template GCS path will be deduced automatically: 'gs://dataflow-templates/`version`/Cloud_PubSub_to_Splunk' | `string` | `"latest"` | no | 36 | | [dataflow_worker_service_account](#input_dataflow_worker_service_account) | Name of Dataflow worker service account to be created and used to execute job operations. In the default case of creating a new service account (`use_externally_managed_dataflow_sa=false`), this parameter must be 6-30 characters long, and match the regular expression [a-z]([-a-z0-9]*[a-z0-9]). If the parameter is empty, worker service account defaults to project's Compute Engine default service account. If using external service account (`use_externally_managed_dataflow_sa=true`), this parameter must be the full email address of the external service account. | `string` | `""` | no | 37 | | [deploy_replay_job](#input_deploy_replay_job) | Determines if replay pipeline should be deployed or not | `bool` | `false` | no | 38 | | [gcs_kms_key_name](#input_gcs_kms_key_name) | Cloud KMS key resource ID, to be used as default encryption key for the temporary storage bucket used by the Dataflow job.
If set, make sure to pre-authorize Cloud Storage service agent associated with that bucket to use that key for encrypting and decrypting. | `string` | `""` | no | 39 | | [primary_subnet_cidr](#input_primary_subnet_cidr) | The CIDR Range of the primary subnet | `string` | `"10.128.0.0/20"` | no | 40 | | [scoping_project](#input_scoping_project) | Cloud Monitoring scoping project ID to create dashboard under.
This assumes a pre-existing scoping project whose metrics scope contains the `project` where dataflow job is to be deployed.
See [Cloud Monitoring settings](https://cloud.google.com/monitoring/settings) for more details on scoping project.
If parameter is empty, scoping project defaults to value of `project` parameter above. | `string` | `""` | no | 41 | | [splunk_hec_token](#input_splunk_hec_token) | Splunk HEC token. Must be defined if `splunk_hec_token_source` if type of `PLAINTEXT` or `KMS`. | `string` | `""` | no | 42 | | [splunk_hec_token_kms_encryption_key](#input_splunk_hec_token_kms_encryption_key) | The Cloud KMS key to decrypt the HEC token string. Required if `splunk_hec_token_source` is type of KMS | `string` | `""` | no | 43 | | [splunk_hec_token_secret_id](#input_splunk_hec_token_secret_id) | Id of the Secret for Splunk HEC token. Required if `splunk_hec_token_source` is type of SECRET_MANAGER | `string` | `""` | no | 44 | | [splunk_hec_token_source](#input_splunk_hec_token_source) | Define in which type HEC token is provided. Possible options: [PLAINTEXT, KMS, SECRET_MANAGER]. | `string` | `"PLAINTEXT"` | no | 45 | | [subnet](#input_subnet) | Subnet to deploy into. This is required when deploying into existing network (`create_network=false`) (e.g. Shared VPC) | `string` | `""` | no | 46 | | [use_externally_managed_dataflow_sa](#input_use_externally_managed_dataflow_sa) | Determines if the worker service account provided by `dataflow_worker_service_account` variable should be created by this module (default) or is managed outside of the module. In the latter case, user is expected to apply and manage the service account IAM permissions over external resources (e.g. Cloud KMS key or Secret version) before running this module. | `bool` | `false` | no | 47 | #### Outputs 48 | 49 | | Name | Description | 50 | |------|-------------| 51 | | [dataflow_input_topic](#output_dataflow_input_topic) | n/a | 52 | | [dataflow_job_id](#output_dataflow_job_id) | n/a | 53 | | [dataflow_log_export_dashboard](#output_dataflow_log_export_dashboard) | n/a | 54 | | [dataflow_output_deadletter_subscription](#output_dataflow_output_deadletter_subscription) | n/a | 55 | 56 | 57 | ### Monitoring Dashboard (Batteries Included) 58 | 59 | Deployment templates include an optional Cloud Monitoring custom dashboard to monitor your log export operations: 60 | ![Ops Dashboard of Log Export to Splunk](./images/logging_export_ops_dashboard.png) 61 | 62 | ### Permissions Required 63 | 64 | At a minimum, you must have the following roles before you deploy the resources in this Terraform: 65 | - Logs Configuration Writer (`roles/logging.configWriter`) at the project and/or organization level 66 | - Compute Network Admin (`roles/compute.networkAdmin`) at the project level 67 | - Compute Security Admin (`roles/compute.securityAdmin`) at the project level 68 | - Dataflow Admin (`roles/dataflow.admin`) at the project level 69 | - Pub/Sub Admin (`roles/pubsub.admin`) at the project level 70 | - Storage Admin (`roles/storage.admin`) at the project level 71 | 72 | To ensure proper pipeline operation, Terraform creates necessary IAM bindings at the resources level as part of this deployment to grant access between newly created resources. For example, Log sink writer is granted Pub/Sub Publisher role over the input topic which collects all the logs, and Dataflow worker service account is granted both Pub/Sub subscriber over the input subscription, and Pub/Sub Publisher role over the deadletter topic. 73 | 74 | #### Dataflow permissions 75 | 76 | The Dataflow worker service service account is the identity used by the Dataflow worker VMs. This module offers three options in terms of which worker service account to use and how to manage their IAM permissions: 77 | 78 | 1. Module uses your project's [Compute Engine default service account](https://cloud.google.com/compute/docs/access/service-accounts#default_service_account) as Dataflow worker service account, and manages any required IAM permissions. The module grants that service account necessary IAM roles such as `roles/dataflow.worker` and IAM permissions over Google Cloud resources required by the job such as Pub/Sub, Cloud Storage, and secret or KMS if applicable. This is the **default behavior**. 79 | 80 | 2. Module creates a dedicated service account to be used as Dataflow worker service account, and manages any required IAM permissions. The module grants that service account necessary IAM roles such as `roles/dataflow.worker` and IAM permissions over Google Cloud resources required by the job such as Pub/Sub, Cloud Storage, and secret or KMS key if applicable. To use this option, set `dataflow_worker_service_account` to the name of this new service account. 81 | 82 | 3. Module uses a service account managed outside of the module. The module grants that service account necessary IAM permissions over Google Cloud resources created by the module such as Pub/Sub, Cloud Storage. You must grant this service account the required IAM roles (`roles/dataflow.worker` ) and IAM permissions over external resources such as any provided secret or KMS key (more below), _before_ running this module. To use this option, set `use_externally_managed_dataflow_sa` to `true` and set `dataflow_worker_service_account` to the email address of this external service account. 83 | 84 | For production workloads, as a security best practice, it's recommended to use option 2 or 3, both of which rely on user-managed worker service account, instead of the Compute Engine default service account. This ensures a minimally-scoped service account dedicated for this pipeline. 85 | 86 | For option 3, make sure to grant: 87 | - The provided Dataflow service account the following roles: 88 | - `roles/dataflow.worker` 89 | - `roles/secretmanager.secretAccessor` on secret - if `SECRET_MANAGER` HEC token source is used 90 | - `roles/cloudkms.cryptoKeyDecrypter` on KMS key- if `KMS` HEC token source is used 91 | 92 | For option 1 & 3, make sure to grant: 93 | - Your user account or service account used to run Terraform the following role: 94 | - `roles/iam.serviceAccountUser` on the Dataflow service account in order to impersonate the service account. See following note for more details. 95 | 96 | **Note about Dataflow worker service account impersonation**: To run this Terraform module, you must have permission to impersonate the Dataflow worker service account in order to attach that service account to Dataflow worker VMs. In case of the default Dataflow worker service account (Option 1), ensure you have `iam.serviceAccounts.actAs` permission over Compute Engine default service account in your project. For security purposes, this Terraform does not modify access to your existing Compute Engine default service account due to risk of granting broad permissions. On the other hand, if you choose to create and use a user-managed worker service account (Option 2) by setting `dataflow_worker_service_account` (and keeping `use_externally_managed_dataflow_sa` = `false`), this Terraform will add necessary impersonation permission over the new service account. 97 | 98 | See [Security and permissions for pipelines](https://cloud.google.com/dataflow/docs/concepts/security-and-permissions#security_and_permissions_for_pipelines_on) to learn more about Dataflow service accounts and their permissions. 99 | 100 | ### Getting Started 101 | 102 | #### Requirements 103 | * Terraform 0.13+ 104 | * Splunk Dataflow template 2022-04-25-00_RC00 or later 105 | 106 | #### Enabling APIs 107 | Before deploying the Terraform in a Google Cloud Platform Project, the following APIs must be enabled: 108 | * Compute Engine API 109 | * Dataflow API 110 | 111 | For information on enabling Google Cloud Platform APIs, please see [Getting Started: Enabling APIs](https://cloud.google.com/apis/docs/getting-started#enabling_apis). 112 | 113 | #### Setup working directory 114 | 115 | 1. Copy placeholder vars file `sample.tfvars` into new `terraform.tfvars` to hold your own settings. 116 | 2. Update placeholder values in `terraform.tfvars` to correspond to your GCP environment and desired settings. See [list of input parameters](#configurable-parameters) above. 117 | 3. Initialize Terraform working directory and download plugins by running: 118 | 119 | ```shell 120 | $ terraform init 121 | ``` 122 | 123 | #### Authenticate with GCP 124 | Note: You can skip this step if this module is inheriting the Terraform Google provider (e.g. from a parent module) with pre-configured credentials. 125 | 126 | ```shell 127 | $ gcloud auth application-default login --project 128 | ``` 129 | 130 | This assumes you are running Terraform on your workstation with your own identity. For other methods to authenticate such as using a Terraform-specific service account, see [Google Provider authentication docs](https://registry.terraform.io/providers/hashicorp/google/latest/docs/guides/provider_reference#authentication). 131 | 132 | #### Deploy log export pipeline 133 | 134 | ```shell 135 | $ terraform plan 136 | $ terraform apply 137 | ``` 138 | 139 | #### View log export monitoring dashboard 140 | 141 | 1. Retrieve dashboard id from terraform output 142 | ```shell 143 | $ terraform output dataflow_log_export_dashboard 144 | ``` 145 | The output is of the form `"projects/{project_id_or_number}/dashboards/{dashboard_id}"`. 146 | 147 | Take note of dashboard_id value. 148 | 149 | 2. Visit newly created Monitoring Dashboard in Cloud Console by replacing dashboard_id in the following URL: https://console.cloud.google.com/monitoring/dashboards/builder/{dashboard_id} 150 | 151 | #### Deploy replay pipeline when needed 152 | 153 | The replay pipeline is not deployed by default; instead it is only used to move failed messages from the PubSub deadletter subscription back to the input topic, in order to be redelivered by the main log export pipeline (as depicted in above [diagram](#architecture-diagram)). Refer to [Handling delivery failures](https://cloud.google.com/architecture/deploying-production-ready-log-exports-to-splunk-using-dataflow#handling_delivery_failures) for more detail. 154 | 155 | **Caution**: Make sure to deploy replay pipeline only after the root cause of the delivery failure has been fixed. Otherwise, the replay pipeline will cause an infinite loop where failed messages are sent back for re-delivery, only to fail again, causing an infinite loop and wasted resources. For that same reason, make sure to tear down the replay pipeline once the failed messages from the deadletter subscription are all processed or replayed. 156 | 157 | 1. To deploy replay pipeline, set `deploy_replay_job` variable to `true`, then follow the sequence of `terraform plan` and `terraform apply`. 158 | 2. Once the replay pipeline is no longer needed (i.e. the number of messages in the PubSub deadletter subscription is 0), set `deploy_replay_job` variable to `false`, then follow the sequence of `terraform plan` and `terraform apply`. 159 | 160 | ### Cleanup 161 | 162 | To delete resources created by Terraform, run the following then confirm: 163 | ``` shell 164 | $ terraform destroy 165 | ``` 166 | 167 | ### Using customer-managed encryption keys (CMEK) 168 | 169 | For those who require CMEK, this module accepts CMEK keys for the following services: 170 | - Cloud Storage: see `gcs_kms_key_name` input parameter. You are responsible for granting Cloud Storage service agent the role Cloud KMS CryptoKey Encrypter/Decrypter (`roles/cloudkms.cryptoKeyEncrypterDecrypter`) in order to use the provided Cloud KMS key for encrypting and decrypting objects in the temporary storage bucket. The Cloud KMS key must be available in the location that the temporary bucket is created in (specified in `var.region`). For more details, see [Use customer-managed encryption keys](https://cloud.google.com/storage/docs/encryption/using-customer-managed-keys) in Cloud Storage docs. 171 | 172 | ### Authors 173 | 174 | * **Roy Arsan** - [rarsan](https://github.com/rarsan) 175 | * **Nick Predey** - [npredey](https://github.com/npredey) 176 | * **Igor Lakhtenkov** - [Lakhtenkov-iv](https://github.com/Lakhtenkov-iv) 177 | 178 | ### Copyright & License 179 | 180 | Copyright 2021 Google LLC 181 | 182 | Terraform templates for Google Cloud Log Export to Splunk are licensed under the Apache license, v2.0. Details can be found in [LICENSE](./LICENSE) file. 183 | -------------------------------------------------------------------------------- /images/logging_export_ops_dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/terraform-splunk-log-export/123d101f228ac90062635b7ee14fab8df4c46fc6/images/logging_export_ops_dashboard.png -------------------------------------------------------------------------------- /images/logging_export_to_splunk.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoogleCloudPlatform/terraform-splunk-log-export/123d101f228ac90062635b7ee14fab8df4c46fc6/images/logging_export_to_splunk.png -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | data "google_project" "project" { 16 | project_id = var.project 17 | } 18 | 19 | data "google_client_openid_userinfo" "provider_identity" {} 20 | 21 | # Generate new random hex to be used for bucket name 22 | resource "random_id" "bucket_suffix" { 23 | byte_length = 4 24 | } 25 | 26 | # Generate new random id each time we switch to a new template version id, 27 | # to be used for pipeline job name to force job replacement vs in-place update 28 | resource "random_id" "dataflow_job_instance" { 29 | byte_length = 2 30 | keepers = { 31 | dataflow_template_version = var.dataflow_template_version 32 | } 33 | } 34 | 35 | locals { 36 | dataflow_temporary_gcs_bucket_name = "${var.project}-${var.dataflow_job_name}-${random_id.bucket_suffix.hex}" 37 | dataflow_temporary_gcs_bucket_path = "tmp/" 38 | 39 | dataflow_splunk_template_gcs_path = "gs://dataflow-templates/${var.dataflow_template_version}/Cloud_PubSub_to_Splunk" 40 | dataflow_pubsub_template_gcs_path = "gs://dataflow-templates/${var.dataflow_template_version}/Cloud_PubSub_to_Cloud_PubSub" 41 | 42 | # If provided, set Dataflow worker to new user-managed service account; 43 | # otherwise, use Compute Engine default service account 44 | dataflow_worker_service_account = ((var.dataflow_worker_service_account != "") ? 45 | ((var.use_externally_managed_dataflow_sa == false) ? 46 | google_service_account.dataflow_worker_service_account[0].email : 47 | var.dataflow_worker_service_account) : 48 | "${data.google_project.project.number}-compute@developer.gserviceaccount.com") 49 | 50 | # Cross validate Dataflow service account variables 51 | # TODO: Remove once https://github.com/hashicorp/terraform/issues/25609 is resolved or use pre-conditions if version > 1.2.0 52 | # tflint-ignore: terraform_unused_declarations 53 | validate_sa_settings = (var.use_externally_managed_dataflow_sa && var.dataflow_worker_service_account == "") ? tobool("Please pass dataflow_worker_service_account if you want to manage it externally.") : true 54 | # Grant Dataflow worker service account required IAM roles over external resources 55 | # **unless** using an externally managed worker service account (Option 1 and 2): 56 | grant_service_account_roles = var.use_externally_managed_dataflow_sa == false 57 | # Grant caller (you!) permissions to impersonate service account 58 | # **only** when creating a new worker service account (Option 2) 59 | grant_service_account_impersonation = ( 60 | var.dataflow_worker_service_account != "" && var.use_externally_managed_dataflow_sa == false 61 | ) 62 | 63 | subnet_name = coalesce(var.subnet, "${var.network}-${var.region}") 64 | project_log_sink_name = "${var.dataflow_job_name}-project-log-sink" 65 | # tflint-ignore: terraform_unused_declarations 66 | organization_log_sink_name = "${var.dataflow_job_name}-organization-log-sink" 67 | 68 | dataflow_main_job_name = "${var.dataflow_job_name}-main-${random_id.dataflow_job_instance.hex}" 69 | dataflow_replay_job_name = "${var.dataflow_job_name}-replay-${random_id.dataflow_job_instance.hex}" 70 | 71 | dataflow_input_topic_name = "${var.dataflow_job_name}-input-topic" 72 | dataflow_input_subscription_name = "${var.dataflow_job_name}-input-subscription" 73 | dataflow_output_deadletter_topic_name = "${var.dataflow_job_name}-deadletter-topic" 74 | dataflow_output_deadletter_sub_name = "${var.dataflow_job_name}-deadletter-subscription" 75 | # Store HEC token secret and secret version IDs in two separate local values. 76 | # This is needed to disambiguate between the two given that the Dataflow template expects 77 | # secret version ID whereas the IAM policy binding expects parent secret ID 78 | splunk_hec_token_secret_version_id = var.splunk_hec_token_secret_id 79 | # Infer secret ID from input which is actually the secret *version* ID 80 | splunk_hec_token_secret_id = ((var.splunk_hec_token_secret_id != "") 81 | ? regex("^(projects\\/[^\\n\\r\\/]+\\/secrets\\/[^\\n\\r\\/]+)\\/versions\\/[^\\n\\r\\/]+$", var.splunk_hec_token_secret_id)[0] 82 | : "") 83 | 84 | # Dataflow job parameters (not externalized for this project) 85 | dataflow_job_include_pubsub_message = true 86 | dataflow_job_enable_batch_logs = false 87 | dataflow_job_enable_gzip_http_compression = true 88 | 89 | # Metrics scope for Monitoring dashboard defaults to project unless explicitly provided 90 | scoping_project = (var.scoping_project != "") ? var.scoping_project : var.project 91 | } 92 | 93 | resource "google_pubsub_topic" "dataflow_input_pubsub_topic" { 94 | project = var.project 95 | name = local.dataflow_input_topic_name 96 | } 97 | 98 | resource "google_pubsub_subscription" "dataflow_input_pubsub_subscription" { 99 | project = var.project 100 | name = local.dataflow_input_subscription_name 101 | topic = google_pubsub_topic.dataflow_input_pubsub_topic.name 102 | 103 | # messages retained for 7 days (max) 104 | message_retention_duration = "604800s" 105 | ack_deadline_seconds = 30 106 | 107 | # subscription never expires 108 | expiration_policy { 109 | ttl = "" 110 | } 111 | } 112 | 113 | resource "google_logging_project_sink" "project_log_sink" { 114 | project = var.project 115 | name = local.project_log_sink_name 116 | destination = "pubsub.googleapis.com/projects/${var.project}/topics/${google_pubsub_topic.dataflow_input_pubsub_topic.name}" 117 | filter = var.log_filter 118 | 119 | exclusions { 120 | name = "exclude_dataflow" 121 | description = "Exclude dataflow logs to not create an infinite loop" 122 | filter = "resource.type=\"dataflow_step\" AND resource.labels.job_name = \"${local.dataflow_main_job_name}\"" 123 | } 124 | 125 | unique_writer_identity = true 126 | } 127 | 128 | # resource "google_logging_organization_sink" "organization_log_sink" { 129 | # name = local.organization_log_sink_name 130 | # org_id = "ORGANIZATION_ID" 131 | # destination = "pubsub.googleapis.com/projects/${var.project}/topics/${google_pubsub_topic.dataflow_input_pubsub_topic.name}" 132 | # filter = var.log_filter 133 | # 134 | # include_children = "true" 135 | # } 136 | -------------------------------------------------------------------------------- /monitoring.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | resource "google_monitoring_group" "splunk-export-pipeline-group" { 16 | display_name = "Splunk Log Export Group" 17 | project = local.scoping_project 18 | 19 | filter = "resource.metadata.name=starts_with(\"${var.dataflow_job_name}\")" 20 | } 21 | 22 | resource "google_monitoring_dashboard" "splunk-export-pipeline-dashboard" { 23 | project = local.scoping_project 24 | dashboard_json = < 0 ? google_compute_router_nat.dataflow_nat[0].name : ""}\"", 805 | "secondaryAggregation": { 806 | "alignmentPeriod": "60s" 807 | } 808 | } 809 | } 810 | } 811 | ], 812 | "timeshiftDuration": "0s", 813 | "yAxis": { 814 | "label": "y1Axis", 815 | "scale": "LINEAR" 816 | } 817 | } 818 | } 819 | } 820 | ] 821 | } 822 | } 823 | 824 | EOF 825 | } 826 | -------------------------------------------------------------------------------- /network.tf: -------------------------------------------------------------------------------- 1 | resource "google_compute_network" "splunk_export" { 2 | count = var.create_network == true ? 1 : 0 3 | 4 | project = var.project 5 | name = var.network 6 | auto_create_subnetworks = false 7 | } 8 | 9 | resource "google_compute_subnetwork" "splunk_subnet" { 10 | count = var.create_network == true ? 1 : 0 11 | 12 | project = var.project 13 | name = local.subnet_name 14 | ip_cidr_range = var.primary_subnet_cidr 15 | region = var.region 16 | network = google_compute_network.splunk_export[count.index].id 17 | private_ip_google_access = true 18 | 19 | # Optional configuration to log network traffic at the subnet level 20 | # log_config { 21 | # aggregation_interval = "INTERVAL_15_MIN" 22 | # flow_sampling = 0.1 23 | # metadata = "INCLUDE_ALL_METADATA" 24 | # } 25 | 26 | } 27 | 28 | # Create DNS policy that enables logging of DNS queries in new network 29 | resource "google_dns_policy" "splunk_network_dns_policy" { 30 | count = var.create_network == true ? 1 : 0 31 | 32 | project = var.project 33 | name = "${var.network}-dns-policy" 34 | 35 | enable_logging = true 36 | 37 | networks { 38 | network_url = google_compute_network.splunk_export[count.index].id 39 | } 40 | } 41 | 42 | resource "google_compute_router" "dataflow_to_splunk_router" { 43 | count = var.create_network == true ? 1 : 0 44 | 45 | project = var.project 46 | name = "${var.network}-${var.region}-router" 47 | region = google_compute_subnetwork.splunk_subnet[count.index].region 48 | network = google_compute_network.splunk_export[count.index].id 49 | } 50 | 51 | resource "google_compute_address" "dataflow_nat_ip_address" { 52 | count = var.create_network == true ? 1 : 0 53 | 54 | project = var.project 55 | name = "dataflow-splunk-nat-ip-address" 56 | region = google_compute_subnetwork.splunk_subnet[count.index].region 57 | } 58 | 59 | resource "google_compute_router_nat" "dataflow_nat" { 60 | count = var.create_network == true ? 1 : 0 61 | 62 | project = var.project 63 | name = "${var.network}-${var.region}-router-nat" 64 | router = google_compute_router.dataflow_to_splunk_router[count.index].name 65 | region = google_compute_router.dataflow_to_splunk_router[count.index].region 66 | nat_ip_allocate_option = "MANUAL_ONLY" 67 | source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" 68 | nat_ips = google_compute_address.dataflow_nat_ip_address[*].self_link 69 | min_ports_per_vm = 128 70 | subnetwork { 71 | name = google_compute_subnetwork.splunk_subnet[count.index].id 72 | source_ip_ranges_to_nat = ["PRIMARY_IP_RANGE"] 73 | } 74 | 75 | log_config { 76 | enable = true 77 | filter = "ERRORS_ONLY" 78 | } 79 | } 80 | 81 | # Creating firewall rule so that dataflow jobs with > 1 worker can communicate over internal IPs. 82 | # Source: https://cloud.google.com/dataflow/docs/guides/routes-firewall#firewall_rules_required_by 83 | resource "google_compute_firewall" "connect_dataflow_workers" { 84 | count = var.create_network == true ? 1 : 0 85 | 86 | project = var.project 87 | name = "dataflow-internal-ip-fwr" 88 | network = google_compute_network.splunk_export[count.index].id 89 | 90 | allow { 91 | protocol = "tcp" 92 | ports = ["12345-12346"] 93 | } 94 | 95 | source_tags = ["dataflow"] 96 | target_tags = ["dataflow"] 97 | } 98 | -------------------------------------------------------------------------------- /outputs.tf: -------------------------------------------------------------------------------- 1 | output "dataflow_job_id" { 2 | value = google_dataflow_job.dataflow_job.job_id 3 | } 4 | 5 | output "dataflow_input_topic" { 6 | value = google_pubsub_topic.dataflow_input_pubsub_topic.name 7 | } 8 | 9 | output "dataflow_output_deadletter_subscription" { 10 | value = google_pubsub_subscription.dataflow_deadletter_pubsub_sub.name 11 | } 12 | 13 | output "dataflow_log_export_dashboard" { 14 | value = google_monitoring_dashboard.splunk-export-pipeline-dashboard.id 15 | } 16 | -------------------------------------------------------------------------------- /permissions.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2022 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 | # https://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 | resource "google_pubsub_topic_iam_binding" "input_sub_publisher" { 16 | project = google_pubsub_topic.dataflow_input_pubsub_topic.project 17 | topic = google_pubsub_topic.dataflow_input_pubsub_topic.name 18 | role = "roles/pubsub.publisher" 19 | members = [ 20 | google_logging_project_sink.project_log_sink.writer_identity 21 | ] 22 | } 23 | 24 | resource "google_pubsub_subscription_iam_binding" "input_sub_subscriber" { 25 | project = google_pubsub_subscription.dataflow_input_pubsub_subscription.project 26 | subscription = google_pubsub_subscription.dataflow_input_pubsub_subscription.name 27 | role = "roles/pubsub.subscriber" 28 | members = [ 29 | "serviceAccount:${local.dataflow_worker_service_account}" 30 | ] 31 | } 32 | 33 | resource "google_pubsub_subscription_iam_binding" "input_sub_viewer" { 34 | project = google_pubsub_subscription.dataflow_input_pubsub_subscription.project 35 | subscription = google_pubsub_subscription.dataflow_input_pubsub_subscription.name 36 | role = "roles/pubsub.viewer" 37 | members = [ 38 | "serviceAccount:${local.dataflow_worker_service_account}" 39 | ] 40 | } 41 | 42 | resource "google_pubsub_topic_iam_binding" "deadletter_topic_publisher" { 43 | project = google_pubsub_topic.dataflow_deadletter_pubsub_topic.project 44 | topic = google_pubsub_topic.dataflow_deadletter_pubsub_topic.name 45 | role = "roles/pubsub.publisher" 46 | members = [ 47 | "serviceAccount:${local.dataflow_worker_service_account}" 48 | ] 49 | } 50 | 51 | resource "google_storage_bucket_iam_binding" "dataflow_worker_bucket_access" { 52 | bucket = google_storage_bucket.dataflow_job_temp_bucket.name 53 | role = "roles/storage.objectAdmin" 54 | members = [ 55 | "serviceAccount:${local.dataflow_worker_service_account}" 56 | ] 57 | } 58 | 59 | resource "google_project_iam_binding" "dataflow_worker_role" { 60 | count = (local.grant_service_account_roles == true) ? 1 : 0 61 | project = var.project 62 | role = "roles/dataflow.worker" 63 | members = [ 64 | "serviceAccount:${local.dataflow_worker_service_account}" 65 | ] 66 | } 67 | 68 | resource "google_secret_manager_secret_iam_member" "dataflow_worker_secret_access" { 69 | count = (var.splunk_hec_token_source == "SECRET_MANAGER" && 70 | var.splunk_hec_token_secret_id != "" && 71 | local.grant_service_account_roles == true) ? 1 : 0 72 | project = var.project 73 | secret_id = local.splunk_hec_token_secret_id # Use secret ID inferred from input secret version ID 74 | role = "roles/secretmanager.secretAccessor" 75 | member = "serviceAccount:${local.dataflow_worker_service_account}" 76 | } 77 | 78 | resource "google_kms_crypto_key_iam_member" "dataflow_worker_kms_access" { 79 | count = (var.splunk_hec_token_source == "KMS" && 80 | var.splunk_hec_token_kms_encryption_key != "" && 81 | local.grant_service_account_roles == true) ? 1 : 0 82 | crypto_key_id = var.splunk_hec_token_kms_encryption_key 83 | role = "roles/cloudkms.cryptoKeyDecrypter" 84 | member = "serviceAccount:${local.dataflow_worker_service_account}" 85 | } 86 | 87 | # To deploy Dataflow jobs, terraform caller identity must have permission to impersonate 88 | # worker service account in order to attach that service account to Compute Engine VMs. 89 | # In case of user-managed worker service account, add necessary permission over new service account, 90 | # (id: google_service_account.dataflow_worker_service_account[0].id) 91 | # In case of default worker service account (i.e. Compute Engine default service account), caller 92 | # must have permission to impersonate Compute Engine default service account; otherwise, job 93 | # deployment will return an error. For security purposes, we do not modify access to existing 94 | # default Compute Engine service account 95 | resource "google_service_account_iam_binding" "terraform_caller_impersonate_dataflow_worker" { 96 | count = (local.grant_service_account_impersonation == true) ? 1 : 0 97 | service_account_id = google_service_account.dataflow_worker_service_account[0].id 98 | role = "roles/iam.serviceAccountUser" 99 | 100 | members = [ 101 | "user:${data.google_client_openid_userinfo.provider_identity.email}" 102 | ] 103 | } 104 | -------------------------------------------------------------------------------- /pipeline.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | 16 | resource "google_pubsub_topic" "dataflow_deadletter_pubsub_topic" { 17 | project = var.project 18 | name = local.dataflow_output_deadletter_topic_name 19 | } 20 | 21 | resource "google_pubsub_subscription" "dataflow_deadletter_pubsub_sub" { 22 | project = var.project 23 | name = local.dataflow_output_deadletter_sub_name 24 | topic = google_pubsub_topic.dataflow_deadletter_pubsub_topic.name 25 | 26 | # messages retained for 7 days (max) 27 | message_retention_duration = "604800s" 28 | 29 | # subscription never expires 30 | expiration_policy { 31 | ttl = "" 32 | } 33 | } 34 | 35 | resource "google_storage_bucket" "dataflow_job_temp_bucket" { 36 | project = var.project 37 | name = local.dataflow_temporary_gcs_bucket_name 38 | location = var.region 39 | storage_class = "REGIONAL" 40 | uniform_bucket_level_access = true 41 | dynamic "encryption" { 42 | for_each = (var.gcs_kms_key_name == "") ? [] : [1] 43 | content { 44 | default_kms_key_name = var.gcs_kms_key_name 45 | } 46 | } 47 | } 48 | 49 | resource "google_storage_bucket_object" "dataflow_job_temp_object" { 50 | name = local.dataflow_temporary_gcs_bucket_path 51 | content = "Placeholder for Dataflow to write temporary files" 52 | bucket = google_storage_bucket.dataflow_job_temp_bucket.name 53 | } 54 | 55 | resource "google_service_account" "dataflow_worker_service_account" { 56 | count = (var.dataflow_worker_service_account != "" && var.use_externally_managed_dataflow_sa == false) ? 1 : 0 57 | project = var.project 58 | account_id = var.dataflow_worker_service_account 59 | display_name = "Dataflow worker service account to execute pipeline operations" 60 | } 61 | 62 | resource "google_dataflow_job" "dataflow_job" { 63 | project = var.project 64 | name = local.dataflow_main_job_name 65 | template_gcs_path = local.dataflow_splunk_template_gcs_path 66 | temp_gcs_location = "gs://${local.dataflow_temporary_gcs_bucket_name}/${local.dataflow_temporary_gcs_bucket_path}" 67 | service_account_email = local.dataflow_worker_service_account 68 | machine_type = var.dataflow_job_machine_type 69 | max_workers = var.dataflow_job_machine_count 70 | parameters = merge({ 71 | inputSubscription = google_pubsub_subscription.dataflow_input_pubsub_subscription.id 72 | outputDeadletterTopic = google_pubsub_topic.dataflow_deadletter_pubsub_topic.id 73 | url = var.splunk_hec_url 74 | parallelism = var.dataflow_job_parallelism 75 | batchCount = var.dataflow_job_batch_count 76 | includePubsubMessage = local.dataflow_job_include_pubsub_message 77 | disableCertificateValidation = var.dataflow_job_disable_certificate_validation 78 | enableBatchLogs = local.dataflow_job_enable_batch_logs # Supported as of 2022-03-21-00_RC01 79 | enableGzipHttpCompression = local.dataflow_job_enable_gzip_http_compression # Supported as of 2022-04-25-00_RC00 80 | tokenSource = var.splunk_hec_token_source # Supported as of 2022-03-14-00_RC00 81 | }, 82 | (var.dataflow_job_udf_gcs_path != "" && var.dataflow_job_udf_function_name != "") ? 83 | { 84 | javascriptTextTransformGcsPath = var.dataflow_job_udf_gcs_path 85 | javascriptTextTransformFunctionName = var.dataflow_job_udf_function_name 86 | } : {}, 87 | (var.splunk_hec_token_source == "PLAINTEXT") ? 88 | { 89 | token = var.splunk_hec_token 90 | } : {}, 91 | (var.splunk_hec_token_source == "KMS") ? 92 | { 93 | token = var.splunk_hec_token 94 | tokenKMSEncryptionKey = var.splunk_hec_token_kms_encryption_key 95 | } : {}, 96 | (var.splunk_hec_token_source == "SECRET_MANAGER") ? 97 | { 98 | tokenSecretId = local.splunk_hec_token_secret_version_id # Supported as of 2022-03-14-00_RC00 99 | } : {}, 100 | ) 101 | region = var.region 102 | network = var.network 103 | subnetwork = "regions/${var.region}/subnetworks/${local.subnet_name}" 104 | ip_configuration = "WORKER_IP_PRIVATE" 105 | 106 | lifecycle { 107 | ignore_changes = [ 108 | additional_experiments # Ignore default experiments that may be added by Dataflow templates API 109 | ] 110 | } 111 | 112 | depends_on = [ 113 | google_compute_subnetwork.splunk_subnet, 114 | google_storage_bucket_object.dataflow_job_temp_object 115 | ] 116 | } 117 | -------------------------------------------------------------------------------- /replay.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | /* 16 | When the replay job needs to be run, simply run terraform with overriding variable 17 | `terraform apply -var deploy_replay_job="true"` or change the value in tfvars file. 18 | After the deadletter Pub/Sub topic has no more messages, set `deploy_replay_job` value to `false ` and 19 | run a regular terraform deployment (ex. terraform apply). 20 | */ 21 | 22 | 23 | resource "google_dataflow_job" "splunk_dataflow_replay" { 24 | count = var.deploy_replay_job == true ? 1 : 0 25 | 26 | project = var.project 27 | name = local.dataflow_replay_job_name 28 | template_gcs_path = local.dataflow_pubsub_template_gcs_path 29 | temp_gcs_location = "gs://${local.dataflow_temporary_gcs_bucket_name}/${local.dataflow_temporary_gcs_bucket_path}" 30 | machine_type = var.dataflow_job_machine_type 31 | max_workers = var.dataflow_job_machine_count 32 | parameters = { 33 | inputSubscription = google_pubsub_subscription.dataflow_deadletter_pubsub_sub.id 34 | outputTopic = google_pubsub_topic.dataflow_input_pubsub_topic.id 35 | } 36 | region = var.region 37 | network = var.network 38 | subnetwork = "regions/${var.region}/subnetworks/${local.subnet_name}" 39 | ip_configuration = "WORKER_IP_PRIVATE" 40 | 41 | on_delete = "cancel" 42 | 43 | lifecycle { 44 | ignore_changes = [ 45 | additional_experiments # Ignore default experiments that may be added by Dataflow templates API 46 | ] 47 | } 48 | 49 | depends_on = [ 50 | google_compute_subnetwork.splunk_subnet, 51 | google_storage_bucket_object.dataflow_job_temp_object 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /sample.tfvars: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | project = "[MY_PROJECT]" 16 | region = "[MY_REGION]" 17 | 18 | create_network = false 19 | network = "" 20 | subnet = "" 21 | primary_subnet_cidr = "10.128.0.0/20" 22 | 23 | # Log sink details 24 | log_filter = "" 25 | 26 | # Dataflow job output 27 | splunk_hec_url = "" 28 | splunk_hec_token_source = "" 29 | splunk_hec_token = "" 30 | splunk_hec_token_secret_id = "" 31 | splunk_hec_token_kms_encryption_key = "" 32 | 33 | # Dataflow job parameters 34 | dataflow_worker_service_account = "export-pipeline-worker" 35 | use_externally_managed_dataflow_sa = false 36 | dataflow_job_name = "export-pipeline" 37 | dataflow_job_machine_type = "n1-standard-4" 38 | dataflow_job_machine_count = 2 39 | dataflow_job_parallelism = 16 40 | dataflow_job_batch_count = 10 41 | dataflow_job_disable_certificate_validation = false 42 | dataflow_job_udf_gcs_path = "" 43 | dataflow_job_udf_function_name = "" 44 | 45 | # Dashboard parameters 46 | scoping_project = "[MY_PROJECT]" 47 | 48 | # Replay job settings 49 | deploy_replay_job = false 50 | -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | # Copyright 2021 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 | # https://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 | variable "project" { 16 | type = string 17 | description = "Project ID to deploy resources in" 18 | } 19 | 20 | variable "region" { 21 | type = string 22 | description = "Region to deploy regional-resources into. This must match subnet's region if deploying into existing network (e.g. Shared VPC). See `subnet` parameter below" 23 | } 24 | 25 | variable "create_network" { 26 | description = "Boolean value specifying if a new network needs to be created." 27 | default = false 28 | type = bool 29 | } 30 | 31 | variable "network" { 32 | description = "Network to deploy into" 33 | type = string 34 | } 35 | 36 | variable "subnet" { 37 | type = string 38 | description = "Subnet to deploy into. This is required when deploying into existing network (`create_network=false`) (e.g. Shared VPC)" 39 | default = "" 40 | } 41 | 42 | variable "primary_subnet_cidr" { 43 | type = string 44 | description = "The CIDR Range of the primary subnet" 45 | default = "10.128.0.0/20" 46 | } 47 | 48 | # Dashboard parameters 49 | 50 | variable "scoping_project" { 51 | type = string 52 | description = <<-EOF 53 | Cloud Monitoring scoping project ID to create dashboard under. 54 | This assumes a pre-existing scoping project whose metrics scope contains the `project` where dataflow job is to be deployed. 55 | See [Cloud Monitoring settings](https://cloud.google.com/monitoring/settings) for more details on scoping project. 56 | If parameter is empty, scoping project defaults to value of `project` parameter above. 57 | EOF 58 | default = "" 59 | } 60 | 61 | # Log sink details 62 | 63 | variable "log_filter" { 64 | type = string 65 | description = "Log filter to use when exporting logs" 66 | } 67 | 68 | # Dataflow job output 69 | 70 | variable "splunk_hec_url" { 71 | type = string 72 | description = "Splunk HEC URL to write data to. Example: https://[MY_SPLUNK_IP_OR_FQDN]:8088" 73 | 74 | validation { 75 | condition = can(regex("https?://.*(:[0-9]+)?", var.splunk_hec_url)) 76 | error_message = "Splunk HEC url must of the form ://: ." 77 | } 78 | } 79 | 80 | variable "splunk_hec_token_source" { 81 | type = string 82 | default = "PLAINTEXT" 83 | description = "Define in which type HEC token is provided. Possible options: [PLAINTEXT, KMS, SECRET_MANAGER]." 84 | 85 | validation { 86 | condition = contains(["PLAINTEXT", "KMS", "SECRET_MANAGER"], var.splunk_hec_token_source) 87 | error_message = "Valid values for var: dataflow_token_source are ('PLAINTEXT', 'KMS', 'SECRET_MANAGER')." 88 | } 89 | } 90 | 91 | variable "splunk_hec_token" { 92 | type = string 93 | description = "Splunk HEC token. Must be defined if `splunk_hec_token_source` if type of `PLAINTEXT` or `KMS`." 94 | default = "" 95 | sensitive = true 96 | } 97 | 98 | variable "splunk_hec_token_kms_encryption_key" { 99 | type = string 100 | description = "The Cloud KMS key to decrypt the HEC token string. Required if `splunk_hec_token_source` is type of KMS" 101 | default = "" 102 | validation { 103 | condition = can(regex("^projects\\/[^\\n\\r\\/]+\\/locations\\/[^\\n\\r\\/]+\\/keyRings\\/[^\\n\\r\\/]+\\/cryptoKeys\\/[^\\n\\r\\/]+$", var.splunk_hec_token_kms_encryption_key)) || var.splunk_hec_token_kms_encryption_key == "" 104 | error_message = "HEC token encryption key must match rex: '^projects\\/[^\\n\\r\\/]+\\/locations\\/[^\\n\\r\\/]+\\/keyRings\\/[^\\n\\r\\/]+\\/cryptoKeys\\/[^\\n\\r\\/]+$' pattern." 105 | } 106 | } 107 | 108 | # TODO: Make cross variable validation once https://github.com/hashicorp/terraform/issues/25609 is resolved 109 | variable "splunk_hec_token_secret_id" { 110 | type = string 111 | description = "Id of the Secret for Splunk HEC token. Required if `splunk_hec_token_source` is type of SECRET_MANAGER" 112 | default = "" 113 | validation { 114 | condition = can(regex("^projects\\/[^\\n\\r\\/]+\\/secrets\\/[^\\n\\r\\/]+\\/versions\\/[^\\n\\r\\/]+$", var.splunk_hec_token_secret_id)) || var.splunk_hec_token_secret_id == "" 115 | error_message = "HEC token secret id key must match rex: '^projects\\/[^\\n\\r\\/]+\\/secrets\\/[^\\n\\r\\/]+\\/versions\\/[^\\n\\r\\/]+$' pattern." 116 | } 117 | } 118 | 119 | variable "gcs_kms_key_name" { 120 | type = string 121 | description = <