AWS Lambda function written in nodejs is not updating my DynamoDB

Viewed 866

I'm a student who is new to AWS, but I have reached a blocker.... I am trying to use a lambda function to update an attribute for an item in my dynamodb table. The lambda function is being triggered, but for some reason I am unable to update the item.

I was able to successfully delete the item from the dynamodb, but when I try to update an attribute for an item nothing happens.

The attribute human_confirmed is not updating to true after the function executes. I've been trying different things I've found on Google, but nothing is working :(

console.log('Loading function');
var AWS = require('aws-sdk');

exports.handler = (event, context, callback) => {
    AWS.config.update({
      region: "us-east-1"
    });

    var dynamodb = new AWS.DynamoDB.DocumentClient();
    var instance = event.instanceID;
    var InstanceName = instance;

    var params = {
        TableName: "reminders",
        Key: {
            "instanceID": {
             S: InstanceName
            },
        },
      UpdateExpression: 'SET #a = :x',
      ExpressionAttributeNames: {'#a' : 'human_confirmed'},
      ExpressionAttributeValues: {
        ':x' : 'true',
      },
      ReturnValues:"UPDATED_NEW"
    };
    dynamodb.update(params, function(err, data) {
        if (err) 
            callback(err, null); // an error occurred
        else     
            callback(null, data);           // successful response
    });
    callback(null, "Updating resource from reminder table: " + InstanceName + ".... The system will no longer contain automated emails about this resource's tags!");
};

2 Answers

make sure you have this configuration: dynamodb:PutItem in your serverless.yml file

iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "dynamodb:PutItem"
      Resource: "*"

Thanks for the help @kaxi1993 I believe my IAMRole permissions were correct. Here was the code that worked for me.

'use strict';
console.log('Loading function');
var AWS = require('aws-sdk');

var dynamodb = new AWS.DynamoDB({apiVersion: '2012-08-10'});

exports.handler = (event, context, callback) => {
AWS.config.update({
  region: 'us-east-1'
});

var instance = event.instanceID;

var params = {
    TableName: 'reminders',
    Key: {
        'instanceID': {
         S: instance
        }
    },
    UpdateExpression: 'set human_confirmed = :x',
    ExpressionAttributeValues: {
       ':x': {S: 'true'},
    },
    ReturnValues: 'UPDATED_NEW'
};
dynamodb.updateItem(params, function(err, data) {
    if (err) 
        callback(err, null); // an error occurred
    else     
        callback(null, data);           // successful response
});
callback(null, 'Updating instance from reminder table: ' + instance + '.... The system will no longer contain automated emails about this resource.');

};

Related