Amazon Lambda writing to DynamoDB

0 votes

I'm new to dynamoDB and I'm trying to write some data to the table using Lambda. So far I have this:

'AddFood': function () {

    var FoodName = this.event.request.intent.slots.FoodName.value;
    var FoodCalories = this.event.request.intent.slots.FoodCalories.value;
    console.log('FoodName : ' + FoodName);

    const params = {
        TableName: 'Foods',
        Item: {
            'id': {"S": 3},
            'calories': {"S": FoodCalories},
            'food': {"S": FoodName}
            }
    };

    writeDynamoItem(params, myResult=>{
        var say = '';

        say = myResult;

        say = FoodName + ' with ' + FoodCalories + ' calories has been added ';
        this.response.speak(say).listen('try again');
        this.emit(':responseReady');

    });

    function writeDynamoItem(params, callback) {

        var AWS = require('aws-sdk');
        AWS.config.update({region: AWSregion});

        var docClient = new AWS.DynamoDB();

        console.log('writing item to DynamoDB table');

        docClient.putItem(params, function (err, data) {
            if (err) {
                callback(err, null)
            } else {
               callback(null, data)
            }
        });
    }
}

Does anyone know why the data is not appearing in the database? I have checked the IAM and the policy is set to AmazonDynamoDBFullAccess.

Sep 5, 2018 in AWS by bug_seeker
• 15,510 points
2,777 views

1 answer to this question.

0 votes

After making a few changes to the write function, the following code allowed me to write items to the database:

function writeDynamoItem(params, callback) {

const AWS = require('aws-sdk');
AWS.config.update({region: AWSregion});

const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});

console.log('writing item to DynamoDB table');

docClient.put(params, function (err, data) {
    if (err) {
        callback(err, null)
         console.error("Unable to write item. Error JSON:", JSON.stringify(err, null, 2))
    } else {
       callback(null, data)
    }
});

}

answered Sep 5, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer

How to link AWS Lambda function to Amazon CloudWatch ?

In order to create Log Group and ...READ MORE

answered Jul 20, 2018 in AWS by datageek
• 2,540 points
1,592 views
0 votes
1 answer

How to set-up DynamoDB trigger using AWS Lambda?

Well this code worked for me. You ...READ MORE

answered Aug 20, 2018 in AWS by Archana
• 4,170 points
2,859 views
0 votes
1 answer

How to upload an object into Amazon S3 in Lambda?

I suspect you are calling the context.done() function before s3.upload() has ...READ MORE

answered Sep 27, 2018 in AWS by Archana
• 4,170 points
1,235 views
+1 vote
1 answer

Do i need to use Amazon EC2 with DynamoDB?

You can use Amazon DynamoDB without restrictions ...READ MORE

answered Oct 11, 2018 in AWS by Archana
• 4,170 points
2,223 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
3 answers

Not able to pass params POST to AWS Lambda from Amazon API Gateway

For this template : application/x-www-form-urlencoded  This line below will ...READ MORE

answered Jun 13, 2018 in AWS by Cloud gunner
• 4,670 points
8,775 views
0 votes
1 answer

How do I roll-back a message to Amazon MQ (AMQ) from Lambda?

It is apparently not possible using STOMP ...READ MORE

answered Sep 3, 2018 in AWS by Priyaj
• 58,020 points
1,250 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP