Step 1: Create the remote service (Azure function) in the Portal¶
This topic provides detailed instructions for creating an Azure Function for use as the remote service for your external function.
Previous step¶
Create the Azure functions app¶
There are multiple possible ways to create a remote service. This section shows how to create a remote service that is implemented as a JavaScript function.
This external function is synchronous. For information about creating an asynchronous external function, see Creating an Asynchronous Function on Azure.
Create an Azure Functions app to serve as a container for the function(s) that you create later:
If you haven’t already, log into the Azure Portal.
Create the Azure Functions app by following the instructions in the Microsoft documentation: Azure Functions App (https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal).
As you follow the instructions, remember the following:
When you enter a name the Function App Name field, also record the name in the
Azure Function app name
field in your tracking worksheet.When asked to choose how to Publish, choose Code.
Some restrictions apply when creating multiple apps in the same resource group. For details, see the Microsoft documentation: Azure app service (https://docs.microsoft.com/en-us/azure/app-service/containers/app-service-linux-intro#limitations).
Snowflake provides a sample “echo” function in Node.js. To use this sample function to get started:
When asked for the
Runtime stack
, select Node.js.When asked for the version of Node.js, select version 12.
When asked which OS to run the function on, choose “Windows” or “Linux”.
If you are only creating a demo function, Snowflake recommends selecting “Windows”.
Linux Function Apps cannot be edited in the Azure Portal. Users must publish the code through the Visual Studio Code interface.
If you want to run your Azure Function on Linux rather than Microsoft Windows, see the Microsoft documentation: Azure Functions (https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-vs-code?pivots=programming-language-javascript).
Azure AD authentication is not available on Linux when using the “Consumption” pricing plan for Azure Functions. You must use an “App Service” pricing plan or “Premium” pricing plan in order to authenticate with Azure AD.
For more details, see the Microsoft documentation: Azure AD (https://docs.microsoft.com/en-us/azure/app-service/configure-authentication-provider-aad).
Create an HTTP-triggered Azure function¶
After you create your Azure Functions app (container), you need to create an Azure Function in the container. This function acts as the remote service.
Microsoft allows Azure Functions to be called (“triggered”) different ways. A Snowflake external function invokes a remote service via an HTTP POST command, so the Azure Function you create must be an “HTTP-triggered function”.
Tip
You can use the instructions provided by Microsoft to create the HTTP-triggered function:
Create an app portal (https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-function-app-portal)
Create an Azure function (https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-first-azure-function#create-function)
However, Snowflake provides custom instructions that include additional details and sample code, and suggest a different authorization level than Microsoft. We suggest using the custom instructions in place of Microsoft’s instructions.
Create the function¶
To perform the tasks described in this section, you should be in the Function App screen in the Azure Portal. The name of your Azure Functions app should be displayed, typically near the upper left corner of the screen.
To create the HTTP-triggered function:
In the left-hand side menu tree, look for the section titled Functions. In that section, click on the item labeled Functions to add a function.
Click on the + Add button.
Select HTTP trigger from the list of potential triggers on the right.
Enter the name to use for your HTTP-triggered function.
Record this name in the
HTTP-Triggered Function name
field in your tracking worksheet.Enter the Authorization level.
Snowflake recommends choosing Function as the authorization level.
For more information about possible authorization levels, see the Microsoft documentation: HTTP-triggered functions (https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp#configuration).
Click on the button titled Add.
This takes you to a screen that shows the function name and, below that, the word Function.
In the tree menu on the left-hand side, click on Code + Test.
Replace the default code with your own code.
Sample code for a JavaScript “echo” function is provided below.
The function reads each row, then copies the row to the output (results). The row number is also included in the output. The output is returned as part of a multi-level dictionary.
This function accepts and returns data in the same format (JSON) that Snowflake sends and reads. For more details about data formats, see Remote Service Input and Output Data Formats .
Normally, the function returns HTTP code 200. If no rows are passed to the function (i.e. if the request body is empty), the function returns error code 400.
module.exports = async function(context, request) { context.log('JavaScript HTTP trigger function processed a request.'); if (request.body) { var rows = request.body.data; var results = []; rows.forEach(row => { results.push([row[0], row]); }); results = {data: results} context.res = { status: 200, body: JSON.stringify(results) }; } else { context.res = { status: 400, body: "Please pass data in the request body." }; } };
Click on the Save button above the code.
Test the function¶
To test the HTTP-triggered Azure Function you just created, paste the following sample data into the Body field and click on the Test/Run button:
{ "data": [ [ 0, 43, "page" ], [ 1, 42, "life, the universe, and everything" ] ] }
The content of the output should be similar to the following:
{ "data": [ [ 0, [ 0, 43, "page" ] ], [ 1, [ 1, 42, "life, the universe, and everything" ] ] ] }
Note that the formatting might be different from what is shown above.
Next step¶
Step 2: Create the proxy service (Azure API Management service) in the Portal