diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..edbfef2 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +name: ci + +on: + push: + branches: + - '*' + - '*/*' + - '**' + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Install Terraform + uses: hashicorp/setup-terraform@v1 + with: + terraform_version: 1.0.0 + terraform_wrapper: false + + - name: Check formatting + run: terraform fmt -check -recursive diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9732bc6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ + +# Local .terraform directories +**/.terraform/* + +# .tfstate files +*.tfstate +*.tfstate.* +*.terraform.lock.hcl + +# Crash log files +crash.log + +# Ignore any .tfvars files that are generated automatically for each Terraform run. Most +# .tfvars files are managed as part of configuration and so should be included in +# version control. +# +# example.tfvars +*.tfvars + +# Ignore override files as they are usually used to override resources locally and so +# are not checked in +override.tf +override.tf.json +*_override.tf +*_override.tf.json + +# Credentials Files +**/credentials.json +**/*.json + +/.idea/ +.DS_Store diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..9247635 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,3 @@ +Version 0.1.0 (2022-03-18) +-------------------------- +Initial release diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..393f675 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2021-2021 Snowplow Analytics Ltd. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..ea4ce6b --- /dev/null +++ b/README.md @@ -0,0 +1,166 @@ +[![Release][release-image]][release] [![CI][ci-image]][ci] [![License][license-image]][license] [![Registry][registry-image]][registry] [![Source][source-image]][source] + +# terraform-aws-snowflake-loader-setup + +A Terraform module for preparing snowflake for loading snowplow data. Should be used after the `terraform-snowflake-target`. + +## Prerequisites + +Authentication for the service user is required for the snowflake terraform provider - [follow this tutorial][snowflake-service-user-tutorial] to obtain snowflake connection details: + +| Parameter | Description | +|------|---------| +| account | The account name. | +| username | A snowflake user to perform resource creation.| +| region | Region for the snowflake deployment. | +| role | Needs to be ACCOUNTADMIN or similar. | +| private_key_path | Path the private key. | + +## Usage + +### Applying module directly + +1. Fill variables in [terraform.tfvars.tmpl](terraform.tfvars.tmpl) and copy it to `terraform.tfvars`. +2. Using environment variables for authentication as [described in here][snowflake-env-vars]. Fill the template in [snowflake_provider_vars.sh](snowflake_provider_vars.sh) and run `source ./snowflake_provider_vars.sh` to set up your local environment. +3. Run `terraform init` +4. Run `terraform apply` + +### Using the module from another module + +Pass authentication parameters directly to Snowflake provider. + +```hcl +provider "snowflake" { + username = "my_user" + account = "my_account" + region = "us-west-2" + role = "ACCOUNTADMIN" + private_key_path = "/path/to/private/key" +} + +module "snowflake_target" { + source = "snowplow-devops/target/snowflake" + + name = "snowplow" + snowflake_password = "example_password" +} + +module "snowflake_setup" { + source = "snowplow-devops/snowflake-loader-setup/aws" + + name = "snowplow" + stage_bucket = "snowplow-oss-bucket" + aws_account_id = "0000000000" + snowflake_database = module.snowflake_target.snowflake_database + snowflake_event_table = module.snowflake_target.snowflake_event_table + snowflake_file_format = module.snowflake_target.snowflake_file_format + snowflake_schema = module.snowflake_target.snowflake_schema + snowflake_user = module.snowflake_target.snowflake_user +} +``` + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.0.0 | +| [snowflake](#requirement\_snowflake) | 0.25.32 | + +## Providers + +| Name | Version | +|------|---------| +| [snowflake](#provider\_snowflake) | 0.25.32 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [snowflake_database_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/database_grant) | resource | +| [snowflake_file_format_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/file_format_grant) | resource | +| [snowflake_integration_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/integration_grant) | resource | +| [snowflake_role.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/role) | resource | +| [snowflake_role_grants.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/role_grants) | resource | +| [snowflake_schema_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/schema_grant) | resource | +| [snowflake_stage.folder_monitoring](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/stage) | resource | +| [snowflake_stage.transformed](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/stage) | resource | +| [snowflake_stage_grant.folder_monitoring](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/stage_grant) | resource | +| [snowflake_stage_grant.transformed](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/stage_grant) | resource | +| [snowflake_storage_integration.integration](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/storage_integration) | resource | +| [snowflake_table_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/table_grant) | resource | +| [snowflake_warehouse.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/warehouse) | resource | +| [snowflake_warehouse_grant.loader](https://registry.terraform.io/providers/chanzuckerberg/snowflake/0.25.32/docs/resources/warehouse_grant) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [aws\_account\_id](#input\_aws\_account\_id) | AWS account id for the role to be linked with storage integration. Role itself won't be created by this module | `string` | n/a | yes | +| [name](#input\_name) | A name which will be prepended to the created resources | `string` | n/a | yes | +| [snowflake\_database](#input\_snowflake\_database) | Snowflake database name | `string` | n/a | yes | +| [snowflake\_event\_table](#input\_snowflake\_event\_table) | Snowflake events table name | `string` | n/a | yes | +| [snowflake\_file\_format](#input\_snowflake\_file\_format) | Snowflake file format name | `string` | n/a | yes | +| [snowflake\_schema](#input\_snowflake\_schema) | Snowflake schema name | `string` | n/a | yes | +| [snowflake\_user](#input\_snowflake\_user) | Snowflake username | `string` | n/a | yes | +| [stage\_bucket](#input\_stage\_bucket) | Name of the S3 bucket which will be used as stage by Snowflake | `string` | n/a | yes | +| [folder\_monitoring\_enabled](#input\_folder\_monitoring\_enabled) | Folder monitoring loading | `bool` | `false` | no | +| [override\_folder\_monitoring\_stage\_url](#input\_override\_folder\_monitoring\_stage\_url) | Override monitoring stage url, if not set it will be defaulted "s3://${var.stage\_bucket\_name}/${var.name}/shredded/v1" | `string` | `""` | no | +| [override\_iam\_loader\_role\_name](#input\_override\_iam\_loader\_role\_name) | Override integration iam role name, if not set it will be var.name with -snowflakedb-load-role suffix | `string` | `""` | no | +| [override\_snowflake\_loader\_role](#input\_override\_snowflake\_loader\_role) | Override loader role name in snowflake, if not set it will be uppercase var.name with "\_LOADER\_ROLE" suffix | `string` | `""` | no | +| [override\_snowflake\_loader\_user](#input\_override\_snowflake\_loader\_user) | Override loader user name in snowflake, if not set it will be uppercase var.name with \_LOADER\_USER suffix | `string` | `""` | no | +| [override\_snowflake\_wh\_name](#input\_override\_snowflake\_wh\_name) | Override warehouse name, if not set it will be defaulted to uppercase var.name with "\_WAREHOUSE" suffix | `string` | `""` | no | +| [override\_transformed\_stage\_url](#input\_override\_transformed\_stage\_url) | Override transformed stage url, if not set it will be defaulted "s3://${var.stage\_bucket\_name}/${var.name}/monitoring" | `string` | `""` | no | +| [snowflake\_wh\_auto\_resume](#input\_snowflake\_wh\_auto\_resume) | Whether to enable auto resume which makes automatically resume the warehouse when any statement that requires a warehouse is submitted | `bool` | `true` | no | +| [snowflake\_wh\_auto\_suspend](#input\_snowflake\_wh\_auto\_suspend) | Time period to wait before suspending warehouse | `number` | `60` | no | +| [snowflake\_wh\_size](#input\_snowflake\_wh\_size) | Size of the Snowflake warehouse to connect to | `string` | `"XSMALL"` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| [aws\_iam\_storage\_integration\_role\_arn](#output\_aws\_iam\_storage\_integration\_role\_arn) | AWS storage integration IAM role ARN | +| [aws\_iam\_storage\_integration\_role\_name](#output\_aws\_iam\_storage\_integration\_role\_name) | AWS storage integration IAM role name | +| [aws\_iam\_storage\_integration\_user\_arn](#output\_aws\_iam\_storage\_integration\_user\_arn) | AWS storage integration IAM user ARN | +| [aws\_s3\_bucket\_name](#output\_aws\_s3\_bucket\_name) | Name of the S3 bucket which will be used as stage by Snowflake | +| [aws\_s3\_folder\_monitoring\_stage\_url](#output\_aws\_s3\_folder\_monitoring\_stage\_url) | AWS bucket url of folder monitoring stage | +| [aws\_s3\_transformed\_stage\_url](#output\_aws\_s3\_transformed\_stage\_url) | AWS bucket url of transformed stage | +| [aws\_storage\_external\_id](#output\_aws\_storage\_external\_id) | AWS external id | +| [snowflake\_loader\_role](#output\_snowflake\_loader\_role) | Snowflake role for loading snowplow data | +| [snowflake\_monitoring\_stage\_name](#output\_snowflake\_monitoring\_stage\_name) | Name of monitoring stage | +| [snowflake\_transformed\_stage\_name](#output\_snowflake\_transformed\_stage\_name) | Name of transformed stage | +| [snowflake\_warehouse](#output\_snowflake\_warehouse) | Snowflake warehouse name | + +# Copyright and license + +The Terraform Snowflake Loader Setup project is Copyright 2022-2022 Snowplow Analytics Ltd. + +Licensed under the [Apache License, Version 2.0][license] (the "License"); +you may not use this software except in compliance with the License. + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +[snowflake-service-user-tutorial]: https://quickstarts.snowflake.com/guide/terraforming_snowflake/index.html?index=..%2F..index#2 +[snowflake-env-vars]: https://quickstarts.snowflake.com/guide/terraforming_snowflake/index.html?index=..%2F..index#3 + +[release]: https://github.com/snowplow-devops/terraform-aws-snowflake-loader-setup/releases/latest +[release-image]: https://img.shields.io/github/v/release/snowplow-devops/terraform-aws-snowflake-loader-setup + +[ci]: https://github.com/snowplow-devops/terraform-aws-snowflake-loader-setup/actions?query=workflow%3Aci +[ci-image]: https://github.com/snowplow-devops/terraform-aws-snowflake-loader-setup/workflows/ci/badge.svg + +[license]: https://www.apache.org/licenses/LICENSE-2.0 +[license-image]: https://img.shields.io/badge/license-Apache--2-blue.svg?style=flat + +[registry]: https://registry.terraform.io/modules/snowplow-devops/snowflake-loader-setup/aws/latest +[registry-image]: https://img.shields.io/static/v1?label=Terraform&message=Registry&color=7B42BC&logo=terraform + +[source]: https://github.com/snowplow/snowplow-rdb-loader +[source-image]: https://img.shields.io/static/v1?label=Snowplow&message=Snowflake%20Loader&color=0E9BA4&logo=GitHub diff --git a/init.tf b/init.tf new file mode 100644 index 0000000..3d4b794 --- /dev/null +++ b/init.tf @@ -0,0 +1,34 @@ +resource "snowflake_storage_integration" "integration" { + name = "${upper(var.name)}_SNOWFLAKE_STORAGE_INTEGRATION" + type = "EXTERNAL_STAGE" + enabled = true + storage_allowed_locations = ["s3://${var.stage_bucket}/"] + storage_provider = "S3" + storage_aws_role_arn = local.snowflake_iam_load_role_arn +} + +resource "snowflake_warehouse" "loader" { + name = local.wh_name + warehouse_size = var.snowflake_wh_size + auto_suspend = var.snowflake_wh_auto_suspend + auto_resume = var.snowflake_wh_auto_resume +} + +resource "snowflake_stage" "transformed" { + name = local.transformed_stage_name + url = local.transformed_stage_url + database = var.snowflake_database + schema = var.snowflake_schema + storage_integration = snowflake_storage_integration.integration.name + file_format = "FORMAT_NAME = \"${var.snowflake_database}\".\"${var.snowflake_schema}\".${var.snowflake_file_format}" +} + +resource "snowflake_stage" "folder_monitoring" { + count = var.folder_monitoring_enabled ? 1 : 0 + name = local.folder_monitoring_stage_name + url = local.folder_monitoring_stage_url + database = var.snowflake_database + schema = var.snowflake_schema + storage_integration = snowflake_storage_integration.integration.name + file_format = "FORMAT_NAME = \"${var.snowflake_database}\".\"${var.snowflake_schema}\".${var.snowflake_file_format}" +} diff --git a/main.tf b/main.tf new file mode 100644 index 0000000..f949e7b --- /dev/null +++ b/main.tf @@ -0,0 +1,11 @@ +locals { + wh_name = var.override_snowflake_wh_name != "" ? var.override_snowflake_wh_name : "${upper(var.name)}_WAREHOUSE" + snowflake_loader_user = var.override_snowflake_loader_user != "" ? var.override_snowflake_loader_user : "${upper(var.name)}_LOADER_USER" + snowflake_loader_role = var.override_snowflake_loader_role != "" ? var.override_snowflake_loader_role : "${upper(var.name)}_LOADER_ROLE" + transformed_stage_name = "${upper(var.name)}_TRANSFORMED_STAGE" + transformed_stage_url = var.override_transformed_stage_url != "" ? var.override_transformed_stage_url : "s3://${var.stage_bucket}/tranformed/good" + folder_monitoring_stage_name = "${upper(var.name)}_FOLDER_MONITORING_STAGE" + folder_monitoring_stage_url = var.override_folder_monitoring_stage_url != "" ? var.override_folder_monitoring_stage_url : "s3://${var.stage_bucket}/${var.name}/monitoring" + snowflake_iam_load_role_name = var.override_iam_loader_role_name != "" ? var.override_iam_loader_role_name : "${var.name}-snowflakedb-load-role" + snowflake_iam_load_role_arn = "arn:aws:iam::${var.aws_account_id}:role/${local.snowflake_iam_load_role_name}" +} diff --git a/outputs.tf b/outputs.tf new file mode 100644 index 0000000..b8e3835 --- /dev/null +++ b/outputs.tf @@ -0,0 +1,54 @@ +output "snowflake_loader_role" { + description = "Snowflake role for loading snowplow data" + value = local.snowflake_loader_role +} + +output "snowflake_warehouse" { + description = "Snowflake warehouse name" + value = local.wh_name +} + +output "snowflake_transformed_stage_name" { + description = "Name of transformed stage" + value = local.transformed_stage_name +} + +output "snowflake_monitoring_stage_name" { + description = "Name of monitoring stage" + value = local.folder_monitoring_stage_name +} + +output "aws_s3_transformed_stage_url" { + description = "AWS bucket url of transformed stage" + value = snowflake_stage.transformed.url +} + +output "aws_s3_folder_monitoring_stage_url" { + description = "AWS bucket url of folder monitoring stage" + value = join("", snowflake_stage.folder_monitoring[*].url) +} + +output "aws_s3_bucket_name" { + description = "Name of the S3 bucket which will be used as stage by Snowflake" + value = var.stage_bucket +} + +output "aws_storage_external_id" { + description = "AWS external id" + value = snowflake_storage_integration.integration.storage_aws_external_id +} + +output "aws_iam_storage_integration_role_arn" { + description = "AWS storage integration IAM role ARN" + value = local.snowflake_iam_load_role_arn +} + +output "aws_iam_storage_integration_user_arn" { + description = "AWS storage integration IAM user ARN" + value = snowflake_storage_integration.integration.storage_aws_iam_user_arn +} + +output "aws_iam_storage_integration_role_name" { + description = "AWS storage integration IAM role name" + value = local.snowflake_iam_load_role_name +} diff --git a/role.tf b/role.tf new file mode 100644 index 0000000..1b48b14 --- /dev/null +++ b/role.tf @@ -0,0 +1,86 @@ +resource "snowflake_role" "loader" { + name = local.snowflake_loader_role +} + +resource "snowflake_warehouse_grant" "loader" { + for_each = toset(["MONITOR", "USAGE", "OPERATE"]) + warehouse_name = snowflake_warehouse.loader.name + privilege = each.key + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_database_grant" "loader" { + database_name = var.snowflake_database + privilege = "USAGE" + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_file_format_grant" "loader" { + database_name = var.snowflake_database + schema_name = var.snowflake_schema + file_format_name = var.snowflake_file_format + privilege = "USAGE" + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_integration_grant" "loader" { + integration_name = snowflake_storage_integration.integration.name + privilege = "USAGE" + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_stage_grant" "transformed" { + database_name = var.snowflake_database + schema_name = var.snowflake_schema + stage_name = snowflake_stage.transformed.name + privilege = "USAGE" + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_stage_grant" "folder_monitoring" { + for_each = toset(snowflake_stage.folder_monitoring[*].name) + database_name = var.snowflake_database + schema_name = var.snowflake_schema + stage_name = each.value + privilege = "USAGE" + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_schema_grant" "loader" { + for_each = toset([ + "CREATE TABLE", + "CREATE TEMPORARY TABLE", + "MODIFY", + "USAGE" + ]) + database_name = var.snowflake_database + schema_name = var.snowflake_schema + privilege = each.key + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_table_grant" "loader" { + for_each = toset([ + "INSERT", + "OWNERSHIP", + "SELECT" + ]) + database_name = var.snowflake_database + schema_name = var.snowflake_schema + table_name = var.snowflake_event_table + privilege = each.key + roles = [snowflake_role.loader.name] + with_grant_option = false +} + +resource "snowflake_role_grants" "loader" { + role_name = snowflake_role.loader.name + users = [var.snowflake_user] +} diff --git a/snowflake_provider_vars.sh b/snowflake_provider_vars.sh new file mode 100644 index 0000000..1684cae --- /dev/null +++ b/snowflake_provider_vars.sh @@ -0,0 +1,5 @@ +export SNOWFLAKE_USER="sf_user" +export SNOWFLAKE_ACCOUNT="sf_account" +export SNOWFLAKE_REGION="us-west-2" +export SNOWFLAKE_ROLE="sf_role" +export SNOWFLAKE_PRIVATE_KEY_PATH="/sf/private/key/path" \ No newline at end of file diff --git a/terraform.tfvars.tmpl b/terraform.tfvars.tmpl new file mode 100644 index 0000000..cf0a20c --- /dev/null +++ b/terraform.tfvars.tmpl @@ -0,0 +1,10 @@ +name = "snowplow" +stage_bucket = "snowplow-oss-bucket" +# The terraform-snowflake-target outputs +snowflake_database = "SNOWPLOW_DATABASE" +snowflake_event_table = "EVENTS" +snowflake_file_format = "SNOWPLOW_ENRICHED_JSON" +snowflake_schema = "ATOMIC" +snowflake_user = "snowplow" +# Account id to which storage integration will be linked to. +aws_account_id = "00000000" diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..05c15ed --- /dev/null +++ b/variables.tf @@ -0,0 +1,114 @@ +variable "name" { + description = "A name which will be prepended to the created resources" + type = string + + validation { + condition = can(length(var.name) == 0) + error_message = "Name could not be empty." + } + + validation { + condition = !can(regex("-", var.name)) + error_message = "Name could not contain '-'." + } +} + +variable "snowflake_database" { + description = "Snowflake database name" + type = string +} + +variable "snowflake_schema" { + description = "Snowflake schema name" + type = string +} + +variable "snowflake_event_table" { + description = "Snowflake events table name" + type = string +} + +variable "snowflake_user" { + description = "Snowflake username" + type = string +} + +variable "snowflake_file_format" { + description = "Snowflake file format name" + type = string +} + +variable "aws_account_id" { + description = "AWS account id for the role to be linked with storage integration. Role itself won't be created by this module" + type = string + + validation { + condition = can(regex("^\\d+$", var.aws_account_id)) + error_message = "AWS account consists of numbers." + } +} + +variable "stage_bucket" { + description = "Name of the S3 bucket which will be used as stage by Snowflake" + type = string +} + +variable "snowflake_wh_size" { + description = "Size of the Snowflake warehouse to connect to" + default = "XSMALL" + type = string +} + +variable "folder_monitoring_enabled" { + description = "Folder monitoring loading" + type = bool + default = false +} + +variable "snowflake_wh_auto_suspend" { + description = "Time period to wait before suspending warehouse" + default = 60 + type = number +} + +variable "snowflake_wh_auto_resume" { + description = "Whether to enable auto resume which makes automatically resume the warehouse when any statement that requires a warehouse is submitted " + default = true + type = bool +} + +variable "override_snowflake_wh_name" { + description = "Override warehouse name, if not set it will be defaulted to uppercase var.name with \"_WAREHOUSE\" suffix" + default = "" + type = string +} + +variable "override_folder_monitoring_stage_url" { + description = "Override monitoring stage url, if not set it will be defaulted \"s3://$${var.stage_bucket_name}/$${var.name}/shredded/v1\"" + default = "" + type = string +} + +variable "override_transformed_stage_url" { + description = "Override transformed stage url, if not set it will be defaulted \"s3://$${var.stage_bucket_name}/$${var.name}/monitoring\"" + default = "" + type = string +} + +variable "override_snowflake_loader_user" { + description = "Override loader user name in snowflake, if not set it will be uppercase var.name with _LOADER_USER suffix" + default = "" + type = string +} + +variable "override_snowflake_loader_role" { + description = "Override loader role name in snowflake, if not set it will be uppercase var.name with \"_LOADER_ROLE\" suffix" + default = "" + type = string +} + +variable "override_iam_loader_role_name" { + description = "Override integration iam role name, if not set it will be var.name with -snowflakedb-load-role suffix" + default = "" + type = string +} diff --git a/versions.tf b/versions.tf new file mode 100644 index 0000000..09df76e --- /dev/null +++ b/versions.tf @@ -0,0 +1,10 @@ +terraform { + required_version = ">= 1.0.0" + + required_providers { + snowflake = { + source = "chanzuckerberg/snowflake" + version = "0.25.32" + } + } +}