Simple serverless hello world example
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
helloworld
.gitignore
LICENSE
README.md

README.md

Serverless hello-world

These are my notes from a quick fire attempt to learn the basic concepts of "serverless" application architecture. Writing these notes and running the scripts took about 35mins, so this is pretty easy concept to get your head around if you're familiar with node-js and AWS.

Motivation

AWS Lambda is a scaleable platform for functions. You can deploy a function and run it without having to worry about the server infrastructure that it runs on. From their site (https://aws.amazon.com/lambda/details/):

AWS Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you. You can use AWS Lambda to extend other AWS services with custom logic, or create your own back-end services that operate at AWS scale, performance, and security.

You pay for what you use, based on how long the function runs for and how much memory it uses. The serverless function can not store any data within the local filesystem. Storage can be achieved by other means, such as writing to an S3 bucket, or using another AWS service.

The word "serverless" is sort of a misnomer. It doesn't mean the server goes away; merely that you as a developer no longer have to worry about your servers, assuming your program has been developed with "serverless" in mind.

One limitation that comes to mind: there does not appear to be anyway to control what CPU your function runs on. This might be a limitation if your function is especially CPU intensive.

serverless.com

See https://serverless.com/

A number of competitors to AWS Lambda have cropped up recently, offered by all the usual players (MS, Google, etc). You can use some helpful scripts at serverless.com to manage deployments between these platforms. It means you don't necessarily have to be stuck inside AWS to make use of this concept.

Notes on setting up a serverless function

These are my notes from a very handy article, written by Adnan Rahić, here: https://hackernoon.com/a-crash-course-on-serverless-with-node-js-632b37d58b44

Setting up

  1. Install serverless

    $ npm install -g serverless

  2. Login to AWS Console

  3. Services -> IAM -> Users -> Add User

  4. Enter a unique name for the user.

  5. Under "Select AWS Access Type", select "Programmatic Access."

  6. Then "Next: Permissions"

  7. Under "Set permissions for $USER_NAME", click "Attach existing policies directly."

  8. In the table that appears, check "AdministratorAccess" and nothing else.

  9. Finally, hit "Create User".

  10. Store the key and secret key in a safe place.

Getting the credentials into serverless

Serverless has a help function that stores the credentials in your ~/.aws/credentials file; this seems to be the default behavior on OSX anyway:

$ serverless config credentials --provider aws --key ??????? --secret ??????

Create the function

$ serverless create --template aws-nodejs --path helloworld

This creates a new folder with some simple boilerplate for a node-js function. The folder includes its own .gitignore. The boilerplate javascript handler looks like this:

'use strict';

module.exports.hello = (event, context, callback) => {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: 'Go Serverless v1.0! Your function executed successfully!',
            input: event,
        }),
    };

    callback(null, response);

    // Use this code if you don't use the http event with the LAMBDA-PROXY integration
    // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

Inside serverless.yml?

Extremely minimal at this point:

service: helloworld

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello

We need to update it to expose the hello function:

service: helloworld

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello
    events:
    - http:
        path: hello/get
        method: get

Deploy to AWS

Easy:

$ serverless deploy -v

Test the function out:

$ serverless invoke -f hello -l

Installing offline serverless

This is a recipe for allowing serverless functionality on a local dev environment. This is handy because deploying to AWS appears to still be a low process (20+ seconds). From the boilerplate folder created by serverless earlier:

$ npm init
$ npm install serverless-offline --save-dev

Make another modification to serverless.yml so it knows about the offline plugin:

service: helloworld

provider:
  name: aws
  runtime: nodejs6.10

functions:
  hello:
    handler: handler.hello
    events:
    - http:
        path: hello/get
        method: get

plugins:
    - serverless-offline

To run the local emulation:

$ serverless offline start