第 2 步:在控制台中创建代理服务 (Google Cloud API Gateway)¶
Snowflake 不会直接向远程服务发送数据(HTTP POST 请求)。相反,Snowflake 会将数据发送到代理服务,该代理服务会将数据从 Snowflake 中继到远程服务(即 GCP Cloud Function),然后再返回。
本主题说明了有关创建和配置 Google Cloud API Gateway,以用作外部函数的代理服务。
上一步¶
创建 API 定义¶
在本地文件系统中,创建并自定义一个 YAML 格式的配置文件,该文件指定您正在创建的 API。文件的扩展名应为 .yaml 或 .yml。
配置文件模板:
swagger: '2.0'
info:
title: API Gateway config for Snowflake external function.
description: This configuration file connects the API Gateway resource to the remote service (Cloud Function).
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/<PATH>:
post:
summary: Echo the input.
operationId: echo
x-google-backend:
address: <HTTP ENDPOINT TO ROUTE REQUEST TO>
protocol: h2
responses:
'200':
description: <DESCRIPTION>
schema:
type: string
填写或更新以下字段:
替换
<PATH>with a unique name. This will be incorporated into URLs, so use only characters that are valid in URLs. For example, enterdemo-func-resource。请注意,与此配置文件中的其他字段不同,请在冒号前而不是冒号后输入
<PATH>值。例如,以下是正确的:paths: /demo-func-resource:
路径名不应包含任何 路径参数 (https://swagger.io/docs/specification/2-0/describing-parameters/#path-parameters)。Google 在 设置 URL 路径 (https://cloud.google.com/api-gateway/docs/passing-data#setting_the_backend_service_address_and_path_in_the_openapi_spec) 时支持路径参数。但是,Snowflake 不支持 CREATE EXTERNAL FUNCTION 语句中指定的相应 URL 中的路径参数。
将上一步中的路径(例如
demo-func-resource)复制到跟踪工作表中的Path Suffix字段。找到
x-google-backend字段下的address字段,并用跟踪工作表中Cloud Function Trigger URL字段的值替换<HTTP ENDPOINT TO ROUTE REQUEST TO>。结果应类似于:x-google-backend: address: https:// ...
URL 不应放在引号内。
URL 不一定是 Google 托管的端点,也可以是任何 HTTP 端点的路径。
如果您在 第 1 步:在控制台中创建远程服务 (Google Cloud Function) 中选择了 Require HTTPS,请确保在
address字段中输入的 URL 以https开头。您可以选择更新以下任何值:
info部分中的title。info部分中的description。paths部分的post子部分中的operationId。paths部分的post子部分中的summary。
查看示例配置文件。应该与下面的内容相似:
swagger: '2.0' info: title: "API Gateway config for Snowflake external function" description: "This configuration file connects the API Gateway resource to the remote service (Cloud Function)." version: 1.0.0 schemes: - https produces: - application/json paths: /demo-func-resource: post: summary: "echo the input" operationId: echo x-google-backend: address: https://my_dev.cloudfunctions.net/demo-cloud-function-01 protocol: h2 responses: '200': description: echo result schema: type: string备注
此配置将使您的网关对公众开放,直到您在本教程 第 5 步:在控制台中为代理服务创建 GCP 安全策略 中对其进行保护。
或者,要确保没有人可以同时使用您的网关,请向配置文件添加一个安全定义,该定义使用一个临时的无效服务账户名 (
google_service_account),如该可选步骤中所述。在此步骤中添加此安全定义意味着在完成 第 5 步:在控制台中为代理服务创建 GCP 安全策略 中的安全配置之前,您无法测试外部函数。具体来说,在 第 4 步:在 Snowflake 中为 GCP 创建外部函数 中测试外部函数的指令还不会起作用。将以下
securityDefinitions部分添加到配置文件schemes部分的正上方,并处于相同的缩进级别。securityDefinitions: <security-def-name>: authorizationUrl: "" flow: "implicit" type: "oauth2" x-google-issuer: "google_service_account" x-google-jwks_uri: "https://www.googleapis.com/robot/v1/metadata/x509/google_service_account"将
<security-def-name>替换为唯一的安全定义名称(例如snowflakeAccess01)。将此名称记录在跟踪工作表的
Security Definition Name字段中。
更新配置文件的
post:部分以引用上面创建的安全定义。在operationId字段下方,添加:security: - <security-def-name>: []
确保缩进与
operationId字段处于同一水平。将
<security-def-name>替换为跟踪工作表中Security Definition Name字段的值。请确保在安全定义名称之前包含连字符和空格,如上所示。
确保在冒号后包含空方括号 (
[])。
例如:
paths: /demo-func-resource: post: summary: "echo the input" operationId: echo security: - snowflakeAccess01: [] x-google-backend: address: https://my_dev.cloudfunctions.net/demo-cloud-function-01 protocol: h2
保存配置文件。
在跟踪工作表的
Configuration File Name字段中记录文件路径和名称。
要了解有关 API 配置文件的更多信息,请参阅以下 GCP 文档:
OpenAPI 概述 (https://cloud.google.com/api-gateway/docs/openapi-overview)。
创建 API 定义 (https://cloud.google.com/api-gateway/docs/quickstart-console#creating_an_api_definition)。
创建 API Gateway¶
要创建 API Gateway,请执行以下操作:
创建 GCP API。
创建 API Config。
使用 API Config 创建 Gateway。
创建 GCP API¶
此步骤创建一个 GCP API,它是一个容器,可以包含一个或多个 API Gateway 以及一个或多个配置文件:
如果您还没有这样做,请通过点击 GCP 菜单并选择 API Gateway 转到 Google Cloud API Gateway 屏幕。
点击 CREATE GATEWAY。
输入 Display Name 和 API ID (如
demo-api-display-name-for-external-function1和demo-api-id-for-external-function1)。您不需要在跟踪工作表中记录这些值,因为以后创建外部函数时不需要输入这些值。但是,您可能需要记录 API ID,以便在使用完毕后可以将其删除。
创建 API Config¶
将配置文件上传到控制台,创建 API Config。
滚动到屏幕的 API Config 部分。
搜索包含 Upload an API Spec 的字段。
点击 BROWSE,选择配置文件。配置文件的名称记录在跟踪工作表的
Configuration File Name字段中。在包含 Display Name 的字段中输入显示名称。
选择服务账户。
如果创建了示例函数,则在包含 Select a Service Account 的字段中,选择 App Engine default service account。
如果要创建要在生产中使用的函数(而不是作为示例),则可以选择其他服务账户。
所选服务账户必须具有适当的权限,包括调用 Cloud Function 的权限。
使用 API Config 创建网关¶
滚动到屏幕的 Gateway details 部分。
输入新 API Gateway 的 Display Name。
点击 Location 字段,选择适当的区域(如
us-central1)。点击 CREATE GATEWAY。
这将进入 APIs 屏幕,显示 APIs 的列表。
如果新的 API 不能立即显示,请等待几分钟,然后点击 Refresh 按钮。
将 API 的 Managed Service 值复制到跟踪工作表中的
Managed Service Identifier字段。此时,您仍然可以看到 APIs 的列表。点击 API 的名称。
您应该会看到 4 个选项卡:OVERVIEW、DETAILS、CONFIGS 和 GATEWAYS。
点击 GATEWAYS 选项卡。
将 Gateway URL 复制到跟踪工作表中的
Gateway Base URL字段。