【发布时间】:2021-02-12 01:10:10
【问题描述】:
TLDR
我不知道如何格式化使用无服务器创建的 AWS Lambda API 的发布请求
当前实施
我有一个使用 serverless 框架创建的 Lambda 函数。我有两个功能;一个打印所有模块及其版本的函数,另一个对通过 post 请求发送的数据进行预测的函数。它通过挂载 EFS 卷来处理依赖存储,这里是 serverless.yml
service: test3Predict
plugins:
- serverless-pseudo-parameters
custom:
efsAccessPoint: fsap-**********
LocalMountPath: /mnt/efs
subnetsId: subnet-**********
securityGroup: sg-**********
provider:
name: aws
runtime: python3.6
region: us-east-2
timeout: 20
package:
exclude:
- node_modules/**
- .vscode/**
- .serverless/**
- .pytest_cache/**
- __pychache__/**
functions:
ohPredict:
handler: handler.lambda_handler_OH
environment: # Service wide environment variables
MNT_DIR: ${self:custom.LocalMountPath}
vpc:
securityGroupIds:
- ${self:custom.securityGroup}
subnetIds:
- ${self:custom.subnetsId}
iamManagedPolicies:
- arn:aws:iam::aws:policy/AmazonElasticFileSystemClientReadWriteAccess
events:
- http:
path: ohPredict
method: get
fileSystemConfig:
localMountPath: '${self:custom.LocalMountPath}'
arn: 'arn:aws:elasticfilesystem:${self:provider.region}:#{AWS::AccountId}:access-point/${self:custom.efsAccessPoint}'
test:
handler: handler.test
environment: # Service wide environment variables
MNT_DIR: ${self:custom.LocalMountPath}
vpc:
securityGroupIds:
- ${self:custom.securityGroup}
subnetIds:
- ${self:custom.subnetsId}
iamManagedPolicies:
- arn:aws:iam::aws:policy/AmazonElasticFileSystemClientReadWriteAccess
events:
- http:
path: test
method: get
fileSystemConfig:
localMountPath: '${self:custom.LocalMountPath}'
arn: 'arn:aws:elasticfilesystem:${self:provider.region}:#{AWS::AccountId}:access-point/${self:custom.efsAccessPoint}'
似乎测试正在运行;如果我运行 sls deploy 并在浏览器中输入我的 test 函数的 URL,我会得到预期的输出。
问题
ohPredict 功能无法正常工作。当我使用requests python 模块向它发送发布请求时,
url = 'https://****.execute-api.****.amazonaws.com/dev/ohPredict'
myobj = {"data": data}
x = requests.post(url, json=myobj)
res = eval(x.text)
print(res)
我收到以下错误:
{'message': 'Missing Authentication Token'}
我用一个简单的 lambda 函数和一个 post 请求运行了一个测试,而没有使用无服务器,这就是我需要的所有代码。我想我会尝试尝试并提供一些值,我最终得到了这个:
url = 'https://****.execute-api.****.amazonaws.com/dev/ohPredict'
myobj = {"data": data}
x = requests.post(url, headers={'Authorization': 'FOO'}, json=myobj)
res = eval(x.text)
print(res)
导致:
{'message': "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=FOO"}
我尝试提供其他值,但输出没有任何变化。如何让我的函数接收我的发布请求?
【问题讨论】:
-
API Gateway 错误消息可能会产生误导,我在您的
serverless.yml中看不到任何身份验证。但是您将 Lambda 指定为仅接受GET和method: get,但随后您执行POST,您需要使用method: post让您的函数接受POST。 -
您知道,我只是在查看了他们的一个帖子请求示例后才想到这一点。我会试试看。
标签: post aws-lambda serverless-framework