Wilson Mar bio photo

Wilson Mar

Hello. Join me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Invisible ubiquitious server clouds across the land


Overview

This describes the ecosystem around the “Serverless” computing concept to build applications as a collection of microservices that run in response to events, auto-scaled in a cloud.

This article is a pre-requisite to my tutorial on creating a new Amazon Lambda serverless app running in the Amazon cloud.


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

Serverless = FaaS

“Serverless” refer to an architectural style called FaaS (Function as a Service) where programming code are independently deployed and run on a cloud system rather than on-premises servers. The developer simply uploads the source code, then leave it to pros at the cloud provider to take care of security, monitoring, disk space management, log management, scaling, redundancy, backup, crash reporting, etc..

Zero system administration by the developer.

It’s a fast route to get apps in production.

PROTIP: It’s still up to developers to do testing and performance measuring and tuning. Use of multi-tenancy makes for response-time variation. So do sythentic transactions outside the cloud vendor to monitor user experience, and to keep your app in cache to avoid process start-up after sleeping.

Mike Roberts at http://martinfowler.com/articles/serverless.html wrote “Depending on the circumstances, such systems can significantly reduce operational cost and complexity at a cost of vendor dependencies and (at the moment) immaturity of supporting services.”

Database idle costs money!

“You never pay for idle” Austen says in an interview with by CloudAcademy Introduction to the Serverless Paradigm [23:50]

PROTIP: But this is not true within AWS if you use DynamoDB, which Amazon touts as the default database. While Lambda does not incur charges while idle, DynamoDB instances do incur charges for data stored even though no data is read or written to it.

PROTIP: On AWS use SimpleDB instead of DynamoDB for true no-cost idle.

FaaS Providers

There is plenty of competition (to keep prices low).

PROTIP: The future of FaaS vendors isn’t the front-end but the back-end services that include API Gateways and Artificial Intelligence features such as image recognition, text sentiment analysis, natural language process (NLP), and Machine Learning.

Frameworks

AWS Lambda

The AWS Serverless Application Model (SAM) at https://github.com/awslabs/serverless-application-model was announced Nov 2016 to define the SAM format to build Cloud Formation templates that access Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by serverless applications.

Two AWS evangelists created pre-built templates with manual instructions, which they walked though in 30 minutes on Twitch.tv:

  1. Get an AWS IAM User with AWSCodeStarFullAccess.
  2. Login the AWS Console to select region N. Virginia.
  3. In AWS CodeStar. Start a project. Node.js. create role.
  4. Choose a project template:
    • Under Application Category select Web Application.
    • Under Programming Languages select Node.js.
    • Pick “Node.JS Web Application AWS Lambda (running serverless)”.

Azure Functions

Marketing information is at https://azure.microsoft.com/services/functions

  1. Create an Azure Storage account.
  2. Search for “Function App” in the Azure Marketplace.
  3. Click Create.
  4. Provide an app name that’s globally unique among all others public.

    .azurewebsites.net

  5. Create or use a Resource Group to manage your similar functions as a group.
  6. Select a Location closest to you.

    PROTIP: Include the location as a suffix in your app name.

  7. Selection of Memory Allocation can be adjusted once you figure out how much after running a few times.
  8. Create.
  9. Click Refresh or the “bell” icon to see messages related.

  10. After deployment, click the Resource Group, then the app name.
  11. Click “Webhood + API” and JavaScript language (or C#).
  12. Copy the Function URL and paste it in a browser.
  13. Click Logs to see the history tracking.
  14. Add a Parameter.

IBM Bluemix OpenWhisk

https://developer.ibm.com/openwhisk

The advantage of IBM’s hybrid-cloud approach is that one can use IBM’s proprietary Bluemix UI and then use command-line with OpenWhisk (which is open sourced).

ibm-openwhisk-arch-720x168

Google Cloud Functions

https://cloud.google.com/functions

@googlecloud

Google Firebase

Iron.io and other on-premises

Iron.io has their Gesalt Framework

Usage in the wild

Compelling Science Fiction.com uses Simple Email to read emails and sends it to S3. Lambda emails notifications. Stories are saved in DynamoDB. Python pulls data.

Eric Jonas does hyperperameter sweeps, Monte Carlo simulations. Spark requires dedicated servers and is not very elastic. map of map reduce.

His Simultaneous “Big Lambda” talk

https://www.emitconference.com Emit Conference for “event-driven architectures” August in San Francisco has people who’ve done it talk about it.

Concerns

PROTIP: The other side of freedom from server hassles is that developers also give away control.

Having one’s data in another company’s cloud requires trust in that company’s ability to keep data secure, redundant, and pricing fair.

PROTIP: Going with a particular vendor’s API means that you need to keep up with changes in APIs that can occur frequently, even though they may not apply to your own operation.

There is the danger of vendor lock-in.

But frameworks have emerged to allieviate that:

Serverless Coding Frameworks

  • Shep (bustle.com)
  • Apex, from Terraform, a more feature-rich server product.
  • ClaudiaJS
  • Sparta for AWS Lambda, as a Golang app, is baked into deliverable binary (unlike Node).

    • https://gospart.io
    • https://github.com/mweagle/Sparta
  • Gordon is written in Python

  • Zappa is written in Python for Flask apps running only on AWS Lambda

  • The Serverless Framework

Apex

http://apex.run/

“Apex” manages AWS Lambda functions.

See https://github.com/apex/apex/blob/master/docs/infra.md

from Terraform, the same company

“Apex” from Terraform, the same company

a more feature-rich server product.

Currently the following variables are exposed to Terraform:

aws_region the AWS region name such as "us-west-2"
apex_environment the environment name such as "prod" or "stage"
apex_function_role the Lambda role ARN
apex_function_arns A map of all lambda functions
apex_function_names A map of all the names of the lambda functions

Serverless the company

The name “serverless” has been co-opted by enprepreneur Austen Collins @austencollins who built the Serverless company around its open-source Serverless Framework on GitHub, a combination of command-line utilities and conventions.

His initial Serverless presentation at AWS:Invent 2015

Hacker News announced it in 2015 when the product was first called JAWS.

The company’s social media:

Install Serverless Framework

PROSTIP: The Serverless Framework is a command-line tool, providing scaffolding, workflow automation and best practices for developing and deploying your serverless architecture.

Below is an annotated, expanded verson of this:

  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.

    The command notes whether you should npm install -g npm

    Look around

  4. Get summary of commands using the abbreviated command:

    sls

    The response (version 1.28.0) you’ll have to extend the terminal screen to avoid line wrapping:

    Commands
           * You can run commands with "serverless" or the shortcut "sls"
           * Pass "--verbose" to this command to get in-depth plugin info
           * Pass "--no-color" to disable CLI colors
           * Pass "--help" after any <command> for contextual help
     
    Framework
           * Documentation: https://serverless.com/framework/docs/
     
    config ........................ Configure Serverless
    config credentials ............ Configures a new provider profile for the Serverless Framework
    create ........................ Create new Serverless service
    deploy ........................ Deploy a Serverless service
    deploy function ............... Deploy a single function from the service
    deploy list ................... List deployed version of your Serverless Service
    deploy list functions ......... List all the deployed functions and their versions
    info .......................... Display information about the service
    install ....................... Install a Serverless service from GitHub or a plugin from the Serverless registry
    invoke ........................ Invoke a deployed function
    invoke local .................. Invoke function locally
    logs .......................... Output the logs of a deployed function
    metrics ....................... Show metrics for a specific function
    package ....................... Packages a Serverless service
    plugin ........................ Plugin management for Serverless
    plugin install ................ Install and add a plugin to your service
    plugin uninstall .............. Uninstall and remove a plugin from your service
    plugin list ................... Lists all available plugins
    plugin search ................. Search for plugins
    print ......................... Print your compiled and resolved config file
    remove ........................ Remove Serverless service and all resources
    rollback ...................... Rollback the Serverless service to a specific deployment
    rollback function ............. Rollback the function to the previous version
    slstats ....................... Enable or disable stats
     
    Platform (Beta)
           * The Serverless Platform is currently in experimental beta. Follow the docs below to get started.
           * Documentation: https://serverless.com/platform/docs/
     
    emit .......................... Emits an event to a running Event Gateway
    login ......................... Login or sign up for the Serverless Platform
    logout ........................ Logout from the Serverless Platform
    run ........................... Runs the Event Gateway and the Emulator
     
    Plugins
    AwsConfigCredentials, Config, Create, Deploy, Emit, Info, Install, Invoke, Login, Logout, Logs, Metrics, Package, Plugin, PluginInstall, PluginList, PluginSearch, PluginUninstall, Print, Remove, Rollback, Run, SlStats
    
  5. Verify where the executable is located:

    
    command -v serverless
    

    The response:

    /usr/local/bin/serverless
    
  6. Get the location of files

    find / -name serverless 2>/dev/null
    
  7. Navigate to framework folders and files:

    
    cd /usr/local/lib/node_modules/serverless
    ls -al
    

    In the serverless framework folder

  8. 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.

    https://serverless.com/framework

    Serverless is open-sourced under the MIT license and actively maintained by a full-time, venture-backed team.

    Files of Serverless Framework

README.md, CONTRIBUTING.md, LICENSE.txt are standard GitHub files.

.eslintrc.js contains rules for how lint programs identify issues with code formatting.

.jsbeautifyrc contains settings for JavaScript code beautify program.

.jscsrc

.npmignore defines files and folders for NPM to ignore.

.travis.yml is used by the Travis task runner.

Dockerfile defines how to load the server in Docker.

docker-compose.yml

Folders

.idea contains settings for use by the IDEA IDE.

.github

bin

coverage

lib contains library files

node_modules are populated

scripts

tests


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.

Lambda functions have a 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.

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).

Sample Hello Project

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

    
    mkdir ~/gits/sls
    cd ~/gits/sls
    

    Serverless Platform Online

  2. Open the cloud dashboard:

    serverless login
    

    This opens a browser to:

    https://dashboard.serverless.com/?cli=true

  3. Log in using your GitHub or Google credentials.

    • App visualization - View the services you’ve deployed with the Serverless Framework. inspect config, monitor deployment activity and more.

    • Collaborate - Share your Serverless Framework projects with teammates.

    • Hosted event gateway - Your account includes a free Event Gateway. Create event-driven APIs, workflows and integrations with any systems you’d like to integrate with serverless functions-as-a-service.

  4. Click on the + App button An app name is suggested by concatenating your account and app plus the “slsgateway.com”, such as:

    myname-myapp-dev.slsgateway.com

  5. Edit the name and click SAVE.
  6. Return to the Terminal to see:

    Serverless: You are now logged in

    New local

  7. Create a serverless.yml file using a template for a nodejs app running on the aws cloud provider:

    sls create -t aws-nodejs --path aws-nodejs-hello
    

    -t is a contraction of command parameter --templatef

    Serverless: Generating boilerplate...
     _______                             __
    |   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
    |   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
    |____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
    |   |   |             The Serverless Application Framework
    |       |                           serverless.com, v1.28.0
     -------'
     
    Serverless: Successfully generated boilerplate for template: "aws-nodejs"
    Serverless: NOTE: Please update the "service" property in serverless.yml with your service name
    
  8. The previous command (like Git clone) creates a folder, so

    cd aws-nodejs-hello

    Two files are created in the folder:

    • handler.js which is the coding for the Lambda function.
    • serverless.yml defines the configuration.

  9. Edit file serverless.yml to add permissions to use Amazon’s SES (Simple Email Service):

    service: aws-nodejs-hello
    app: myapp-dev
    tenant: wilsonmar
     
    provider:
      name: aws
      runtime: nodejs6.10
      iamRoleStatements:
             - Effect: "Allow"
     Action:
       - "ses:*"
     Resource:
       - "*"
     
    functions:
    hello:
       handler: handler.hello
       events:
          - http:
              path: hello
              mention: get
    

    Functions

    The name of the function is defined (“hello”).

    Handlers

    PROTIP: Handlers can compress or transform objects while being uploaded to Amazon S3.

  10. The other lines beginning with # are comments that can be deleted.

    Deploy function

  11. Deploy the app (-verbosely):

    sls deploy -v

    PROTIP: Be mindful of this message: Serverless: WARNING: Missing “tenant” and “app” properties in serverless.yml. Without these properties, you can not publish the service to the Serverless Platform.

    The response is a lot of lines, including something like this:

    Service Information
    service: aws-nodejs-hello
    stage: dev
    region: us-east-1
    stack: aws-nodejs-hello-dev
    api keys:
      None
    endpoints:
      None
    functions:
      hello: aws-nodejs-hello-dev-hello
     
    *Stack Outputs*
    HelloLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:903265058630:function:aws-nodejs-hello-dev-hello:1
    ServerlessDeploymentBucketName: aws-nodejs-hello-dev-serverlessdeploymentbucket-wafuwil8ilvx
    

    You’ll get a “congrats on your first deploy” email if it is:

    Congrats on your very first deployment on the Serverless Framework! Here's to many more. 🎉 
     
    Use these resources from our community to help you build apps faster:
    Serverless examples repository for a list of ready-to-go, deployable serverless services
    Serverless plug-ins to extend base functionality
    David
    your friendly neighborhood developer
    @ Serverless
    

    Functions deployed

  12. List services:

    sls deploy list

    An example response:

    Serverless: Listing deployments:
    Serverless: -------------
    Serverless: Timestamp: 1531622582547
    Serverless: Datetime: 2018-07-15T02:43:02.547Z
    Serverless: Files:
    Serverless: - aws-nodejs-hello.zip
    Serverless: - compiled-cloudformation-template.json
    

    Show Function Logs

  13. Open up a separate tab in your console and fetch (stream) all logs for your Function:

    sls logs -f hello -t

    Sample response:

    START RequestId: 9cb599bd-87d9-11e8-a1ac-77e94c02a7c3 Version: $LATEST
    END RequestId: 9cb599bd-87d9-11e8-a1ac-77e94c02a7c3
    REPORT RequestId: 9cb599bd-87d9-11e8-a1ac-77e94c02a7c3  Duration: 0.55 ms Billed Duration: 100 ms   Memory Size: 1024 MB  Max Memory Used: 21 MB  
    

    Invoke function

  14. Invoke:

    sls invoke -f hello --log

    Response:

    {
     "statusCode": 200,
     "body": "{\"message\":\"Go Serverless v1.0! Your function executed successfully!\",\"input\":{}}"
    }
    --------------------------------------------------------------------
    START RequestId: 5df3ecdc-87d9-11e8-8ed7-c3ee16fc727b Version: $LATEST
    END RequestId: 5df3ecdc-87d9-11e8-8ed7-c3ee16fc727b
    REPORT RequestId: 5df3ecdc-87d9-11e8-8ed7-c3ee16fc727b  Duration: 2.16 ms Billed Duration: 100 ms   Memory Size: 1024 MB  Max Memory Used: 20 MB  
    

    PROTIP: Pay attention to “Max Memory Used:”.

    Remove all functions

  15. Remove all Functions, Events and Resources of the service, so charges are no longer accrued:

    sls remove

    BLAH: Example response:

    Serverless: Getting all objects in S3 bucket...
    Serverless: Removing objects in S3 bucket...
    Serverless: Removing Stack...
    Serverless: Checking Stack removal progress...
    .......
    Serverless: Stack removal finished...
    Serverless: Successfully archived your service on the Serverless Platform
    

Implement a sample project

Choose one pre-defined:

  1. Create a folder.

  2. Load a sample project from the list above:

    sls 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)
    
  3. Press Enter to accept the default

  4. 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!!!!

...

Structure of Folders and Files

PROTIP: 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

Local Runs for testing

For 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, which makes it extensible.

Several plugins come with the Framework.

Plugins can be written in python, node.js, java, scala or C#.

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:

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

Get Permissions

aws-lambda-node-js-programming

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

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

Resources

Rock Stars

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:

CNCF

  • white paper PDF in https://github.com/cncf/wg-serverless from the Kubernetes folks

Pluralsight video tutorials

More on Clouds

This is one of a series on Cloud computing