September 16, 2024

What Are Snowflake External Functions?

Discover how to extend Snowflake's capabilities using external functions for custom processing.
Dexter Chu
Head of Marketing

External functions in Snowflake are user-defined functions (UDFs) that allow the execution of code outside of Snowflake within a remote service. This architecture enables complex computations and integrations that extend beyond the native capabilities of Snowflake itself. External functions operate similarly to traditional UDFs within SQL statements. They accept parameters, process these parameters via a remote service, and return the results to Snowflake. The interaction with the remote service typically occurs through a proxy service, enhancing security and facilitating various billing models.

How do Snowflake External Functions Work?

The workflow of external functions involves several steps to ensure seamless execution and data processing. Here is a detailed overview:


1. A SQL statement that calls an external function is executed within Snowflake.
2. Snowflake reads the external function definition and the corresponding API integration information.
3. An HTTP POST request is sent from Snowflake to the proxy service, containing the data in JSON format.
4. The proxy service forwards the request to the remote service, which processes the data and returns the result.
5. If asynchronous processing is required, Snowflake sends multiple HTTP GET requests until a timeout or error occurs.

This workflow ensures that data is processed efficiently and securely, leveraging the capabilities of remote services.

What are the Key Components of Snowflake External Functions?

Snowflake external functions consist of several key components that work together to enable their functionality:

  • External Function: An external function in Snowflake is a database object containing the information required to call a remote service. It acts like any other UDF in SQL statements, accepting parameters and returning values.
  • Remote Service: The remote service executes the actual code and is responsible for processing the inputs received from Snowflake. It accepts inputs in JSON format and returns outputs in the same format.
  • Proxy Service: A proxy service acts as an intermediary between Snowflake and the remote service. It relays data between the two and provides additional security features.
  • API Integration: API integration is a Snowflake object that stores the security and necessary information required to interface with the proxy or remote service.

How to Create and Use Snowflake External Functions

1. Define the External Function

Begin by defining the external function in Snowflake. This involves specifying the remote service and the parameters it will accept.

CREATE OR REPLACE FUNCTION my_external_function(input_param STRING)
RETURNS STRING
LANGUAGE JAVASCRIPT
RUNTIME_VERSION = '1.0'
HANDLER = 'handler'
API_INTEGRATION = 'my_api_integration'
AS 'https://my-remote-service.com/execute';

This code defines an external function that calls a remote service to process the input parameter and return a result.

2. Create the API Integration

Next, create the API integration that will handle the communication between Snowflake and the remote service.

CREATE OR REPLACE API INTEGRATION my_api_integration
API_PROVIDER = 'aws_api_gateway'
API_AWS_ROLE_ARN = 'arn:aws:iam::123456789012:role/my-role'
API_ALLOWED_PREFIXES = ('https://my-remote-service.com/');

This code sets up the API integration with the necessary security and configuration settings.

3. Execute the External Function

Finally, execute the external function within a SQL statement to process data using the remote service.

SELECT my_external_function('input_data') FROM my_table;

This SQL statement calls the external function, passing in the input data and receiving the processed result from the remote service.

Common Challenges and Solutions

While using Snowflake external functions, users may encounter several common challenges. Here are some solutions:

  • Creation-Time Requirements: Ensure you have a deep understanding of the cloud platform and specific security settings to create external functions successfully.
  • Execution-Time Issues: Mitigate potential delays and timeouts by implementing batch processing and retries where necessary.
  • Restrictions: Be aware of the limitations, such as the inability to create stored procedures and the lack of support for future grants of privileges on external functions.

Recap of Snowflake External Functions

Snowflake external functions offer a powerful way to extend the capabilities of the Snowflake platform by leveraging remote services. Here are the key takeaways:

  • Flexibility: External functions allow developers to write code in various programming languages and access external libraries, enabling complex computations and integrations.
  • Workflow: The workflow of external functions ensures efficient and secure data processing through a series of well-defined steps.
  • Best Practices: Following best practices, such as using batch API usage and handling duplicate rows, can maximize the benefits and mitigate the limitations of external functions.

By understanding and applying these concepts, users can effectively utilize Snowflake external functions to enhance their data processing and analysis capabilities.

Keep reading

View all