Step 1: Create the Remote Service (AWS Lambda Function) in the Management Console

This topic provides detailed instructions for creating an AWS Lambda Function for use as the remote service for your external function.

This topic includes code for a sample Lambda Function that you can use as-is to create your first external function, or that you can use as a starting point for a custom Lambda Function.

In this Topic:

Introduction

There are multiple ways to create a remote service on AWS. This topic shows one way, which is to create a Lambda Function to used as the remote service.

Snowflake provides two sample Lambda Functions, each of which is written in Python.

More details are provided in Snowflake Sample Functions (in this topic).

Understanding Lambda Function Input and Output

In order for Snowflake to send data to and receive data from your remote service, your remote service must accept and return data in JSON format.

Platform-independent information about remote service input and output is in Remote Service Input and Output Data Formats .

This section provides details that are specific to AWS Lambda Functions.

Language-independent Input and Output via JSON

The information in this section applies to all Lambda Functions used as remote services for Snowflake external functions. For platform-specific information about external function input and output, see the sub-section(s) below.

On AWS, the convention for an HTTP-compatible service is to return the body inside a JSON object that also includes the HTTP status code. The JSON for a typical return value from an AWS Lambda function looks like the following:

{
"statusCode": <http_status_code>,
"body":
        {
            "data":
                  [
                      [ 0, <value> ],
                      [ 1, <value> ]
                      ...
                  ]
        }
}

The structure of the JSON input is similar to the preceding, but includes additional key-value pairs that you are unlikely to need, and excludes the statusCode.

Python-specific Lambda Function Input

The following material applies to Snowflake-compatible Python-language Lambda Functions, including the sample Lambda Functions provided by Snowflake. This information is in addition to the language-independent rules for input and output.

A Snowflake-compatible Python-Language Lambda Function receives two parameters, event and context. Simple external functions typically need only the event parameter.

The event parameter includes many sub-fields, one of which is body. The body is a JSON-compatible string that contains a dictionary.

The dictionary includes a key named data; the corresponding value for data is an array. That array contains the rows passed by Snowflake.

Each row is represented by an array that is nested inside the data array.

(Because AWS Lambda conveniently processes the HTTP POST request sent by Snowflake, extracts the body, and passes the body inside the event parameter, the example functions provided by Snowflake do not need to parse the entire HTTP POST request.)

The Sample Synchronous Lambda Function includes code showing how to read the event parameter.

Choose the Code for the Lambda Function

Snowflake Sample Functions

Snowflake supplies two sample functions:

  • The shorter example is synchronous. If you are new to external functions or Lambda Functions, Snowflake recommends that you use this to create your first sample external function.

    Experienced users can also copy and modify it to use as a starting point for custom remote services.

    The code is available in sample synchronous Lambda Function.

  • The other example is asynchronous.

    This sample is intended primarily as a sample for building customized asynchronous remote services.

    The code is available in sample asynchronous Lambda Function.

Custom Lambda Function

You can write your own Lambda Function from scratch, or you can use one of the functions described in Snowflake Sample Functions (in this topic) as a starting point.

If you have an existing remote service that you want to use, then you can skip most of the instructions in this step of the tutorial. Instead, do the following:

  1. Record your AWS Account ID in the “AWS Account ID” field in the tracking worksheet.

  2. Record the remote service’s Lambda Function name in the “Lambda Function Name” field in the tracking worksheet.

  3. Go to Step 2: Create the Proxy Service (Amazon API Gateway) in the AWS Management Console.

Create a Lambda Function

To create an AWS Lambda Function, follow the steps below.

Note

Although these steps show you how to create the sample remote services provided by Snowflake, you can use these steps as a model for creating your own customized remote service. If you create a custom Lambda Function, then modify the steps below as appropriate (e.g. choose the appropriate programming language for your remote service’s code).

  1. Log into the AWS Management Console, if you haven’t already.

  2. If you have not already recorded your AWS account ID in the worksheet field named “Your AWS Account ID”, record it now.

    If you need to look up your AWS account ID, follow the AWS instructions.

  3. Select Lambda.

  4. Select Create function.

  5. Enter a function name.

    Record this name in the “Lambda Function Name” field in the worksheet.

  6. Select the programming language to use. If you are using one of the sample Python functions provided by Snowflake, then choose Python 3.7.

  7. Choose or create an execution role for this function.

    Select the appropriate option(s), typically Create a new role with basic Lambda permissions.

    (This role is separate from your cloud account role and separate from your Snowflake role(s).)

  8. Click on the Create Function button.

  9. In the lambda_function tab, enter the code for the function.

    If you have not already written your own function, you can use the following example provided by Snowflake:

    Tip

    If you cannot paste into the edit window, try double-clicking on the function’s file name to enable editing.

  10. Click on the Deploy button to deploy the function.

Test the Lambda Function

Snowflake strongly recommends that you test your Lambda Function.

For the sample synchronous Python function provided by Snowflake, use the following test data (replace any default data with the data below):

{
  "body":
    "{ \"data\": [ [ 0, 43, \"page\" ], [ 1, 42, \"life, the universe, and everything\" ] ] }"
}

The execution results should be similar to:

{
  "statusCode": 200,
  "body": "{\"data\": [[0, [\"Echoing inputs:\", 43, \"page\"]], [1, [\"Echoing inputs:\", 42, \"life, the universe, and everything\"]]]}"
}

(The format might differ.)

If the preceding worked correctly, you now have an AWS Lambda function that you can use as the remote service for your external function.