Terraform Module for Managing Snowflake Functions
- snowflake_function
The terraform-snowflake-functions repository is a Terraform module designed to manage the creation and configuration of functions in Snowflake. Snowflake is a cloud-based data warehousing platform.
Here's a summary of what this module does:
Function Creation
: This module allows you to create user-defined functions in Snowflake. You can specify the function's name, return type, and the actual function body (the code that gets executed when the function is called).Language Support
: The module supports creating functions written in JavaScript and a Python variant, as these are the languages supported by Snowflake for user-defined functions.Function Configuration
: You can configure various aspects of the function, such as whether it's a secure function, its null input behavior, return behavior, and the version of the JavaScript runtime to use.Database and Schema
: You can specify the database and schema where the function should be created.Argument Specification
: The module allows you to specify the arguments for the function, including their names and types.Import and Package Management
: You can specify additional JavaScript libraries to import and Snowflake packages to use in the function.
In summary, the terraform-snowflake-functions repository is a Terraform module for managing Snowflake functions. It provides a declarative way to create and configure functions in Snowflake, making it easier to version control and automate your Snowflake function deployments.
Example CICD with BitBucket
and Codefresh
:
The Snowflake Terraform module for creating functions supports two languages for the function body:
-
JavaScript: You can write your function in JavaScript, which is fully supported by Snowflake. When using JavaScript, set the language argument to "JAVASCRIPT".
-
Python: Snowflake also supports a Python variant for user-defined functions. However, it's important to note that this is not a fully featured Python runtime. It's a Python-like syntax that gets transpiled to JavaScript under the hood. When using this Python variant, set the language argument to "PYTHON".
-
SQL: Snowflake also supports inline SQL for the Statement.
These are the languages supported by Snowflake for user-defined functions, and hence by this Terraform module. Other languages like Java, C#, etc., are not supported for writing user-defined functions in Snowflake.
The following shows basica and advanced usages for this module.
The following is an example for Python
:
module "my_python_function" {
source = "https://github.com/Richard-Barrett/terraform-snowflake-functions"
version = "0.0.1"
database = "my_database"
schema = "my_schema"
name = "my_python_function"
return_type = "STRING"
statement = <<-EOF
def my_python_function():
return 'Hello, World!'
EOF
language = "PYTHON"
}
GitHub Copilot To use this module for a Python function in Snowflake, you would need to provide the necessary arguments to the module. Here's an example:
In this example, we're creating a Python function that returns the string 'Hello, World!'. The statement argument contains the Python code for the function. The language argument is set to 'PYTHON' to indicate that the function is written in Python.
Please replace the placeholders (my_database, my_schema, etc.) with your actual values. Also, ensure that the source argument points to the correct path of your module.
Note: The Snowflake Python language is only available in the Snowflake Scripting variant of JavaScript and is not a fully featured Python runtime. It is a Python-like syntax that gets transpiled to JavaScript.
The following is an example for Javascript
:
module "my_javascript_function" {
source = "https://github.com/Richard-Barrett/terraform-snowflake-functions"
version = "0.0.1"
database = "my_database"
schema = "my_schema"
name = "my_javascript_function"
return_type = "STRING"
statement = <<-EOF
function my_javascript_function() {
return 'Hello, World!';
}
EOF
language = "JAVASCRIPT"
}
In this example, we're creating a JavaScript function that returns the string 'Hello, World!'. The statement argument contains the JavaScript code for the function. The language argument is set to 'JAVASCRIPT' to indicate that the function is written in JavaScript.
Please replace the placeholders (my_database, my_schema, etc.) with your actual values. Also, ensure that the source argument points to the correct path of your module.
The following is an advanced usage for the module specifying a file instead of inline code
module "my_javascript_function" {
source = "./modules/my_function" # replace with the actual path to your module
database = "my_database"
schema = "my_schema"
name = "my_javascript_function"
return_type = "STRING"
statement = file("${path.module}/my_javascript_function.js")
language = "JAVASCRIPT"
}
In this example, the file function reads the contents of the my_javascript_function.js file in the same directory as the module. The ${path.module} interpolation is a built-in Terraform variable that gives the path of the module.
Please replace the placeholders (my_database, my_schema, etc.) with your actual values. Also, ensure that the source argument points to the correct path of your module, and that the file path in the file function points to your JavaScript file.
module "my_advanced_function" {
source = "./modules/my_function" # replace with the actual path to your module
database = "my_database"
schema = "my_schema"
name = "my_advanced_function"
return_type = "STRING"
statement = file("${path.module}/my_advanced_function.js")
language = "JAVASCRIPT"
is_secure = true
comment = "This is my advanced function"
handler = "myHandler"
null_input_behavior = "CALLED_ON_NULL_INPUT"
return_behavior = "IMMUTABLE"
runtime_version = "1.0"
target_path = "/path/to/target"
arguments = [
{
name = "arg1"
type = "STRING"
},
{
name = "arg2"
type = "NUMBER"
}
]
imports = ["import1", "import2"]
packages = ["package1", "package2"]
}
In this example, we're creating a secure JavaScript function with a comment, a handler, specific null input behavior, return behavior, runtime version, target path, arguments, imports, and packages. The function statement is read from a file named my_advanced_function.js in the same directory as the module.
Please replace the placeholders (my_database, my_schema, etc.) with your actual values. Also, ensure that the source argument points to the correct path of your module, and that the file path in the file function points to your JavaScript file.
- Language Support: Snowflake supports JavaScript and a Python variant for user-defined functions. Other languages like Java are not supported.
- File Path: If you're reading the function statement from a file, ensure that the file path is correct. The ${path.module} interpolation can be used to reference files relative to the module's directory.
- Function Arguments: The arguments variable should be a list of objects, each with a name and type attribute. If the function doesn't take any arguments, you can set arguments to an empty list.
- Secure Functions: If is_secure is set to true, the function is a secure function. Secure functions don't reveal sensitive data and are suitable for user-defined functions that manipulate sensitive data.
- Null Input Behavior: The null_input_behavior variable determines how the function behaves when called with null input. It can be set to CALLED_ON_NULL_INPUT (the function is called normally) or RETURNS_NULL_ON_NULL_INPUT (the function returns null).
- Return Behavior: The return_behavior variable determines whether the function is IMMUTABLE (always returns the same result for the same input) or VOLATILE (can return different results for the same input).
- Runtime Version: The runtime_version variable specifies the version of the JavaScript runtime to use for the function.
- Imports and Packages: The imports and packages variables allow you to specify additional JavaScript libraries to import and Snowflake packages to use, respectively.
- Terraform Version and Provider: Ensure that you're using a version of Terraform and the Snowflake provider that supports all the features you're using.
- Idempotency: Terraform is designed to be idempotent, meaning running the same configuration multiple times should result in the same state. However, if your function statement includes randomness or depends on external state, it might not be idempotent.
Remember to always test your Terraform configurations in a safe environment before applying them to production.
The terraform-snowflake-functions
is a Terraform module designed to streamline the creation, configuration, and management of user-defined functions in Snowflake, a cloud-based data warehousing platform.
Key features of this module include:
User-Defined Function Creation
: Enables the creation of functions with custom names, return types, and function bodies.Language Support
: Supports functions written in JavaScript and a Python variant, aligning with Snowflake's language support for user-defined functions.Function Configuration
: Provides options to configure various function attributes such as security settings, null input behavior, return behavior, and JavaScript runtime version.Database and Schema Specification
: Allows users to define the specific database and schema where the function should be created.Argument Management
: Facilitates the specification of function arguments, including their names and types.Import and Package Management
: Supports the inclusion of additional JavaScript libraries and Snowflake packages within the function.
In essence, the terraform-snowflake-functions
module provides a declarative, version-controlled, and automated approach to managing Snowflake function deployments.
Name | Version |
---|---|
terraform | >= 1.5.6 |
null | ~> 3.1.0 |
snowflake | ~> 0.90.0 |
Name | Version |
---|---|
snowflake | ~> 0.90.0 |
No modules.
Name | Type |
---|---|
snowflake_function.this | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
comment | The comment for the function | string |
"" |
no |
database | The name of the database | string |
n/a | yes |
handler | The handler for the function | string |
"" |
no |
imports | The imports for the function | list(string) |
[] |
no |
is_secure | The security status of the function | bool |
false |
no |
language | The language of the function | string |
n/a | yes |
name | The name of the function | string |
n/a | yes |
null_input_behavior | The null input behavior of the function | string |
"" |
no |
packages | The packages for the function | list(string) |
[] |
no |
return_behavior | The return behavior of the function | string |
"" |
no |
return_type | The return type of the function | string |
n/a | yes |
runtime_version | The runtime version of the function | string |
"" |
no |
schema | The name of the schema | string |
n/a | yes |
statement | The statement of the function | string |
n/a | yes |
target_path | The target path of the function | string |
"" |
no |
Name | Description |
---|---|
snowflake_function | The Snowflake function |
snowflake_function_name | The name of the Snowflake function |