TLDR
我不知道如何格式化使用无服务器创建的AWS Lambda API的post请求
当前实现
我有一个使用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模块的post请求时,
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请求运行了一个测试,没有使用serverless,这就是我需要的所有代码。我想我应该尝试一下并提供一些值,最后我得到了这样的结果:
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"}我尝试提供其他值,但没有在输出中得到任何更改。如何让我的post请求被我的函数接收?
发布于 2021-02-12 01:10:10
正如你在网上提到的,问题出在方法上。只需将method: get替换为method: post,即可修复此问题
https://stackoverflow.com/questions/66142935
复制相似问题