DynamoDB updateItem in Lambda fails silently - javascript

I'm attempting to implement a simple counter with a Lambda function, but whenever I test it, the updateItem below simply does not work: none of the log statements in the callback are run at all, and of course the relevant counter in the table is never updated. Here's my lambda function:
'use strict';
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({ apiVersion: '2012-08-10' });
let params = {
TableName: 'Counters',
Key: {
'name': { S: 'global' }
},
UpdateExpression: 'SET val = val + :inc',
ExpressionAttributeValues: {
':inc': { N: '1' }
},
ReturnValues: 'ALL_NEW'
};
exports.handler = async(event) => {
console.log("Invoked counter-test");
dynamodb.updateItem(params, function(err, data) {
console.log("In updateItem callback");
if (err)
console.log(err, err.stack);
else
console.log(data);
});
console.log("Updated counter");
const response = {
statusCode: 200,
body: JSON.stringify('Counter updated'),
};
return response;
};
And here's the output of the test:
Response:
{
"statusCode": 200,
"body": "\"Counter updated\""
}
Request ID:
"80e92299-2eea-45e4-9c68-54ccf87199c5"
Function Logs:
START RequestId: 80e92299-2eea-45e4-9c68-54ccf87199c5 Version: $LATEST
2019-05-07T11:34:21.931Z 80e92299-2eea-45e4-9c68-54ccf87199c5 Invoked counter-test
2019-05-07T11:34:21.934Z 80e92299-2eea-45e4-9c68-54ccf87199c5 Updated counter
END RequestId: 80e92299-2eea-45e4-9c68-54ccf87199c5
REPORT RequestId: 80e92299-2eea-45e4-9c68-54ccf87199c5 Duration: 275.91 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 67 MB
As you can see, no log statements from the updateItems callback ran.
If I try to update the counter from the command line using aws dynamodb it does work, however:
$ aws dynamodb update-item \
--table-name Counters \
--key '{"name": { "S": "global" }}' \
--update-expression 'SET val = val + :inc' \
--expression-attribute-values '{":inc": {"N": "1"}}' \
--return-values ALL_NEW \
--output json
{
"Attributes": {
"name": {
"S": "global"
},
"val": {
"N": "129"
}
}
}

This is due to the asynchronous nature of Javascript.
The method updateItem is asynchronous and you don't wait for the callback to be fired before returning (you start the updateItem operation and then immediately return a response).
If you want to maintain the callback pattern, you should do:
exports.handler = (event, context, callback) => {
console.log("Invoked counter-test");
dynamodb.updateItem(params, function(err, data) {
console.log("In updateItem callback");
if (err) {
console.log(err, err.stack);
callback(err);
} else {
console.log(data);
console.log("Updated counter");
const response = {
statusCode: 200,
body: JSON.stringify('Counter updated'),
};
callback(null, response);
}
});
};
Using promises:
exports.handler = (event, context, callback) => {
console.log("Invoked counter-test");
dynamodb.updateItem(params).promise()
.then((data) => {
console.log(data);
console.log("Updated counter");
const response = {
statusCode: 200,
body: JSON.stringify('Counter updated'),
};
callback(null, response);
});
.catch((err) => {
console.log(err, err.stack);
callback(err);
})
};
Using await (recommended):
exports.handler = async (event) => {
try {
console.log("Invoked counter-test");
const data = await dynamodb.updateItem(params).promise();
console.log(data);
console.log("Updated counter");
const response = {
statusCode: 200,
body: JSON.stringify('Counter updated'),
};
return response;
} catch (err) {
console.log(err, err.stack);
throw err;
}
};
See also Understanding Asynchronous JavaScript, Deeply Understanding JavaScript Async and Await with Examples and AWS SDK for Javascript - Using JavaScript Promises.

Related

Why does my Lambda function send an SQS message twice with one call?

I need to only deliver a message to a standard (not FIFO, but is not related to the question) SQS queue, once with one call.
However, the code below is sending 2 messages with 1 call.
const AWS = require('aws-sdk')
AWS.config.update({region: process.env.AWS_REGION})
const sqs = new AWS.SQS({apiVersion: '2012-11-05'});
async function sendToSQSEvent(body,attributes=null){
var m_body
if (attributes != null)
{
m_body = {
body : body,
attributes : attributes
};
}
else{
m_body = body;
}
m_body = JSON.stringify(m_body);
var params = {
// DelaySeconds: 0, <-- i try but only delay reception
MessageAttributes: {
"Title": {
DataType: "String",
StringValue: "TIME_OUT"
},
"Author": {
DataType: "String",
StringValue: "LAMBDA_IN"
},
},
MessageBody: m_body,
QueueUrl: "https://my_url/sqs"
};
console.log('_________CALL_______________');
var r = await sqs.sendMessage(params, function(err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data.MessageId ,data);
}
}).promise(console.log("_________in promise___________"));
console.log("___end")
}
exports.handler = async (event, context) => {
await sendToSQSEvent(event)
};
Console output is:
START RequestId: RequestId Version: $LATEST
2021-10-11T06:23:52.992Z RequestId INFO _________CALL_______________
2021-10-11T06:23:53.425Z RequestId INFO _________in promise___________
2021-10-11T06:23:53.728Z RequestId INFO Success ********-****-****-****-*********b4f {
ResponseMetadata: { RequestId: '********-****-****-****-*********89d' },
MD5OfMessageBody: '********************************8f',
MD5OfMessageAttributes: '***********************1b0',
MessageId: '********-****-****-****-*********b4f'
}
2021-10-11T06:23:53.786Z RequestId INFO ___end
2021-10-11T06:23:53.807Z RequestId INFO Success ********-****-****-****-*********665 {
ResponseMetadata: { RequestId: '********-****-****-****-********835' },
MD5OfMessageBody: '***********************28f',
MD5OfMessageAttributes: '***********************1b0',
MessageId: '********-****-****-****-*********665'
}
END RequestId: RequestId
What is the issue?
It's sending a message twice as you're mixing synchronous callbacks (function(err, data)) with asynchronous promises (await, async function sendToSQSEvent(...)).
You can see this as CloudWatch is logging 2 sqs.sendMessage(...) responses.
I would recommend sticking with the latter.
This should be your SQS sendMessage logic, which returns a promise object for your handler.
return sqs.sendMessage(params).promise();
You can then check the response in your handler:
exports.handler = async (event, context) => {
try {
var data = await sendToSQSEvent(event)
console.log("Success", data.MessageId ,data);
}
catch (err){
console.log("Error", err);
}
};
This should be the final working result:
const AWS = require('aws-sdk')
AWS.config.update({
region: process.env.AWS_REGION
})
const sqs = new AWS.SQS({
apiVersion: '2012-11-05'
});
async function sendToSQSEvent(body, attributes = null) {
var m_body
if (attributes != null) {
m_body = {
body: body,
attributes: attributes
};
} else {
m_body = body;
}
m_body = JSON.stringify(m_body);
var params = {
MessageAttributes: {
"Title": {
DataType: "String",
StringValue: "TIME_OUT"
},
"Author": {
DataType: "String",
StringValue: "LAMBDA_IN"
},
},
MessageBody: m_body,
QueueUrl: "https://my_url/sqs"
};
return sqs.sendMessage(params).promise();
}
exports.handler = async (event, context) => {
try {
var data = await sendToSQSEvent(event)
console.log("Success", data.MessageId ,data);
}
catch (err){
console.log("Error", err);
}
};

Invoking an api inside a aws lambda but getting a null response

I am trying to invoke a rest API inside an API but it is not returning anything. So I am making a simple lambda which returns a JSON but getting a null value as a response.
var https = require('https');
var dt;
exports.handler = async (event, context) => {
var data = '';
return new Promise((resolve, reject) => {
var params = {
host: "cvwtzygw4a.execute-api.ap-south-1.amazonaws.com",
path: "/test/first"
};
const req = https.request(params, (res) => {
console.log('STATUS: ' + res.statusCode);
res.setEncoding('utf8');
res.o n('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
console.log("DONE");
console.log(data);
dt = JSON.parse(data);
console.log(dt);
});
resolve(dt);
});
req.on('error', (e) => {
reject(e.message);
});
// send the request
req.write('');
req.end();
});
};
You should go through this article to understand how to use NodeJs promises in AWS Lambda. In this, the second solution addresses your use case.
To be specific to your code, I modified to make it very simple using the async/await syntax and the request-promise library.
const request = require('request-promise');
exports.handler = async (event, context) => {
var data = '';
try {
data = await request.get('https://cvwtzygw4a.execute-api.ap-south-1.amazonaws.com/test/first');
console.log('response received', res);
} catch (error) {
console.log('Error', error);
}
return data;
};
Following was the output:
START RequestId: 80d75f93-5fa6-1354-c22c-0597beb075e7 Version: $LATEST
2020-01-03T17:51:19.987Z 80d75f93-5fa6-1354-c22c-0597beb075e7 response received {
"basic" : {"name":"John","age":31,"city":"New York"}
}
END RequestId: 80d75f93-5fa6-1354-c22c-0597beb075e7
REPORT RequestId: 80d75f93-5fa6-1354-c22c-0597beb075e7 Init Duration: 907.81 ms Duration: 1258.54 ms Billed Duration: 1300 ms Memory Size: 128 MB Max Memory Used: 55 MB
"{\n\"basic\" : {\"name\":\"John\",\"age\":31,\"city\":\"New York\"}\n}"

AWS Lambda function not writing to DynamoDB

I have a lambda function that's suppose to be writing to a database. When I run it on my local machine it works but then when I upload it to lambda and test it It doesn't put anything in the database. The role I have the function using has full access to DynamoDB and its the exact same code that works fine when I run it from my laptop. Any idea why that would be the case?
Here's my lambda. The dao class contains the code that actually accesses dynamo. I'm just trying to upload some constant strings right now.
const DAO = require('./PostStatusDAO.js');
exports.handler = async (event, context, callback) => {
var dao = new DAO();
dao.post("this is a test", "#jordan", "#matt", "none");
const response = {
statusCode: 200,
body: {
result: "good"
}
};
return response;
};
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
class PostStatusDAO {
post(in_text, in_user, in_author, in_attachment) {
var params = {
Item: {
user: String(in_user),
timestamp: Date.now(),
author: String(in_author),
text: String(in_text),
attachment: String(in_attachment),
},
TableName: 'Feed',
};
console.log(params);
var result = ddb.put(params, (err, data) => {
console.log("callback");
if(err) {
console.log("Error: ", err);
} else {
console.log("Data: ", data);
}
});
// console.log(result);
}
}
module.exports = PostStatusDAO;
To see the reason why your function is failing you have to either run it synchronously or return the promise back to the caller/runtime like this:
const DAO = require('./PostStatusDAO.js');
exports.handler = async(event, context, callback) => {
var dao = new DAO();
// Return new promise
return new Promise(function(resolve, reject) {
// Do async job
dao.post("this is a test", "#jordan", "#matt", "none", function(err, data) {
if (err) {
console.log("Error: ", err);
reject(err);
}
else {
console.log("Data: ", data);
resolve(data);
}
})
})
};
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-west-2'});
class PostStatusDAO {
async post(in_text, in_user, in_author, in_attachment, callback) {
var params = {
Item: {
user: String(in_user),
timestamp: Date.now(),
author: String(in_author),
text: String(in_text),
attachment: String(in_attachment),
},
TableName: 'Feed',
};
console.log(params);
return ddb.put(params, callback).promise();
}
}
module.exports = PostStatusDAO;

How to return json from callback function within the Lambda?

I'm trying to return the login status from the Cognito callback function, which is written in the NodeJS Lambda. However when I call the API the response keep loading and I'm getting warning error.
Here is my code:
'use strict';
global.fetch = require('node-fetch');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
module.exports.hello = async (event, context) => {
return {
statusCode: 200,
body: JSON.stringify({
message: "Hello there"
}),
};
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
module.exports.register = async (event, context, callback) => {
let poolData = {
UserPoolId : 'xxxxx', // Your user pool id here
ClientId : 'xxxxxxx' // Your client id here
} // the user Pool Data
let userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
let attributeList = [];
let dataEmail = {
Name : 'email',
Value : 'test#gmail.com'
};
let dataName = {
Name : 'name',
Value : 'Jack'
};
var dataPhoneNumber = {
Name : 'phone_number',
Value : '+94234324324234' // your phone number here with +country code and no delimiters in front
};
let attributeEmail = new AmazonCognitoIdentity.CognitoUserAttribute(dataEmail);
let attributeName = new AmazonCognitoIdentity.CognitoUserAttribute(dataName);
var attributePhoneNumber = new AmazonCognitoIdentity.CognitoUserAttribute(dataPhoneNumber);
attributeList.push(attributeEmail);
attributeList.push(attributeName);
attributeList.push(attributePhoneNumber);
userPool.signUp('test#gmail.com', 'H1%23$4jsk', attributeList, null, function(err, result){
let data = {};
if (err) {
callback(null, {
statusCode: 500,
body: JSON.stringify({
status: 'FAIL',
message: err.message
}),
});
} else {
let cognitoUser = result.user;
callback(null, {
statusCode: 200,
body: JSON.stringify({
status: 'SUCCESS',
message: '',
data: {
username: cognitoUser.getUsername(),
id: result.userSub
}
}),
});
}
})
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
The warning error as follows:
Serverless: Warning: handler 'register' returned a promise and also uses a callback!
This is problematic and might cause issues in your lambda.
Serverless: Warning: context.done called twice within handler 'register'!
serverless.yml
service: test-auth
plugins:
- serverless-offline
provider:
name: aws
runtime: nodejs8.10
stage: dev
region: us-east-1
functions:
hello:
handler: handler.hello
events:
- http:
path: message
method: get
register:
handler: handler.register
events:
- http:
path: register
method: post
Any help would be appreciated, Thanks in advance.
EDIT (2019-04-01):
module.exports.register = (event, context) => {
...
userPool.signUp('test#gmail.com', 'H1%23$4jsk', attributeList, null, function(err, result){
// for testing purpose directly returning
return {
statusCode: 500,
body: JSON.stringify({
status: 'FAIL',
message: err.message
})
}
})
};
Its exactly what the error message states.
All async functions return promises.
module.exports.register = async (event, context, callback) => {}
You are also using the callback by calling
callback(null, {
statusCode: 500,
body: JSON.stringify({
status: 'FAIL',
message: err.message
}),
});
Instead of using the callback, just return the either an error or a valid response.
Well the error is accurate. async wraps your return with promise. Either use callback all the way through like:
global.fetch = require('node-fetch');
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
// remove async
module.exports.register = (event, context, callback) => {
...
// if you're using callback, don't use return (setup your callback to be able to handle this value as required) instead do:
// calback({ message: 'Go Serverless v1.0! Your function executed successfully!', event })
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
Or don't use callback, use async/await (Promise) all the way through like:
module.exports.register = async (event, context) => {
...
// needs promise wrapper, when using with promise, you might want to break up your code to be more modular
const mySignUp = (email, password, attributes, someparam) => {
return new Promise((resolve, reject) => {
userPool.signUp(email, password, attributes, someparam, function(err, result) {
let data = {};
if (err) {
reject({
statusCode: 500,
body: JSON.stringify({
status: 'FAIL',
message: err.message
}),
});
} else {
let cognitoUser = result.user;
resolve({
statusCode: 200,
body: JSON.stringify({
status: 'SUCCESS',
message: '',
data: {
username: cognitoUser.getUsername(),
id: result.userSub
}
}),
});
}
})
});
}
// call the wrapper and return
return await mySignUp('test#gmail.com', 'H1%23$4jsk', attributeList, null);
// don't use double return
// Use this code if you don't use the http event with the LAMBDA-PROXY integration
// return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};
Now register will return a promise. Elsewhere in your code you can call register like:
var result = register();
result
.then(data => console.log(data))
// catches the reject from Promise
.catch(err => console.error(err))
or in async/await function (Note: `await` is valid only inside `async` function)
async function someFunc() {
try {
var result = await register();
// do something with result
console.log(result);
} catch (err) {
// reject from Promise
console.error(err)
}
}
Also note use strict is not required here as node modules use strict by default.
You are using an async function with a call back.
Try it this way:
Remove the callback from the async function.
async (event, context)
And modify the return as:
if (err) {
return {
statusCode: 500,
body: JSON.stringify({
status: 'FAIL',
message: err.message
})
}
}
And put an await on the function call.
If it helps anyone else catching this, you can add headers to the return:
return {
statusCode: 200,
headers: {"Content-Type": "application/json"},
body: JSON.stringify(response.data)
};

Serverless querying NodeJS MSSQL and cannot get result back to callback

I'm writing a small Serverless function to query a MSSQL db using the node mssql library (https://www.npmjs.com/package/mssql#callbacks)
I've read the documentation and I think I'm doing everything right but getting confused - I can see the result in my logs, but the main function callback is not being called and therefore data not being outputted by the API (basically the whole thing times out)
Heres my Lambda function:
import {success, error} from './libs/response-lib';
import {EPDBConfig} from "./libs/Database-lib";
import sql from "mssql";
import config from "./config";
export function main(event, context, callback) {
console.log("start");
EPDBConfig().then(dbConfig => {
if(config.debug) console.log("Hello!");
let EPDBconfig = {
user: dbConfig.dbuser,
password: dbConfig.dbpassword,
server: dbConfig.dbhost,
database: dbConfig.dbname
};
sql.connect(EPDBconfig)
.then(pool => {
return pool.request()
.input('student_no', sql.Int, 129546)
.query('select * from Student where StudentNo = #student_no')
}).then(result => {
console.log("success!");
if(config.debug) console.log('result', result);
return result;
}).catch(err => {
if(config.debug) console.log('err1', err);
return err;
});
sql.on('error', err => {
if(config.debug) console.log('err2', err);
return callback(null, error(err));
});
sql.on('done', result => {
if(config.debug) console.log('done', result);
return callback(null, success(result));
});
}).catch(err => {
if(config.debug) console.log('err3', err);
return callback(null, error(err));
})
}
DB Config is pulled from AWS KMS for secure vars
import AWS from "aws-sdk";
import config from "../config";
const kms = new AWS.KMS({
region: AWS.config.region
});
export function EPDBConfig() {
//DECRYPT THE DATABASE CONNECTION DETAILS
return new Promise((resolve, reject) => {
let params = {
CiphertextBlob: Buffer(process.env.epdb, 'base64')
};
kms.decrypt(params, function(err, data) {
if (err) {
reject(err);
} // an error occurred
else {
let dbParams = JSON.parse(String(data.Plaintext));
resolve(dbParams);
}
});
});
}
and response lib:
export function success(data, message) {
return buildResponse(200, true, data, message);
}
export function error(data, message) {
return buildResponse(400, false, data, message);
}
export function unauthorized(data, message) {
return buildResponse(401, false, data, message);
}
export function forbidden(data, message) {
return buildResponse(403, false, data, message);
}
export function exception(data, message) {
return buildResponse(500, false, data, message);
}
function buildResponse(statusCode, successState, data, message) {
var body = {
success: successState,
message: message
};
if (successState) {
body.data = data;
}
return {
statusCode: statusCode,
headers: {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Credentials': true
},
body: JSON.stringify(body)
};
}
Can anyone point out where I'm going wrong here? I think I have a whole pile of promises going on. The sql.on('done', result => { ... doesn't appear to work, and I tried adding 'return callback(null, success(result));' in the area where I have 'success'
Please help me!
So, I endded up resolving this with a bit of refactoring:
import sql from "mssql";
import _ from "lodash";
import {success, error} from './libs/response-lib';
import {DB} from "./libs/Database-lib";
import {Student} from "./models/ep.student";
export function main(event, context, callback) {
DB().then(pool => {
return new Promise((resolve, reject) => {
pool.connect(err => {
if(err) reject(err);
pool.request()
.input('StudentNo', sql.Int, 129546)
.execute('StoredProcedure')
.then(result => {
if(process.env.debug) console.log('result', result);
let student = new Student();
_.assign(student, result.recordset[0]);
resolve(student);
pool.close();
}).catch(err => {
reject(err);
pool.close();
});
});
});
}).then(result => {
if(process.env.debug) console.log('result', result);
callback(null, success(result));
}).catch(err => {
if(process.env.debug) console.log('err', err);
callback(null, error(err));
});
}
The first issue was that I wasn't terminating my connection - just a note that I switched to using a stored procedure as this was always going to be the case.
The second issue was that I wasn't really using promises correctly (i think) I wrapped up my connection in a promise and only when I had the response did I resolve or reject it.

Categories

Resources