问题
I'm working with the Amazon Transcribe service and trying to get CloudWatch Events to fire a Lambda function that executes a POST request to my API.
Here's the Lambda function
var querystring = require('querystring');
var http = require('http');
exports.handler = function(event, context) {
var post_data = querystring.stringify(
event
);
// An object of options to indicate where to post to
var post_options = {
host: '193e561e.ngrok.io',
port: '80',
path: '/api/lambda',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function(chunk) {
console.log('Response: ' + chunk);
context.succeed();
});
res.on('error', function(e) {
console.log("Got error: " + e.message);
context.done(null, 'FAILURE');
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
I've configured CloudWatch Events to listen to the Amazon Transcribe service and specifically for the status of a job changing to COMPLETED
or FAILED
.
What's surprising however is that there's no mention of the Transcribe job name in that event response.
Here's an example:
'version' => '0',
'id' => '1fa5cca6-413f-4a0f-0ba2-66efa49c247e',
'detail-type' => 'Transcribe Job State Change',
'source' => 'aws.transcribe',
'account' => '405723091079',
'time' => '2019-11-19T19:04:25Z',
'region' => 'eu-west-1',
'detail' => NULL,
This is the only way I can think of my app working where the transcription job is invoked via the Amazon Transcribe service and then when it's done, hit my API to update the necessary models in my app but without getting the Transcribe job name, it won't work.
Any advice appreciated.
回答1:
Per your updated question, I suspect your issue is actually here:
var post_data = querystring.stringify(
event
);
Querystring does not support nested objects, such as the detail
block of the cloudwatch event. More info:
- https://github.com/sindresorhus/query-string#user-content-nesting
So although you didn't indicate it in your question, I suspect you are showing the response that you are receiving as the result of this lambda post, not the raw response/event you are receiving from AWS Transcribe.
Perhaps instead of querystring:
var post_data = JSON.stringify(event);
来源:https://stackoverflow.com/questions/58938814/cloudwatch-events-trigger-on-amazon-transcribe-event