Deploy an AWS Lambda function to store data in DynamoDB using the Serverless Framework

Share this video with your friends

Send Tweet

In order to store data into a DynamoDB table we first need to setup the permissions for the Lambda function to have write access. Further we can use the DocumentClient API shipping with AWS to write the data coming from a request to the DynamoDB table.

Daniel Uhl
Daniel Uhl
~ 6 years ago

Great so far!

You have a couple minor typos in your last Create.js in the transcript:

const AWS = require('aws-sdk');
const uuid = require('uuid/v4');

const client = new AWS.DynamoDB.documentClient();

module.exports.run = async (event) => {
  const data = JSON.parse(event.body);
  const params = {
    TableName: 'todos',
    Item: {
      id: uuid(),
      text: data.text,
      checked: false
    }
  };
};
Brain Imascono
Brain Imascono
~ 5 years ago

If you are using the cmd and single quotes are giving you problems, use double quotes and escape the quotes within the string: curl -X POST https://zm7tuyj33a.execute-api.us-east-1.amazonaws.com/dev/todos --data "{"text": "learn serverless"}"

Mark
Mark
~ 4 years ago

I was having issues with posting data with curl. I was getting "Internal Server error". After checking CloudWatch it turns out that the updated uuid package I was using, had changed its API

From https://www.npmjs.com/package/uuid

const { v4: uuidv4 } = require('uuid');
uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'

So I had to use uuidv4() for creating a todo id to make it work