Lambda has never been easier!
@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.
@serverless-guy/lambda
is for developers who seek a small AWS lambda function wrapper that allows them to reuse existing code (such as response template, “middleware”, error handling, etc.) in easiest way possible.serverless framework
and most likely to work on medium to huge project.express JS
or restify
.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.
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);
});
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.