Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Invisible ubiquitious servers across the land


Overview

This is a hands-on narrated tour on how to end up with a serverless app running in a server cloud.

I want you to feel confident that you’ve mastered this skill. That’s why this takes a hands-on approach where you type in commands and we explain the responses and possible troubleshooting. This is a “deep dive” because all details are presented.

Like a good music DJ, I’ve carefully arranged the presentation of concepts into a sequence for easy learning, so you don’t have to spend as much time as me making sense of the flood of material around this subject.

Sentences that begin with PROTIP are a high point of this website to point out wisdom and advice from experience. NOTE point out observations that many miss. Search for them if you only want “TL;DR” (Too Long Didn’t Read) highlights.

Stuck? Contact me and I or one of my friends will help you.


The sentiment about servers is reflected in the wi-fi password attendees of #ServelessConf type in: serverless hateservers-452x183-70kb

About Serverless

The term “serverless” has several meanings.

Initially, the term describes an architectural style where developers shift concerned about the hardware and its ability to scale.

The name “serverless” has been co-opted by enprepreneur @austencollins who built the Serverless company around its open-source serverless framework on GitHub,

His initial Serverless presentation at AWS:Invent 2015

Social media:

Serverless framework install

  1. Install Node.js as a pre-requisite since the framework is written in Node.js.

  2. Install the serverless-framework

    
    npm install -g serverless
    
  3. Verify version:

    
    serverless -v
    

    NOTE: Serverless was in Beta version 0.5.6 as of June 2016, with v1.0 announced 24 June 2016.

Update Serverless

PROTIP: I subscribed to get notifications of changes, and I can see a lot of refactoring is happening very quickly, so I suggest that you update frequently.


   npm update -g serverless
   

Nothing returns if you’re up-to-date.

Look around

  1. Get summary of commands using the abbreviated command:

    sls

    the response:

     _______                             __
    |   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
    |   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
    |____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
    |   |   |             The Serverless Application Framework
    |       |                           serverless.com, v0.5.6
    `-------'
     
    Commands
           * Serverless documentation: http://docs.serverless.com
           * You can run commands with "serverless" or the shortcut "sls"
           * Pass "--help" after any <context> <action> for contextual help
           * Add "--debug" to any command for extra useful logs
     
    project ........ create, init, install, remove
    function ....... create, deploy, logs, remove, rollback, run
    endpoint ....... deploy, remove
    event .......... deploy, remove
    dash ........... deploy, summary
    stage .......... create, remove
    region ......... create, remove
    resources ...... deploy, diff, remove
    plugin ......... create
    variables ...... list, set, unset
    
  2. Verify:

    
    which serverless
    
    /Users/mac/.npm-packages/bin/serverless
    
  3. Navigate to framework folders and files:

    
    cd ~/.npm-packages/lib/node_modules/serverless
    ls -al
    
  4. View the README.md file using a Markdown reader:

    
    atom README.md
    

    Instead of the Atom text editor (from GitHub), alt-click on the file to select Markdown Preview.

    The file contains a list of projects, plugins, and consultants who provide services.

Libraries

React Serverless app running in Azure?

https://github.com/99xt/serverless-dependency-install Serverless plugin to manage dependencies

with architectural best practices using NPM, Angular, Gulp.

https://github.com/99xt/serverless-dynamodb-local

Projects

  1. Create a folder to hold serverless projects. For example:

    ~/gits/sls

  2. Create a blank project.

    serverless create project

    Alternately:

  3. Pick a sample project from https://github.com:

  4. Load a sample project from above:

    serverless project install serverless-graphql

    The response:

    _______                             __
    |   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
    |   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
    |____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
    |   |   |             The Serverless Application Framework
    |       |                           serverless.com, v0.5.6
    `-------'
     
    Serverless: Installing Serverless Project "serverless-graphql"...  
    Serverless: Downloading project and installing dependencies...  
    Serverless: Initializing Serverless Project...  
    Serverless: Enter a new stage name for this project:  (dev)
    
  5. Press Enter to accept the default

  6. Keyboard up and down to select:

    Serverless: For the "dev" stage, do you want to use an existing Amazon Web Services profile or create a new one?
      > Existing Profile
     Create A New Profile
    Serverless: Select a profile for your project:
     default
      > GitHubPublisher1
     me_dev
     serverless-graphql_dev
    Serverless: Creating stage "dev"...  
    Serverless: Select a new region for your stage:
     us-east-1
      > us-west-2
     eu-west-1
     eu-central-1
     ap-northeast-1
    Serverless: Creating region "us-west-2" in stage "dev"...  
    Serverless: Deploying resources to stage "dev" in region "us-west-2" via Cloudformation (~3 minutes)...
    

If you see these error messages, let me know because I’m stuck!!!!

   /Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:61
        fn = function () { throw arg; };
                           ^

ServerlessError: ServerlessError: The security token included in the request is invalid.
  at new ServerlessError (/Users/mac/.npm-packages/lib/node_modules/serverless/lib/Error.js:17:11)
  at ResourcesDeploy.<anonymous> (/Users/mac/.npm-packages/lib/node_modules/serverless/lib/actions/ResourcesDeploy.js:241:25)
  at ResourcesDeploy.tryCatcher (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/util.js:16:23)
  at Promise._settlePromiseFromHandler (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:502:31)
  at Promise._settlePromise (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:559:18)
  at Promise._settlePromise0 (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:604:10)
  at Promise._settlePromises (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/promise.js:679:18)
  at Async._drainQueue (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:138:16)
  at Async._drainQueues (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:148:10)
  at Immediate.Async.drainQueues [as _onImmediate] (/Users/mac/.npm-packages/lib/node_modules/serverless/node_modules/bluebird/js/release/async.js:17:14)
  at tryOnImmediate (timers.js:543:15)
  at processImmediate [as _immediateCallback] (timers.js:523:5)

Serverless Framework

The serverless framework save developers’ time by standardizing the structure of folders and files.

s-project.json       // Project file (JSON or YAML)
s-resources-cf.json  // CloudFormation template
s-templates.json     // Config templates and variables
admin.env            // AWS Profiles (gitignored)
_meta                // Metadata (gitignored)
function1            // A custom function
  |__event.json
  |__handler.js
  |__s-function.json
   

This set of project file is what developers work with.

The serverless framework programming which read and process project files is written in Node JavaScript.

The s-project.json lists plugins:

{
  "name": "notes",
  "custom": {},
  "plugins": [
    "serverless-client-s3"
  ]
}
   

A key differentiator with the Serverless Framework is that infrastructure specs are defined as code in one project. s-resources-cf.json is a AWS CloudFormation template specifying security (IAM) roles, SNS email topics, DynamoDB tables, Queues, ARNs.

Components are deployed per stage (dev, prod, etc.), allowing developers to easily deploy separate prod, test and dev environments.

Within the folder for each function is a s-function.json file containing metadata such as plugins installed with the project.

When the framework is created for a project, a .gitignore file is created with other files to specify private and temporary work files that should not be publicly uploaded to GitHub.

Project metadata in file _meta outputs and user variables used in function configurations. But due to the sensitive nature of these variables, the file needs to be gitignore’d by default. A workaround is to use the Serverless Meta Sync plugin which stores project metadata in S3.

admin.env

Serverless Command-line

Lambda functions can be defined from a command-line using the Serverless framework.

AWS May 2016 Webinar Series - Deep Dive on Serverless Web Applications

http://abalone0204.github.io/2016/05/22/serverless-simple-crud/

Keeping Secrets

BLAH: AWS Lambda doesn’t allow setting and reference to operating system environment variables.

Secrets such as DB connection string or encryption key are secure values that should not be checked into version control (specified in a .gitignore file).

To provide Lambda functions with deploy.env file in combination with the --configFile flag to set values which will be prepended to your compiled Lambda function as process.env environment variables before it gets uploaded to S3.

Local Runs for testing

If we’re taking a “test-first” approach to achieve code maturity, doing test runs locally before committing to a team branch is important.

Let’s examine the choices to emulate AWS Lambda locally:

  1. Install the “Command line tool for locally running and remotely deploying your node.js applications to Amazon Lambda.” from github.com/motdotla/node-lambda

    npm install -g node-lambda

  2. Describe tests in a JSON file.

Plugins

The heart of Serverless are its Plugins. Several plugins come with the Framework.

PROTIP: Plugins need to be installed for each project that uses each.

  1. List plugins installed.

    ls _meta/

  2. Navigate your active directory to the root of your project.
  3. Plugins are downloaded from GitHub by npm:

Functions of the same component can use the lib folder to share common code.

Get Permissions

Since

aws-lambda-node-js-programming

http://stackoverflow.com/questions/37779324/how-to-troubleshoot-serverless-iam-permissions

Resources

Twitter: @goserverless

Phillip Muens (@pmmuens, github.com/pmuens) from Germany

Matthew Fuller

Jake Knowles

  • AWS Lambda: Serverless Microservices Guide with Simple Instructions

John McKim @johncmckim blogs on Medium:

Future topics

  • Handlers that compress or transform objects while being uploaded to Amazon S3.

Social

Serverless

@goserverless

Google Cloud

@googlecloud

More on Serverless

This is one of a series on Serverless computing