[英]An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500). Calling the endpoint from lambda+ api gateway
My sagemaker endpoint seems to be working while calling from lambda function but when I am using same payload from api gateway rest api (rest api deployed on api gateway as ANY Method with LAMBDA API INTEGRATION ENABLED), it is giving following error My sagemaker endpoint seems to be working while calling from lambda function but when I am using same payload from api gateway rest api (rest api deployed on api gateway as ANY Method with LAMBDA API INTEGRATION ENABLED), it is giving following error
Part of lambda function code from where I am calling SageMaker Endpoint我调用 SageMaker 端点的部分 lambda function 代码
@app.post("/dept_predictor")
@tracer.capture_method
def dept_predictor():
# post_data: dict = app.current_event.json_body
# try:
print("Received event: " + json.dumps( app.current_event.body, indent=2))
payload = app.current_event.body
print(payload)
# raise Exception('Malformed input ...')
response = runtime_client.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=json.dumps(payload),
Accept='Accept')
print(response)
Lambda function CloudWatch log after calling lambda function from api gateway Lambda function 从 api 网关调用 lambda function 后的 CloudWatch 日志
[ERROR] ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
SageMaker Endpoint Cloudwatch log SageMaker 端点 Cloudwatch 日志
2023-01-19T00:14:05.961+05:30 Loading the hash vectorizer model ...
2023-01-19T00:14:05.961+05:30 2023-01-18 18:44:05,959 ERROR - sklearn_functions - Exception on /invocations [POST]
2023-01-19T00:14:05.961+05:30 Traceback (most recent call last): File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper return fn(*args, **kwargs) File "/opt/ml/code/sklearn_functions.py", line 130, in input_fn pname = json.loads(request_body)["product_name"]
2023-01-19T00:14:05.962+05:30 TypeError: string indices must be integers
2023-01-19T00:14:05.962+05:30 During handling of the above exception, another exception occurred:
2023-01-19T00:14:05.962+05:30 Traceback (most recent call last): File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request() File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request rv = self.handle_user_exception(e) File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception reraise(exc_type, exc_value, tb) File "/miniconda3/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise raise value File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request rv = self.dispatch_request() File "/miniconda3/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 200, in transform self._model, request.content, request.content_type, request.accept File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_transformer.py", line 227, in _default_transform_fn data = self._input_fn(content, content_type) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 95, in wrapper six.reraise(error_class, error_class(e), sys.exc_info()[2]) File "/miniconda3/lib/python3.7/site-packages/six.py", line 702, in reraise raise value.with_traceback(tb) File "/miniconda3/lib/python3.7/site-packages/sagemaker_containers/_functions.py", line 93, in wrapper return fn(*args, **kwargs) File "/opt/ml/code/sklearn_functions.py", line 130, in input_fn pname = json.loads(request_body)["product_name"]
2023-01-19T00:14:05.962+05:30 10.32.0.2 - - [18/Jan/2023:18:44:05 +0000] "POST /invocations HTTP/1.1" 500 290 "-" "AHC/2.0"
2023-01-19T00:14:05.962+05:30 sagemaker_containers._errors.ClientError: string indices must be integers
I tried to check this similar GitHub issue which is not resolved yet.我试图检查这个尚未解决的类似GitHub 问题。 Any lead on this is highly appreciated.对此的任何领先都表示高度赞赏。 Thanks!谢谢!
Figured out the issue.想通了这个问题。 When I am calling from using api gate rest api same payload is getting transfer without using lambda api proxy integration and ANY method setup as string.当我使用 api 门 rest api 调用时,无需使用 lambda api 代理集成和任何方法设置为字符串即可传输相同的有效负载。 So converting string payload to dict payload solved the issue所以将字符串有效负载转换为字典有效负载解决了这个问题
payload = json.loads(payload)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.