我使用 Serverless 创建了一个 API Gateway+Lambda,以使用如下 serverless.yml 公开
/query
端点:
functions:
query:
name: mylambda-query
runtime: provided.al2
handler: native.handler
provisionedConcurrency: 1
memorySize: 256
events:
- http:
path: /query
method: post
integration: lambda # Non-proxy integration
request:
template:
application/json: |
$input.json('$')
response:
headers:
Access-Control-Allow-Origin: "'www.example.com'"
Access-Control-Allow-Methods: "'POST,OPTIONS'"
Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'"
template: |
$input.body
statusCodes:
200:
pattern: ''
template: $input.body
dependsOn:
- EfsMountTarget1
- EfsMountTarget2
- EfsAccessPoint
fileSystemConfig:
localMountPath: /mnt/data
arn:
Fn::GetAtt: [EfsAccessPoint, Arn]
package:
artifact: target/function.zip
environment:
QUARKUS_LAMBDA_HANDLER: query
QUARKUS_PROFILE: prod
一切似乎都工作正常,除了当我用以下方法测试我的端点时:
curl -H "Origin: http://www.example.com" -H "Content-Type: application/json" -X POST -d '{"somekey":"somedata"}' https://myappid.execute-api.us-east-1.amazonaws.com/dev/query
它响应错误:
{"errorType":"java.lang.IllegalArgumentException","errorMessage":"no data"}
所以我更新了我的 lambda 以记录正文和标头:
LOG.info("Received body: " + event.getBody());
Map<String, String> headers = event.getHeaders();
LOG.info("Received headers: " + headers);
我的日志显示:
Received body: null
Received headers: null
为什么 API 网关不将任何内容传递给 lambda,即使请求模板使用
$input.json('$')
指定了所有内容?
如果我手动检查 API 网关的“集成请求”选项卡上的参数,我注意到
Input passthrough
设置为 Never
。这可能是问题所在吗?如何正确更新我的无服务器配置?
设置您的无服务器框架项目: 如果您还没有安装 Serverless Framework 并创建一个新项目:
npm install -g 无服务器 无服务器创建 --template aws-nodejs --path my-service cd 我的服务