Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

AWS-Serverless-Express Is Awesome

4/23/2018

0 Comments

 
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:
npm install --save aws-serverless-express
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.
const awsServerlessExpress = require('aws-serverless-express')
const routes = require('./routes')
const server = awsServerlessExpress.createServer(routes)

exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);
Notice that I require this file routes.js in my local directory. That file looks like this. 
const emptyRouteHandler = require('./src/routes/empty-route');
const express = require('express');
const routes = express();

routes.get('/', emptyRouteHandler);

module.exports = routes;
Then your emptyRouteHandler could just be a regular JavaScript function that takes the request  and response  objects.
module.exports = function (req, res) {

   // do some cool stuff here...

    res.send('cool stuff');
}

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.
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021