【发布时间】:2019-01-18 19:44:30
【问题描述】:
我有一个 serverless 函数,你可以找到下面的代码,这个函数是使用 serverless 框架部署在 aws lambda 上的。在这个函数中,我调用了一个外部 web api。
当我执行serverless invoke -f my_func 时,我得到了预期的响应。但是如果我运行 curl 命令它会失败并且我得到{"message": "Internal server error"}
这是我的 curl 命令:
curl -X GET \
https://0wb3echzu8.execute-api.us-east-1.amazonaws.com/dev/my_func \
-H 'cache-control: no-cache' \
-H 'postman-token: 1fc551c0-7010-e1e3-7287-6a0d82d1e91a'
这是我的代码:
var request = require("request");
var options = { method: 'GET',
url: 'https://api.coinmarketcap.com/v2/listings/',
headers:
{ 'postman-token': '090e284e-62ad-29f0-0f10-92ae14656a37',
'cache-control': 'no-cache' } };
module.exports.my_func = (event, context, callback) => {
request(options, function (error, response, body) {
if (error) { console.log(error); callback(null, error) }
console.log(body)
callback(null, body)
});
};
这是 serverless.yml 文件:
service: aws-nodejs
app: sonarsic
tenant: vincent
provider:
name: aws
runtime: nodejs6.10
functions:
my_func:
handler: handler.my_func
events:
- http:
path: my_func
method: get
cors: true
它必须与我函数中的 web api 调用有关。如果我不调用 web api,它就可以正常工作。
如果我通过 serverless logs -f my_func 检查日志,我只会获得使用无服务器调用的调用日志。
在发出 curl 命令时,我该怎么做才能找出我的函数内部出了什么问题?
在http部分添加cors:true并不能解决问题
【问题讨论】:
-
您可以尝试启用 CORS 吗?
cors: true下httpgist.github.com/shierro/0e88f5b2ada79ebd53f370e2734c0c0e -
你能检查cloudwatch上的错误并粘贴到这里吗?
-
@Theo cors:true 没有解决问题,好像没有cloudwatch日志
-
好的,您可以尝试跟踪日志吗?
serverless logs -f my_func -t并再次尝试执行 curl?如果没有显示日志,那么您的端点没有到达 lambda 函数 -
你也可以发布整个 serverless.yml 文件吗?
标签: aws-lambda serverless-framework serverless