0

New to AWS and working on Lambda with Node.JS

I have the following code works on my local, but once I uploaded to Lambda (via CLI with successful status) .. however I realized it complaint about index.hanlder issue. Hence after Googling, I updated my code to the 2nd version, it runs fine with Status 200 but return me a NULL.

I've tested the local code and it returns my data from DynamoDB. What seems to be the mistake here?

To provide extra information:

  • My .zip contains the following (index.js, node_modules, package-lock.json, package.json)

Local Code

const express = require('express');
const router = new express.Router();
const awsSDK = require('aws-sdk');
const dynamodb = new awsSDK.DynamoDB({region:'maskedData', apiVersion:'maskedData'});

router.get('/get', (req,res) => {
const params = {
        TableName: "maskedData"
    };
    dynamodb.scan(params, function(err, data){
        if(err){
            console.log(err);
            // callback(err);
            res.send(err);
        } else {
            console.log(data);
            // callback(null, data);
            res.send(data);
        }
    })

})

module.exports = router;

2nd Version

const express = require('express');
const router = new express.Router();
const awsSDK = require('aws-sdk');
const dynamodb = new awsSDK.DynamoDB({region:'maskedData', apiVersion:'maskedData'});

exports.handler = router.get('/get', (req,res) => {
// router.get('/get', (req,res) => {
const params = {
        TableName: "maskedData"
    };
    dynamodb.scan(params, function(err, data){
        if(err){
            console.log(err);
            // callback(err);
            res.send(err);
        } else {
            console.log(data);
            // callback(null, data);
            res.send(data);
        }
    })

})

// module.exports = router
// exports.handler = router;

1 Answer 1

1

To be used as lambda you need to have handler function which have event, context and callback parameter (optional). Something like this

const express = require("express");
const awsSDK = require("aws-sdk");
const dynamodb = new awsSDK.DynamoDB({"region": "maskedData", "apiVersion": "maskedData"});

exports.handler = async event => {
  const params = {
    "TableName": "maskedData"
  };
  const data = await dynamodb.scan(params).promise();
  return data;
};

aws sdk supports promise version so you can use that instead of callback based.

Sign up to request clarification or add additional context in comments.

9 Comments

I do aware there is this Event, Context, Callback. In fact, I practice on Lambda Editor before moving to code on local. However, if I were to do this on my local, how am I suppose to handle the express and express.router() ??
what is your use case for lambda? Are you trying to use express on lambda?
Yes, will be using Express, Body-Parser and etc on Lambda.
how are you planning to use the express api from lambda? I don't think thay is the right way to go about it. How will you access the routes? and since lambda is short lived, the express app will go away
I think you just woke me up.. lambda is stateless and meant to be performed quick tasks. Thank you for pointing out and quick answer!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.