访问 lambda REST 时格式错误的 Lambda 代理响应
Malformed Lambda proxy response when access lambda REST
我使用 Javascript
创建将部署在 AWS Lambda 上并用作 REST 的简单函数。
它应该生成随机名称,但现在我使用硬编码值。
// NamingService.js
class NamingService {
generateName() {
return 'generated-name'
}
}
module.exports = NamingService;
我在这个文件中处理请求
const NamingService = require('./NamingService');
const responses = {
success: (data={}) => {
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
"body": JSON.stringify(data)
}
},
error: (error) => {
return {
"statusCode": error.code || 500,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
"body": JSON.stringify(error)
}
}
};
module.exports = {
generateName: (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const namingService = new NamingService();
namingService.generateName()
.then(name => {
callback(null, responses.success(name))
})
.catch(error => {
callback(null, responses.error(error))
})
}
};
我使用serverless
部署lambda函数
service: naming-service
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
memorySize: 128
timeout: 30
functions:
generateName:
handler: lambdaHandlers.generateName
events:
- http:
path: jester/name/generate
method: get
cors:
origins:
- '*'
在 serverless deploy
之后显示了响应
region: eu-west-1
stack: naming-service-dev
api keys:
None
endpoints:
GET - https://endpoint/jester/name/generate
functions:
generateName: naming-service-dev-generateName
但是当我尝试 运行 它时(在 API 网关上使用测试)我得到这个错误
Fri May 11 21:37:19 UTC 2018 : Endpoint response body before transformations: "generated-name"
Fri May 11 21:37:19 UTC 2018 : Endpoint response headers: {Content-Length=16, Date=Fri, 11 May 2018 21:37:19 GMT;sampled=0, Content-Type=application/json}
Fri May 11 21:37:19 UTC 2018 : Execution failed due to configuration error: Malformed Lambda proxy response
Fri May 11 21:37:19 UTC 2018 : Method completed with status: 502
问题是什么?
问题出在 generateName
处理程序上。我不应该在方法 generateName
上使用 then
generateName: (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const namingService = new NamingService();
const generatedName = namingService.generateName();
callback(null, responses.success(generatedName));
}
我使用 Javascript
创建将部署在 AWS Lambda 上并用作 REST 的简单函数。
它应该生成随机名称,但现在我使用硬编码值。
// NamingService.js
class NamingService {
generateName() {
return 'generated-name'
}
}
module.exports = NamingService;
我在这个文件中处理请求
const NamingService = require('./NamingService');
const responses = {
success: (data={}) => {
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
"body": JSON.stringify(data)
}
},
error: (error) => {
return {
"statusCode": error.code || 500,
"headers": {
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
},
"body": JSON.stringify(error)
}
}
};
module.exports = {
generateName: (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const namingService = new NamingService();
namingService.generateName()
.then(name => {
callback(null, responses.success(name))
})
.catch(error => {
callback(null, responses.error(error))
})
}
};
我使用serverless
部署lambda函数
service: naming-service
provider:
name: aws
runtime: nodejs8.10
region: eu-west-1
memorySize: 128
timeout: 30
functions:
generateName:
handler: lambdaHandlers.generateName
events:
- http:
path: jester/name/generate
method: get
cors:
origins:
- '*'
在 serverless deploy
之后显示了响应
region: eu-west-1
stack: naming-service-dev
api keys:
None
endpoints:
GET - https://endpoint/jester/name/generate
functions:
generateName: naming-service-dev-generateName
但是当我尝试 运行 它时(在 API 网关上使用测试)我得到这个错误
Fri May 11 21:37:19 UTC 2018 : Endpoint response body before transformations: "generated-name"
Fri May 11 21:37:19 UTC 2018 : Endpoint response headers: {Content-Length=16, Date=Fri, 11 May 2018 21:37:19 GMT;sampled=0, Content-Type=application/json}
Fri May 11 21:37:19 UTC 2018 : Execution failed due to configuration error: Malformed Lambda proxy response
Fri May 11 21:37:19 UTC 2018 : Method completed with status: 502
问题是什么?
问题出在 generateName
处理程序上。我不应该在方法 generateName
then
generateName: (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
const namingService = new NamingService();
const generatedName = namingService.generateName();
callback(null, responses.success(generatedName));
}