AWS Lambda update function showing ValidationException - node.js

I am trying to update data by lamda function. I just created a PUT Method and add lambda function in method after reading documentation i created a function here is the code
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
AWS.config.update({region: 'us-east-2', apiVersion: '2012-08-10'});
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
const params = {
TableName: "Would-You-Rather",
Key:{
"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",
},
UpdateExpression: "set would = :w, rather = :r, wouldClick = :wC, , ratherClick = :rC ",
ExpressionAttributeValues:{
":w": "Working",
":r": "Working",
":wC": 12,
":rC": 1
},
ReturnValues:"UPDATED_NEW"
};
console.log("Updating the item...");
docClient.update(params, function(err, data) {
if (err) {
console.error("Unable to update item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("UpdateItem succeeded:", JSON.stringify(data, null, 2));
}
});
};
But when I am testing the function it's showing
"message": "Invalid UpdateExpression: Syntax error; token: \",\", near: \", , ratherClick\"",
"code": "ValidationException",
If i wrap ExpressionAttributes like this
ExpressionAttributeValues:{
":w": "Working",
":r": "Working",
":wC": "12",
":rC": "1"
},
Then its showing this error
"errorType": "Runtime.UserCodeSyntaxError",
"errorMessage": "SyntaxError: Invalid or unexpected token",
This is my POST method which is working fine add this because maybe it will help in better understanding
const AWS = require('aws-sdk');
const dynamodb = new AWS.DynamoDB({region: 'us-east-2', apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => {
const params = {
Item: {
"QuestionID": {
S: context.awsRequestId
},
"Would": {
S: event.would
},
"Rather": {
S: event.rather
},
"wouldClick": {
N: event.wouldClick
},
"ratherClick": {
N: event.ratherClick
}
},
TableName: "Would-You-Rather"
};
dynamodb.putItem(params, function(err, data) {
if (err) {
console.log(err);
callback(err);
} else {
console.log(data);
callback(null, data);
}
});
};
Codepen code
var xhr = new XMLHttpRequest();
xhr.open('PUT', 'https://iv9803zj9d.execute-api.us-east-2.amazonaws.com/Development/would-you-rather');
xhr.onreadystatechange = function(event) {
console.log(event.target.response);
}
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', 'allow');
xhr.send(JSON.stringify({Would: Coffe, Rather: Tea, wouldClick: 15, ratherClick: 13, QuestionID: 3e8976be-e763-4c69-84c3-be03ec4a38de}));

There's a syntax error. You forgot to put the closing quote here
"QuestionID": "a670b861-8a34-4bda-93e0-8bbf2cd25eb6",

Related

Aws Lambda to read the user input dynamically and return the data from Dynamo table

Lambda provides the expected result only when I pass the value manually (Id = '011010').
In the step function the value "ID" value will be random based on the logic from previous step, since the Id value is not static how to handle this scenario "ExpressionAttributeValues"
I tried all the below syntax but no luck..
ExpressionAttributeValues: { ':value': $.External.Id}
ExpressionAttributeValues: { ':value': External.Id.$}
ExpressionAttributeValues: { ':value': $.Id}
ExpressionAttributeValues: { ':value': Id.$}
ExpressionAttributeValues: { ':value': event.Id}
Lambda-Code
'use strict'
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'temptable',
IndexName: 'Id-CurrentStatus-index',
KeyConditionExpression: '#Id= :value',
ExpressionAttributeNames: { '#Id': 'Id'},
ExpressionAttributeValues: { ':value': 'M1' }
};
async function queryItems(){
try {
const data = await docClient.query(params).promise()
return data
} catch (err) {
return err
}
}
exports.handler = async (event, context) => {
try {
const data = await queryItems()
return { body: JSON.stringify(data) }
} catch (err) {
return { error: err }
}
}
I can read it from the
console.log("Memberid :" + JSON.stringify(event.Id, null, 2))
but how to pass the same value in the
ExpressionAttributeValues: { ':value': 'M1' }
I tried the below syntax. nothing works
ExpressionAttributeValues: { ':value': JSON.stringify(event.Id, null, 2) }
ExpressionAttributeValues: { ':value': event.Id}
ExpressionAttributeValues: { ':value': event.Id}
ExpressionAttributeValues: { ':value': Id}
I got the required code after checking multiple thread.
AWS Lambda function to scan/query DynamoDB table using array values as FilterExpression
'use strict'
var AWS = require('aws-sdk');
var mydocumentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = function (event, context, callback) {
var params = {
TableName: 'temptable',
KeyConditionExpression : 'Id= :Id',
FilterExpression : 'Id in (:Id)',
ExpressionAttributeValues: {
":Id": event.Id
}
};
mydocumentClient.scan(params, function (err, data){
if (err) {
callback(err, null);
}else{
callback(null, data);
}
})
}

DynamoDB Scan with Lambda not returning elements

I have a Lambda with the following code:
// Load the AWS SDK for Node.js.
var AWS = require("aws-sdk");
// Set the AWS Region.
AWS.config.update({ region: "us-east-2" });
exports.handler = function(event, context, callback) {
// Create DynamoDB service object.
var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" });
var params = {
TableName: "Ranking",
ProjectionExpression: "#username, Score, Duration",
FilterExpression: "#username = :username",
ExpressionAttributeNames: {
"#username": "Username",
},
ExpressionAttributeValues: {
":username": {
S: 'Alberto'
},
}
};
let toReturn = [];
ddb.scan(params, function (err, data) {
if (err) {
toReturn = err
} else {
toReturn = data.Items;
}
});
let response = {
statusCode: 200,
body: JSON.stringify(toReturn)
};
callback(null, response)
};
However I always see [] as response...
My current DB has the following records:
So my question is... why I don't get back that item?
Since you are using callbacks, your code should be as below. Also Duration is reserved keyword, so it also needs to be modified as below:
// Load the AWS SDK for Node.js.
var AWS = require("aws-sdk");
// Set the AWS Region.
AWS.config.update({ region: "us-east-2" });
exports.handler = function(event, context, callback) {
// Create DynamoDB service object.
var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" });
var params = {
TableName: "Ranking",
FilterExpression: "#username = :username",
ProjectionExpression: "#username, Score, #duration",
ExpressionAttributeNames: {
"#username": "Username",
"#duration": "Duration",
},
ExpressionAttributeValues: {
":username": {
S: 'Alberto'
},
}
};
ddb.scan(params, function (err, data) {
if (err) {
callback(null, err)
} else {
callback(null, data.Items)
}
});
};

Data Item not getting inserted in DynamoDB Table from AWS Lambda Trigger

I have this Lambda code setup in JS for inserting a row in DynamoDB but the code seems to have some issue and doesn't trigger.Unable to figure out the issue as cloud watch also does captures anything. The API Gateway triggers the Lambda will inserts a row in DynamoDB. Tried debugging with console and logs also but unable to figure out the actual issue that is causing the code to break.
exports.handler = async (event) => {
console.log(event);
var eventBody = JSON.parse(event.body);
var eventType = eventBody.event_type;
var content = eventBody.content;
if (content.subscription) {
var subscription = content.subscription;
switch (eventType) {
case "subscription_created":
// Add new entry to License table
console.log('subscription created event');
var customer = content.customer;
var findUserScan = {
ExpressionAttributeValues: {
':email': { S: customer.email },
},
FilterExpression: 'email = :email',
TableName: _dynamodb_userprofile_table
};
try {
await dynamodb.scan(findUserScan, function (err, data) {
if (err) {
console.log("Error", err);
} else {
var userProfileAWS;
data.Items.forEach(function (userProfile, index, array) {
userProfileAWS = AWS.DynamoDB.Converter.unmarshall(userProfile);
});
var companyName;
if (userProfileAWS) {
companyName = userProfileAWS.organization;
}
try {
var licenseEntry = {
"subscriptionID": {
S: subscription.id
},
"companyName": {
S: companyName || ""
},
"customerEmail": {
S: customer.email
},
};
await dynamodb.putItem({
"TableName": _dynamodb_license_table,
"Item": licenseEntry
}).promise()
} catch (error) {
console.log(error)
throw new Error(`Error in dynamoDB: ${JSON.stringify(error)}`);
}
}
}).promise();
} catch (error) {
throw new Error(`Error in dynamoDB: ${JSON.stringify(error)}`);
}
break;

unable to get all items using query DynamoDB

How to scan all items from AWS dynamodb using node.js. I am posting my code here.
//Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
var unmarshalItem = require('dynamodb-marshaler').unmarshalItem;
// Set the region
AWS.config.update({region: 'us-east-1'});
// Create DynamoDB service object
var b = new AWS.DynamoDB({apiVersion: '2012-08-10'});
exports.handler = (event, context, callback) => {
var params = {
TableName: 'IoTdata2',
FilterExpression: "#deviceid = :unitID and #devicetimestamp BETWEEN :ftimestamp and :ttimestamp",
ExpressionAttributeNames: {
"#deviceid": "id",
"#devicetimestamp": "timestamp"
},
ExpressionAttributeValues: {
':unitID': {S: 'arena-MXHGMYzBBP5F6jztnLUdCL' },
':ftimestamp' : {S: '1584022680000' },
':ttimestamp' : {S: '1584023280000' }
},
};
b.scan(params, onScan);
function onScan(err, data) {
if (err) {
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Scan succeeded.");
var items = data.Items.map(function(val){
return unmarshalItem(val);
})
// continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
b.scan(params, onScan);
}
}
callback(null, items);
}
};
I have followed the link https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.NodeJs.04.html
I am getting time out here after a while. I have checked this link too
How to fetch/scan all items from `AWS dynamodb` using node.js
I am unable to return data properly i guess, Any suggestions ? Thanks
This worked for me by following Hank solution from here: How to fetch/scan all items from `AWS dynamodb` using node.js
exports.handler = async (event, context, callback) => {
let params = {
ExpressionAttributeNames: {
"#deviceid": "id",
"#devicetimestamp": "timestamp"
},
ExpressionAttributeValues: {
':unitID': {S: event.deviceid },
':ftimestamp' : {S: event.fromtime },
':ttimestamp' : {S: event.totime }
},
KeyConditionExpression: '#deviceid = :unitID and #devicetimestamp BETWEEN :ftimestamp and :ttimestamp',
TableName: 'IoTdata2'
};
let scanResults = [];
let items;
do {
items = await b.query(params).promise();
items.Items.map((item) => scanResults.push(unmarshalItem(item)));
params.ExclusiveStartKey = items.LastEvaluatedKey;
} while (typeof items.LastEvaluatedKey != "undefined");
var len = scanResults.length;
console.log(len);
callback(null, scanResults);
};

Unable to connect to dynammo db from Lambda

I have written a simple lambda function to list the tables on DynammoDb. But When i execute My code, I am unable to connect to the DB server . I am getting NetworkingError .
Below is the Code,
'use strict';
console.log('Loading function');
exports.handler = (event, context, callback) => {
console.log("$$$$$$$$$second test$$$$$$$$$$")
//var src_bkt = event.Records[0].s3.bucket.name;
//var src_key = event.Records[0].s3.object.key;
var AWS = require("aws-sdk");
//console.log(src_bkt)
console.log("##########################################")
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
var dynamodb = new AWS.DynamoDB({
region: 'us-east-1',
endpoint: "http://localhost:8000"
});
dynamodb.listTables({Limit: 10}, function(err, data) {
if (err) {
console.log("Error", err.code);
} else {
console.log("Table names are ", data.TableNames);
}
});
};
Any suggestion will be helpful
You need to create an instance of documentClient like below,
var docClient = new AWS.DynamoDB.DocumentClient();
And call 'listTables' with documentClient. Please find a sample code for PUT which shows how I am using documentClient,
var AWS = require("aws-sdk");
AWS.config.update({
region: "REGION"
});
var docClient = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
var table = "EmployeeDetails";
var params = {
TableName: table,
Item: {
"Id" : event.Id,
"FirstName": event.FirstName,
"LastName": event.LastName,
"Age": event.Age,
"Gender": event.Gender
}
};
docClient.put(params, function (err, data) {
if (err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
} else {
callback(null, "Added item:", JSON.stringify(data, null, 2))
}
});
};
If you are running in Lambda region is not mandatory but it will work with region as well.
The same code is in my GitHub as well - https://github.com/vnathv/DynamoDb-CRUD.git

Resources