I am very new to AWS and trying to write a simple serverless application to detect the text of an uploaded image in S3 bucket. The image "2272922.jpg" is already uploaded into the S3 bucket. The below snippet works in local through node and returning perfect response
but always returns null when executed from Lambda (or invoked from the api gateway).
'use strict'
const AWS = require('aws-sdk')
AWS.config.update({ region: process.env.AWS_REGION })
var rekognition = new AWS.Rekognition();
console.log('Calling lmabda function detect with arguments');
// Lambda function entry point
exports.handler = async (event) => {
return await processImage(event)
}
//function which will extract the text from S3 bucket image
const processImage = async function (event) {
try {
var params = {
Image: {
S3Object: {
Bucket: "s3uploadbucket",
Name: "2272922.jpg"
}
},
};
rekognition.detectText(params, function (err, data) {
console.log(data);
if (err) console.log(err, err.stack); // an error occurred
else {
var table = "<table><tr><th>Low</th><th>High</th></tr>";
// show each text data detected
for (var i = 0; i < data.TextDetections.length; i++) {
table += '<tr><td>' + "Detected: " + data.TextDetections[i].DetectedText + '</td></tr>' +
'<tr><td>' + "Confidence: " + data.TextDetections[i].Confidence + '</td></tr>' +
'<tr><td>' + "Id : " + data.TextDetections[i].Id + '</td></tr>'
'<tr><td>' + "Parent Id: " + data.TextDetections[i].ParentId + '</td></tr>'
'<tr><td>' + "Type: " + data.TextDetections[i].Type + '</td></tr>'
}
table += "</table>";
console.log(table);
const response = {
statusCode: 200,
body: JSON.stringify(table)
}
return response;
}
});
} catch (error) {
console.log(error);
return error;
}
}
I had given the below roles to execute this lamda function. Since thinking the issue is because of the lambda misses somewhere the Rekognition related policies (as the function works in local), edited the role through console and tried with all the Rekognition related polices, but didn't worked.
Policies:
- Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- s3:GetObject
Resource: arn:aws:s3:::*
- Effect: Allow
Action:
- rekognition:DetectText
- rekognition:DetectLabels
Resource: "*"
