0

I have set up a simple serverless rest api using Node JS and AWS Lambda.

The deployment is done using AWS SAM

Below is the SAM Template :

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Serverless API With SAM

Resources:
  createUser:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.create
      MemorySize: 128
      Runtime: nodejs12.x
      Timeout: 3
      Events:
        createUserApi:
          Type: Api
          Properties : 
              Path : /users
              Method : post
  
  listUsers:
     Type: AWS::Serverless::Function
     Properties:
      Handler: handler.list
      MemorySize: 128
      Runtime: nodejs12.x
      Timeout: 3
      Events:
        listUserApi:
          Type: Api
          Properties : 
              Path : /users
              Method : get

This works fine but below is my observation regarding stacks created. It creates two AWS Lambda functions instead of one.

Both contain two APIs listed as -

createUser
listUsers

Can it not contain only on Lambda function with these two handlers inside ?

handler.js file :

const connectedToDb = require('./src/db/db.js');
const User = require('./src/model/user.js');

module.exports.create = async (event,context) =>{

   console.log('create function called !!');

   try{
    console.log('before connecting to DB ');   
    await connectedToDb();
    console.log('after connecting to DB ');   
    const usr = new User(JSON.parse(event.body));
    console.log('saving a user now  with ',event.body);   
    await usr.save();
    return{
        statusCode : 200,
        body:JSON.stringify(usr)
    }

   }catch(err){
        return{
            statusCode : err.statusCode || 500,
            body : 'Cannot Create Order'
        }
   }
}

module.exports.list = async (event,context) => {

    console.log('listing all users');
    try{
        await connectedToDb();
        const users = await User.find({});
        console.log('users are == ',users);
        return {
            statusCode:200,
            body:JSON.stringify(users)
        }

    }catch(err){

        return {
            statusCode:err || 500,
            body:JSON.stringify(err)
        }
    }
}
Atul
  • 1,560
  • 5
  • 30
  • 75
  • 3
    In your template, you have defined two lambda functions so 2 are created – Yahya Hussein Jul 12 '20 at 09:57
  • Yes. But both lambda functions create EXACTLY same code . Meaning , listUser lambda also contains createUser handler function and vice a versa. Shall they not contain ONLY their handler code ? – Atul Jul 12 '20 at 11:55
  • The purpose of lambdas is to create functions to call on demand. The deployment is following the template which is telling it to create two resources because each resource is linked to one handler. These functions aren't doing the same task so it makes sense to make it separate. If you want to mock an express server under one lambda, you can try aws-serverless-express. Just keep in mind that lambdas are not meant to be used for large express servers. Larger the code, the slower it will be for the lambda to scale on cold-starts. – Elbert Bae Sep 04 '20 at 23:18

0 Answers0