规划用于 AWS 的外部函数

借助本主题,您可以使用 Snowflake 提供的 AWS 管理控制台或 AWS CloudFormation 模板为创建用于 AWS (Amazon Web Services) 的外部函数做好准备。

先决条件

这些说明假设您是经验丰富的 AWS 管理控制台用户。

您需要:

  • 具有 AWS 账户,包括具有以下权限:

    • 通过 IAM (身份和访问管理)创建 AWS 角色。
    • 创建 AWS Lambda 函数。
    • 创建 API 网关端点。
  • 一个 Snowflake 账户,该账户具有 ACCOUNTADMIN 权限,或具有已获得 CREATE INTEGRATION 权限的角色。

  • If you plan to use a private endpoint, you need your Virtual Private Cloud (VPC) ID. (You must use a VPC ID, not a VPC Endpoint ID. VPC Endpoint IDs can change over time.)

如果还没有 VPC ID,可以通过在 Snowflake Web 界面中执行以下命令来查找它:

select system$get_snowflake_platform_info();

输出将类似于:

{
  "snowflake-vpc-id": ["vpc-12345678"],
  "snowflake-egress-vpc-ids": [
 ...
{
  "id": "vpc-12345678",
  "expires": "2025-03-01T00:00:00",
  "purpose": "generic"
},
...
]
 }

在函数输出中,对于使用 “purpose”: “generic” 标识的每个属性,记录相应的 VPC ID。

在决定是使用 AWS 管理控制台还是 AWS CloudFormation 模板创建外部函数后,将 VPC IDs 复制到相应的跟踪工作表:

选择端点类型:区域端点与专用端点

可以通过通常称为 端点 的 URI 访问代理服务(如 Amazon API Gateway)。创建 Amazon API Gateway 的说明要求您选择以下端点类型之一:

  • 区域端点。
  • 专用端点。

以下信息可帮助您选择端点类型。

可以跨 AWS 区域访问区域端点,甚至可以跨云平台访问。Snowflake 实例、代理服务和远程服务都可以位于不同的区域,甚至位于不同的云平台上。例如,在 Azure 上运行的 Snowflake 实例可以向 Amazon API Gateway 区域端点发送请求,而该端点又可以将数据转发到在 GCP 上运行的远程服务。

可以配置专用端点,以仅允许在区域内进行访问。例如,可以配置一个专用端点,以仅允许从同一 AWS 区域的 Snowflake VPC (虚拟私有云)进行访问。Snowflake VPC 和专用端点之间使用 AWS PrivateLink 进行通信。

有关 AWS 上的端点类型的更多详细信息,请参阅:

如果要使用专用端点,但不确定要使用哪个区域,可以通过执行以下任一操作来查找区域:

  • Call the SQL function CURRENT_REGION() (e.g. SELECT CURRENT_REGION()).
  • Check your Snowflake account hostname, which normally indicates the cloud provider and region. For more information about account hostnames, regions, and cloud providers, see Supported cloud regions.

要使用专用端点,账户必须符合以下要求:

  • Snowflake 的 Business Critical Edition(或更高版本)。

选择创建外部函数的方法

Snowflake 提供有关在 AWS 上创建外部函数的两种方法的说明:

  • AWS 管理控制台 Web 界面
  • Snowflake 提供的 AWS CloudFormation 模板

AWS 管理控制台

You can use the AWS Management Console (https://aws.amazon.com/console/) to create a Lambda Function (as the remote service) and an Amazon API Gateway instance (as the proxy service). If you choose this method, you also use the AWS Management Console to configure security-related settings.

使用 AWS 管理控制台创建外部函数的说明包含了一个示例 Lambda 函数,以及创建基本 API Gateway 的详细信息:

  • 新用户几乎无需修改即可使用这些说明。
  • Experienced users can use the instructions and sample Lambda Function as a starting point for creating a custom Lambda Function and a custom-configured API Gateway.

AWS CloudFormation 模板

创建外部函数时,CloudFormation 模板执行以下两个步骤:

  • 创建远程服务(AWS Lambda 函数)。
  • 创建和配置代理服务 (Amazon API Gateway)。

该模板还执行以下操作:

  • 创建两个 IAM 角色(一个用于 Lambda 函数,另一个用于 API Gateway)。
  • 为 API Gateway 配置资源策略。

准备使用 AWS 管理控制台

创建用于跟踪所需信息的工作表

创建外部函数时,应记录输入的特定信息(例如资源调用 URL),以便可以在后续步骤中使用该信息。下面的工作表可帮助您跟踪该信息。

===========================================================================
================ Tracking Worksheet: AWS Management Console ===============
===========================================================================

****** Step 1: Information about the Lambda Function (remote service) *****

Your AWS Account ID: ______________________________________________________

Lambda Function Name: _____________________________________________________


******** Step 2: Information about the API Gateway (proxy Service) ********

New IAM Role Name: ________________________________________________________

New IAM Role ARN: _________________________________________________________

Snowflake VPC ID (optional): ______________________________________________

New API Name: _____________________________________________________________

API Gateway Resource Name: ________________________________________________

Resource Invocation URL: __________________________________________________

Method Request ARN: _______________________________________________________


*** Step 3: Information about the API Integration and External Function ***

API Integration Name: _____________________________________________________

API_AWS_IAM_USER_ARN: _____________________________________________________

API_AWS_EXTERNAL_ID: ______________________________________________________

External Function Name: ___________________________________________________

准备使用 AWS CloudFormation 模板

下载模板

The template is available for download from the deployment templates directory (https://github.com/Snowflake-Labs/sfguide-external-functions-examples/tree/main/DeploymentTemplates/aws/BasicSetup.yaml) in the Snowflake repository in GitHub.

创建用于跟踪所需信息的工作表

创建外部函数时,应记录输入的特定信息(例如资源调用 URL),以便可以在后续步骤中使用该信息。下面的工作表可帮助您跟踪该信息。

===========================================================================
================== Tracking Worksheet: CloudFormation Template ============
===========================================================================

New IAM Role Name: ________________________________________________________

New IAM Role ARN: _________________________________________________________

Resource Invocation URL: __________________________________________________

API_AWS_IAM_USER_ARN: _____________________________________________________

API_AWS_EXTERNAL_ID: ______________________________________________________

用于构建 AWS 上的外部函数的其他资源

When you are ready to create your own remote service for your own external function, you might want to look at the examples of remote services based on Lambda Functions that are available in The Snowflake Labs. (https://github.com/Snowflake-Labs/sfguide-external-functions-examples)

后续步骤

AWS Management Console:

Step 1: Create the remote service (AWS Lambda function) in the Management Console

AWS CloudFormation template:

Step 1: Use the template to create the remote service (AWS Lambda function) and proxy service (API Gateway)