Database retrieval error "Process exited before completing request" - amazon-web-services

I've got problem with getting data from database via AWS lambda. I've got code in Node.js:
'use strict';
console.log('Loading function');
let doc = require('dynamodb-doc');
let dynamo = new doc.DynamoDB();
exports.handler = (event, context, callback) => {
//console.log('Received event:', JSON.stringify(event, null, 2));
if (event.tableName) {
event.id.TableName = event.tableName;
}
dynamo.getItem(event.id, callback);
callback(null, event.id); // Echo back the first key value
// callback('Something went wrong');
};
And testing it with such a JSON code:
{
"tableName": "Events",
"payload": "dbf943c3-aaac-473d-9c6a-7651280e0f90"
}
I get an error:
{
"errorMessage": "Process exited before completing request"
}
Does anyone know what is wrong?
PS. My Database is called Events and id of the record which I want to get is: dbf943c3-aaac-473d-9c6a-7651280e0f90

Related

AWS Lambda error { "errorType": "string", "errorMessage": "error", "trace": [] }, when send request on api gateway

I'm trying to build my first lambda function that triggered by an API gateway request, then sends to Queue service where I included down below: /when I send a request through api gateway, it gives status code: 200 but queue doesn't get any messages/
var AWS = require('aws-sdk');
var sqs = new AWS.SQS();
exports.handler = function(event, context, callback) {
var params = {
MessageBody: JSON.stringify(event),
QueueUrl: 'https://sqs.ap-southeast-1.amazonaws.com/********/****.fifo'
};
sqs.sendMessage(params, function(err, data) {
if (err) {
console.log('error:', "Fail Send Message", +err);
context.done('error', "ERROR Put SQS");
} else {
console.log('data:', data.MessageId);
context.done(null, '');
}
});
}

Null Response while querying DynamoDB

I am querying dynamodb from lambda function written in node.js env -
Trying to query for table CurrencyPrice where Currency column has value "BLC".
When I test my function in lambda console, - it prints until second console log - "querying DB ConsolePrice" and returns a NULL response. It does not print either of the next two console logs and not sure if it is even connecting to DB.
It seems that the code does not go into ddb.query() function at all - had tried putting all loggers in this function but none gets printed.
I have tried checking all possible aws documentation but not able to understand why this function is not getting executed.
My code looks something like below -
var AWS = require ('aws-sdk');
exports.handler = async (event) =>
{
AWS.config.update({region: 'ap-southeast-2'});
console.log("i am in function");
// Create DynamoDB service object
var ddb = new AWS.DynamoDB.DocumentClient();
var table = 'CurrencyPrice';
var params = {
"Select": "ALL_ATTRIBUTES","TableName": "CurrencyPrice",
};
console.log("querying DB" + table);
ddb.query(params, function(err, data) {
console.log("i am in ddb query");
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null,2));
} else {
console.log(data);
}
});
};
Current result that I am getting in lambda console:
Response:
null
Request ID:"XXXX"
Function Logs:
START RequestId: XXX Version: $LATEST
2019-06-02T13:31:55.189Z XXXX INFO i am in function
2019-06-02T13:31:55.331Z XXXX INFO querying DBCurrencyPrice
2019-06-02T13:31:55.390Z XXXX INFO { Select: 'ALL_ATTRIBUTES', TableName: 'CurrencyPrice' }
END RequestId: XXXX
I expect that at least it prints "Unable to Query" or actual data that it connects to DB and query?
If you're using async/await you would want to return a promise.
var AWS = require("aws-sdk");
AWS.config.update({ region: "ap-southeast-2" });
var ddb = new AWS.DynamoDB.DocumentClient();
exports.handler = async event => {
console.log("i am in function");
// Create DynamoDB service object
var table = "CurrencyPrice";
var params = {
Select: "ALL_ATTRIBUTES",
TableName: "CurrencyPrice"
};
console.log("querying DB" + table);
return ddb
.query(params)
.promise()
.then((err, data) => {
console.log("i am in ddb query");
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null, 2));
} else {
console.log(data);
}
});
};
The AWS example
ddbClient.query(params, callback)
shows a synchronous matter. Execution continues without waiting for the callback finishes. That's why you got Null.
You will have to wrap this .query() call into a Promise, so the execution will wait for completion of this callback.
Make query a promise and wait for its fulfilled state.
const promise = await ddb.query(params, function(err, data) {
console.log("i am in ddb query");
if (err) {
console.error("Unable to query. Error:", JSON.stringify(err, null,2));
} else {
console.log(data);
// process your data
}
}).promise();
return promise;

How to fetch last transaction using Lex Bot after confirmation?

I made a Lex Bot to order milk but whenever i completed the process and exit the bot my transaction got erased. I am not able to see my last transaction. I want to know how can i save my previous details so i can enquire it in future?
var AWS = require('aws-sdk');
var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) =>{
var amount = event.currentIntent.slots.amount;
var params1 = {
Key: {
"last_Transaction": {
S: " "
},
},
TableName: "confirmation_Table"
};
dynamodb.getItem(params1, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else
{console.log(data); // successful response
}
});
callback(null, {
"dialogAction":{
"type":"Close",
"fulfillmentState": "Fulfilled",
"message": {
"contentType": "PlainText",
"content": amount
}
}
});
};
You should save the transactions in some file or database, where you can read them in future. DynamoDB is good choice.
Before exiting the bot you can store the transaction with unique id in the DynamoDB.
You can use put_item(),read about them at DynamoDB docs
Below is the pseudo code:
Get info from user
Validate in DialogCodeHook
Come to FulfillmentCodeHook
Save info in DynamoDB
Close

Publishing from AWS lambda to Kinesis Process exited

I am trying to publish events from aws lambda but I get the following error:
Process exited before completing request
Here's my code
exports.handler = (event, context, callback) => {
kinesis.PutRecord({
"Data": event,
"PartitionKey" : "1",
"StreamName": "TestStream"
});
context.done();
callback(null, "");
}
You have context.done called before the callback function where both are exit callbacks for the handler code. Remove context.done in your code, and also do the following change.
const AWS = require('aws-sdk');
const kinesis = new AWS.Kinesis({apiVersion: '2013-12-02'});
exports.handler = (event, context, callback) => {
kinesis.putRecord({
"Data": event,
"PartitionKey" : "1",
"StreamName": "TestStream"
},
function(err, data) {
if (err)
console.log(err, err.stack); // an error occurred
else
callback(null, data); // successful response
});
}

Add information into DynamoDB using Alexa Lambda function

I've been stuck on this code for about a day now.
I am just trying to add information into DynamoDB through a launch request using Alexa.
I get the following error code:
"errorMessage": "RequestId: f96ae2cb-1dbf-11e7-a267-b7cf2f2c95a0 Process exited before completing request"
The information actually gets inserted into DynamoDB, but I can't add more functions to the program because of the error.
From what I understand, it may be a problem with the callback.
I have tried many different ways to "callback" or return something, but I haven't figured out how to avoid the error.
If I uncomment this.emit(':tell', "Hello, inserting Apples into DynamoDB"); the error goes away, but no information gets inserted.
What am I doing wrong and how can I fix it?
Below is my code;
'use strict';
var Alexa = require('alexa-sdk');
const doc = require('dynamodb-doc');
const dynamo = new doc.DynamoDB();
exports.handler = function(event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(handlers);
alexa.execute();
};
var handlers = {
'LaunchRequest': function(event, context, callback) {
// this.emit(':tell', "Hello, inserting Apples into DynamoDB");
var params = {
Item: {
date: Date.now(),
message: "Apples"
},
TableName: '_yourTableName'
};
dynamo.putItem(params, function(err, data) {
if (err) {
callback(err, null);
} else {
callback(null, data);
}
});
context.done();
}
};
This is because you are adding values to Dynamodb, which is a callback but context.done(); is written outside the callback. Before dynamoDb completes the operation it will call context.done(); hence it will exit the process

Categories