简体   繁体   English

我如何使用 API 网关调用 sagemaker 推理端点

[英]How can i call sagemaker inference endpoint using API gateway

I am trying to call sagemaker inference endpoint from api gateway with AWS Integration.I don't want to use lamdba in between of API gateway and sagemaker runtime.我正在尝试使用 AWS 集成从 api 网关调用 sagemaker 推理端点。我不想在 API 网关和 sagemaker 运行时之间使用 lamdba。 I followed this doc to setup api gateway method but it fails.我按照这个文档来设置 api 网关方法,但它失败了。

How can i call sagemaker inference endpoint from API gateway?如何从 API 网关调用 sagemaker 推理端点?

Web Browser ----> API Gateway ----> Sagemaker endpoint Web 浏览器 ----> API 网关 ----> Sagemaker 端点

API Gateway supports integration with AWS services directly (without the Lambda). API Gateway 支持直接与 AWS 服务集成(无需 Lambda)。 You can follow the instructions at https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html .您可以按照https://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started-aws-proxy.html 上的说明进行操作。

When you go to Step 4 in the instructions above, for the AWS Service option, you can choose 'SageMaker Runtime' to target the invoke endpoints.当您转到上述说明中的第 4 步时,对于 AWS 服务选项,您可以选择“SageMaker Runtime”以定位调用终端节点。

Invocation of the SageMaker Runtime API will require proper IAM permissions which is why Lambda is recommended within the architecture.调用 SageMaker Runtime API 将需要适当的 IAM 权限,这就是在架构中推荐 Lambda 的原因。 Without Lambda, you would need to handle the SageMaker InvokeEndpoint call (and AWS signature) passing through API gateway which isn't a best practice for a few different reasons.如果没有 Lambda,您将需要处理通过 API 网关的 SageMaker InvokeEndpoint 调用(和 AWS 签名),由于一些不同的原因,这不是最佳实践。

Here is an example of a simple Lambda python function to handle the InvokeEndpoint call.下面是一个处理 InvokeEndpoint 调用的简单 Lambda python 函数示例。 This will pass input/output through API gateway to SageMaker, and then a prediction back to client side.这将通过 API 网关将输入/输出传递给 SageMaker,然后将预测返回给客户端。

import os
import io
import boto3
import json
import csv

ENDPOINT_NAME = os.environ['ENDPOINT_NAME']

runtime = boto3.client('runtime.sagemaker')

def handler(event, context):

    print("data: " + json.dumps(event))

    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       ContentType='text/csv',
                                       Body=event)

    result = json.loads(response['Body'].read().decode())

    print(result)

    return result

Here is API reference for more information: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html以下是 API 参考以获取更多信息: https : //docs.aws.amazon.com/sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html

Hope this helps!希望这可以帮助!

it's a long shot since it's an old question but somebody might end up here.这是一个很长的镜头,因为这是一个老问题,但有人可能会在这里结束。

Reading the first section of the documentation about calling the inference endpoint in sagemaker, you'll find that you can only call it with a POST and pass your input data in the body.阅读有关在 sagemaker 中调用推理端点的文档的第一部分,您会发现您只能使用 POST 调用它并在正文中传递您的输入数据。

https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_runtime_InvokeEndpoint.html

So it might be that you created a GET method in API Gateway and that you need to map your request parameters to a body payload or simply set up a POST method instead.因此,您可能在 API Gateway 中创建了一个 GET 方法,并且您需要将请求参数映射到正文有效负载,或者只是设置一个 POST 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM