Lambda:向SQS发送消息 [英] Lambda : Send a message to SQS
问题描述
我尝试升级我的Lambda之一,但无法使其正常工作... 所以我有一个lambda来管理我的条纹付款,一切正常.
I tried to upgrade one of my lambda but can't make it work... so I have a lambda to manage my stripe payment, everything work fine.
付款成功后,我想向SQS发送消息.
I want to send a message to SQS when a payment is OK.
您可以在下面看到我的lambda函数:
You can see my lambda function below :
const stripe = require('stripe')("sk_test_xXxXxXxXxX");
const ApiBuilder = require('claudia-api-builder');
const querystring = require('querystring');
var api = new ApiBuilder();
var AWS = require('aws-sdk');
var sqs = new AWS.SQS({region : 'eu-west-1'});
var queueUrl = 'https://sqs.eu-west-1.amazonaws.com/xXXxXXxXXx/xXxXxX-new-site.fifo';
api.post('/stripe',request => {
console.log(request);
let params = querystring.parse(request.body);
console.log(params);
return stripe.charges.create({
amount: params.package,
currency: 'eur',
description: `12 month charge`,
source: params.stripeToken,
receipt_email: params.email,
metadata: {domain_name: params.domain_name, email: params.email},
}).then(charge => {
var responseBody = {
message: ''
};
var responseCode = 200;
var message = {
MessageBody: "TEST",
QueueUrl: queueUrl
};
console.log(message);
sqs.sendMessage(message, function(err, data) {
console.log(err);
console.log(data);
if (err) {
console.log('error:', "failed to send message" + err);
var responseCode = 500;
} else {
console.log('data:', data.MessageId);
responseBody.message = 'Sent to ' + queueUrl;
responseBody.messageId = data.MessageId;
}
var response = {
statusCode: responseCode,
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(responseBody)
};
return response;
});
return charge;
}).catch((err) => {
return err;
});
});
module.exports = api;
我在第25行和第59行之间添加了代码.
I added the code betweend line 25 and 59.
在cloudwatch日志上,我可以看到第36行的控制台输出(console.log(message);),但从第39行什么也看不到.
On cloudwatch logs I can see the console output from line 36 ( console.log(message);) but nothing from the line 39.
感谢您的帮助:)
推荐答案
我对AWS Lambda并不熟悉,但是我看到您使用回调(sqs.sendMessage())执行异步代码并立即调用return charge
无需等待执行结果(sqs.sendMessage())
I'm not so experienced in AWS Lambda, but I see that you execute async code with callback (sqs.sendMessage()) and immediately call return charge
without awaiting of result of such execution (sqs.sendMessage())
我不太确定Lambda应该返回什么(可能是response
,而不是charge
),但是如果您想在sqs.sendMessage()
调用中查看您的console.log
结果,则代码可能看起来像这个:
I'm not pretty sure about what Lambda should return (probably, response
, not charge
), but if you want to see your console.log
results inside sqs.sendMessage()
call, the code might look something like this:
// above code ommitted
}).then(async charge => {
//code ommitted
console.log(message);
// if Lambda supports async/await OR return this promise and process it in next chain
await sqs.sendMessage(message)
.promise()
.then(data=>{
// process data here
})
.catch(err=>{
// process error here
})
return charge;
}).catch((err) => {
return err;
});
});
这篇关于Lambda:向SQS发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!