Things like this are what make it difficult to argue that AWS isn't still the leader in serverless. When we say, "a Lambda function" then sure, we could just expose one function. With the nodejs package aws-serverless-express you can easily handle lots of different endpoints that may or may not have query parameters as well. To me this makes my lambda functions a lot more logically organized as routes.
How It Works
In order to use the library, first install it:
When creating a "Lambda function" there is actually one single function that is called to begin the program. This function is conventionally named exports.handler and located in a file named index.js. Here's a simple index.js file where our function to kick off the Lambda hooks right into aws-serverless-express.
Notice that I require this file routes.js in my local directory. That file looks like this.
Nice For Cron Jobs Too!
If you like to set up cron jobs in linux then you should be thrilled to learn that you can offload those jobs onto serverless architecture as well! If you're looking for the option in the AWS console it's referred to as a "CloudWatch Scheduled Event" in AWS lingo. You can then set a trigger for the event, and one fo the options is an input field that takes an AWS cron expression. You can still use aws-serverless-express for cron jobs if you wanted to, and if you do it will just trigger the default '/' route. I'd say there's a good chance aws-serverless-express will work with other Lambda event triggers as well, although I haven't tested it.
Easily Scaffold A Lambda Project With CodeStar
If you want to quickly get started on an AWS Lambda project that already has aws-serverless-express set up for you, a cool way to do it is with AWS CodeStar. Create a new project and filter the options by Web Service, Node.js, and AWS Lambda. Then choose the template with all three. This will not only scaffold out code for you, but it will also hook it up to the CodeStar dashboard in the AWS console and create a CI/CD pipeline for you right off the bat! Of course going all-out AWS, all-out serverless for everything does come with some vendor lock-in (and to be honest AWS CodePipeline and CloudFormation are not very fast). However, AWS Lambda is still the mature, robust serverless function provider today, and with literally 1 million free lambda executions per month you might as well at least try it out.
The posts on this site are written and maintained by Jim Lynch. About Jim...