@serverless-guy/lambda

Lambda has never been easier!

View the Project on GitHub serverless-guy/lambda

Introduction

@serverless-guy/lambda is a small and humble AWS Lambda function wrapper that let’s you write a much more cleaner, shorter lambda function in simplest way possible.

Who should use this?

Who should not use this?

Basic Usage

Suppose you want to write a lambda function that would return “hello world”, the code below shows how it is currently done without the wrapper.


module.exports = async (event, context) => {
  return {
    body: JSON.stringify({
      message: "hello world"
    }),
    headers: {
      "Access-Control-Allow-Origin": "*"
    },
    statusCode: 200
  };
};

Notice that you still have to set all the basics stuffs if you are to use APIGateway as an event, now take a look how it’s done using our handy-dandy notebook wrapper.

const { wrapper } = require("@serverless-guy/lambda");

module.exports = wrapper(async (request, response) => {
  return response({
    message: "hello world"
  });
});

Not only that we skipped setting of basic headers, and status, we also have made our code cleaner and better.

Now what if I want to return a custom status code?

Of course, that’s possible too! Take a look at the code below.

const { wrapper } = require("@serverless-guy/lambda");

module.exports = wrapper(async (request, response) => {
  const teapotStatus = 418;
  const body = {
    message: "I am a teapot"
  };

  return response(body, teapotStatus);
});

What about custom headers?

Yes, you can also change that one, Here!

const { wrapper } = require("@serverless-guy/lambda");

module.exports = wrapper(async (request, response) => {
  const teapotStatus = 418;
  const body = {
    message: "I am a teapot"
  };
  const headers = {
    "x-api-key": "thiscodeisawesomesoistheapikey"
  };

  return response(body, teapotStatus, headers);
});

And that’s it for the basic usage!

Check the pages below to get started.