第 1 步:在管理控制台中创建远程服务(AWS Lambda 函数)

本主题详细说明如何创建 AWS Lambda 函数,以用作外部函数的远程服务。

本主题包含示例 Lambda 函数的代码,您可以按原样使用该代码创建第一个外部函数,也可以将其用作自定义 Lambda 函数的起点。

上一步

Planning an external function for AWS

简介

有多种方法可用于在 AWS 上创建远程服务。本主题介绍其中一种方法,即创建要用作远程服务的 Lambda 函数。

本教程介绍两个示例 Lambda 函数,每个函数都是用 Python 编写。

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

了解 Lambda 函数输入和输出

为使 Snowflake 能够向远程服务发送数据以及从远程服务接收数据,远程服务必须接受并返回 JSON 格式的数据。

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

本部分提供特定于 AWS Lambda 函数的详细信息。

通过 JSON 且与语言无关的输入和输出

本部分中的信息适用于作为 Snowflake 外部函数的远程服务的所有 Lambda 函数。有关外部函数输入和输出的平台特定信息,请参阅下面的子部分。

在 AWS 上,HTTP 兼容服务的惯例是返回也包含 HTTP 状态代码的 JSON 对象内的主体。来自 AWS Lambda 函数的典型返回值的 JSON 如下所示:

{
"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 特定的 Lambda 函数输入

The following material applies to Snowflake-compatible Python-language Lambda Functions, including the sample Lambda Functions in this tutorial. 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.

(因为 AWS Lambda 方便处理 Snowflake 发送的 HTTP POST 请求、提取主体,并将主体传递到事件参数内,Snowflake 提供的示例函数不需要解析整个 HTTP POST 请求。)

The Sample synchronous Lambda function includes code showing how to read the event parameter.

选择 Lambda 函数的代码

Snowflake 示例函数

Snowflake 提供两个示例函数:

  • 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.

有经验的用户还可以复制和修改此函数,以用作自定义远程服务的起点。

The code is available in sample synchronous Lambda Function.

此示例主要用于生成自定义异步远程服务。

The code is available in sample asynchronous Lambda Function.

自定义 Lambda 函数

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

如果您有要使用的现有远程服务,您可以跳过本教程中此步骤的大部分说明。请改为执行以下操作:

  1. Record your AWS Account ID in the Your 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.

创建 Lambda 函数

要创建 AWS Lambda 函数,请按照以下步骤操作。

Note

尽管这些步骤向您展示如何创建 Snowflake 提供的示例远程服务,但您可以按照这些步骤创建自己的自定义远程服务。如果您创建自定义 Lambda 函数,请根据需要修改以下步骤(例如,为远程服务的代码选择适当的编程语言)。

  1. 如果您尚未登录 AWS 管理控制台,请登录。

  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 (https://docs.aws.amazon.com/IAM/latest/UserGuide/console_account-alias.html#FindingYourAWSId).

  3. Select Lambda.

  4. Select Create function.

  5. 输入函数名称。

    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.10.

  7. 为此函数选择或创建执行角色。

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

(此角色不同于云账户角色,也不同于 Snowflake 角色。)

  1. Click on the Create Function button.
  2. In the lambda_function tab, enter the code for the function.

如果您尚未编写自己的函数,则可以使用 Snowflake 提供的以下示例:

sample synchronous code or the

Tip

如果您无法粘贴到编辑窗口中,请尝试双击函数的文件名以进行编辑。

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

测试 Lambda 函数

Click the down-arrow beside the Test button and select Configure test event. In the Event name field, type test.

对于 Snowflake 提供的示例同步 Python 函数,请使用以下测试数据(用以下数据替换任何默认数据):

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

Click Create, and then click Test.

执行结果应该类似于:

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

您现在有一个 AWS Lambda 函数,可以将其用作外部函数的远程服务。

后续步骤

Step 2: Create the proxy service (Amazon API Gateway) in the AWS Management Console