当我尝试将 lambda 函数集成到 AWS 的 API 网关中时出现错误消息 当我检查终端节点 URL 时



这是我 main.py 文件,其中定义了lambda函数。当我尝试使用以下命令在 linux 终端中执行此操作时,我正在获取输出。

linux 终端中使用的命令:sudo serverless invoke local -f aimodel-lambda -d '{"body":[500]}' 输出:

"StatusCode": 200,
"body": 89.01565538975994
}

main.py 文件:

import pickle
import boto3
from botocore.vendored import requests
from sklearn.linear_model import LinearRegression
import numpy as np
import json
def get_model():
s3_client = boto3.client("s3")
# s3client = boto3.client("s3")
# s3 = boto3.resource("s3")
s3 = boto3.resource("s3")
bucket_name ='deploy-simpleml'
bucket = s3.Bucket(bucket_name)
s3_client.download_file('deploy-simpleml','finalized_model.sav','/tmp/finalized_model.sav')
model = pickle.load(open("/tmp/finalized_model.sav",'rb'))
return model
# # bucket_name = 'deploy-simpleml'
# # bucket = s3.Bucket(bucket_name) 
# bucket= boto3.resource('s3').Bucket(bucket_name)
# s3_client.download_file('model/finalized_model.sav','/tmp/finalized_model.sav')
# model = pickle.load(open("finalized_model.sav",'rb'))
# return model
#    # bucket.download_file('model/finalized_model.sav','/tmp/finalized_model.sav')
#    # model= pickle.load(open("finalized_model.sav",'rb'))
#    # return model
def predict(event):
sample =event['body']
sample=np.array(sample)
sample = sample.reshape(-1,1)
# x = x.values.reshape(-1,1)
model = get_model()
result = model.predict(sample)
return result
def lambda_handler(event,context):
result = predict(event)
response = {'StatusCode':200,
'body': result[0]}
return response

我通过"serverless.yml"文件触发代码:

无服务器 .yml 文件

service : test
plugins:
- serverless-python-requirements
provider:
name: aws
runtime: python3.6
region : ap-southeast-2
deploymentBucket:
name : deploy-simpleml
IamRole:
Type: AWS::IAM::Role
Properties:
RoleName: "test-deploy-dev-ap-southeast-2-lambdaRole"
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal: "*"
Service: lambda.amazonaws.com
Action:
- s3.*
- s3.GetObject
- lambda:InvokeFunction
- lambda:InvokeAsync
- execute-api:Invoke
- execute-api:InvalidateCache

Resource:
- "arn:-----------------------------------given my method request ARN number-------"
# Condition:
#   StringEquals:
#     aws:sourceVpce": "9896d9ff"
custom:
pythonRequirements:
slim : true
pythonBin: /usr/bin/python3
environment:
SLS_DEBUG: serverless-deploy
functions:
# postApiTest:
#   handler: main.lambda_handler
#   name: post-api-test
#   events:
#     - http:
#         path: dev
#         method: post
#         private: true
aimodel-lambda:
name: aimodel-lambda-function
description : deploy trained linear reg model on aws lambda using serverless
handler : main.lambda_handler
events :
- http:

path: GET/testapi
method: GET
private: true 

然后,我创建了API网关,通过将资源和方法添加为GET并在集成请求中,我在映射模板中选择"application/json",并在编辑器中通过选择"空"给出了以下命令。

{
"body" : "$input.params("500")"
}

在方法执行中,在 URL 查询字符串参数中,我给出了正文。

作为参考,我添加了字符串参数屏幕截图

当我检查端点URL时,我面临以下错误:

{"errorMessage": "'body'", "errorType": "KeyError", "stackTrace": [["/var/task/main.py", 38, "lambda_handler", "result = predict(event)"], ["/var/task/main.py", 29, "predict", "sample =event['body']"]]}

当我单击测试并在查询字符串中给出参数时,方法执行查询字符串

我收到以下错误。请帮我解决这个问题。

身体反应:

{
"errorMessage": "float() argument must be a string or a number, not 'dict'",
"errorType": "TypeError",
"stackTrace": [
[
"/var/task/main.py",
38,
"lambda_handler",
"result = predict(event)"
],
[
"/var/task/main.py",
34,
"predict",
"result = model.predict(sample)"
],
[
"/var/task/sklearn/linear_model/_base.py",
225,
"predict",
"return self._decision_function(X)"
],
[
"/var/task/sklearn/linear_model/_base.py",
207,
"_decision_function",
"X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])"
],
[
"/var/task/sklearn/utils/validation.py",
531,
"check_array",
"array = np.asarray(array, order=order, dtype=dtype)"
],
[
"/var/task/numpy/core/_asarray.py",
85,
"asarray",
"return array(a, dtype, copy=False, order=order)"
]
]
}

第34 行的 Numpy 重塑会创建一个数组。

您的模型需要字符串或整数,这就是返回此错误的原因。

相关内容

  • 没有找到相关文章

最新更新