Invisible ubiquitious server clouds across the land
Overview
- Serverless = FaaS
- FaaS Providers - Pricing
- Concerns (the Downsides)
- Serverless usage in the wild
- Services
- Best Practices
- Local Runs for testing
- Serverless Coding Frameworks
- Get Permissions
- Serverless Command-line
- Keeping Secrets
- Sample Hello Project
- Implement a sample project
- Structure of Folders and Files
- Production limits & prep
- Troubleshooting
- Libraries
- Rock Stars
- Salesforce integration
- More on Clouds
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.
A sentiment about physical servers is reflected in the wi-fi password used by
attendees of #ServelessConf:
Serverless = FaaS
“Serverless” refer to an architectural style called FaaS (Function as a Service) where software program code are independently deployed and run on a cloud system for “zero system administration” by the developer.
This is also called an “event-driven system”.
For those who want to run a lean shop, it’s a fast, easy, low-cost route to get apps in production without worry about scalability. Unlike renting a machine (as in EC2) which accrue charges even when idle, charges for Serverless functions accrue only when individual requests are invoked.
The developer simply uploads the source code, then leave it to the cloud provider to take care of security, monitoring, disk space management, log management, scaling, redundancy, backup, crash reporting, etc..
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.”
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.
“Serverless is on the relentless historical trend toward industrialization” – @simonwardley
FaaS Providers - Pricing
Free tiers bring down costs, as shown by Peter Sbarski’s Serverless Calculator at serverlesscalc.com
- AWS (Amazon Web Services) Lambda Nov 2014 supports NodeJs, Python, any run-time
- Microsoft Azure Functions 2016 supports NodeJs, Java, C#, F#, Python
- Google Cloud Functions 2018 supports NodeJs, Python
- Alibaba Function Compute’s calculator returned a price of $330.05. Its language support expanded from NodeJs and Python to Java, PHP, C# (the most among FaaS vendors).
Google App Engine in 2008 was arguably the first.
-
CloudFlare
- Pivotal Cloud Functions built on Knative, part of the project Riff open-source project led by Google for deployment atop Kubernetes and Istio. defining the pfs command line interface
-
Pivotal’s Project Riff based on KNative.
- Oracle’s Fn Project
- Iron.io for on-premises. Ironically, Iron originated the term “serverless” in 2012.
Concerns (the Downsides)
Database idle costs money!
“You never pay for idle” Austen says in an interview with CloudAcademy Introduction to the Serverless Paradigm [23:50]
WRONG! While Lambda does not incur charges while idle, DynamoDB,, which Amazon touts as the default database, 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.
Control
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 usage in the wild
When a $9 million monolithic app built for the Australian Census failed, two students over two-weeks created a scalable system using $500 of compute time (including load testing).
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
http://www.emitconference.com Emit Conference for “event-driven architectures” August 17th, 2017 in San Francisco has people who’ve done it:
- Rob Gruhl Senior Manager of Serverless Platform Team at Nordstrom
- Bobby Calderwood Technology Fellow at Capital One
- Madhuri Yechuri Founder of Elotl
- Dave Copeland Director of Engineering at Stitch Fix
-
Shawn Burke Staff Software Engineer at Uber
- Ajay Nair Head of Product (AWS Lambda) at Amazon Web Services
- Jason Polites Product Manager for Google Cloud Functions
- Chris Anderson Senior Program Manager for Azure Functions
-
Cornelia Davis Senior Director of Technology at Pivotal Software
- Anne Thomas VP & Distinguished Analyst at Gartner, Inc.
- Matthew Lancaster Global Lead, Lightweight Architectures at Accenture
An audio Q&A with The CTO of New York Times, Nick Rockwell, who migrated the paper to using React and GraphQL Apollo that reads off of a Kafka pipeline on Google Cloud and Big Query.
Services
Functionality | Type | AWS service | 3rd-party |
---|---|---|---|
Compute | Functions | AWS Lambda | Iron.io |
Security | Authentication | IAM, Cognito federation | Auth0, AuthRocket, Okta |
Logging, Monitoring, Anomaly Analytics | CloudWatch, CloudTrail, X-Ray tracing | Honeycomb, Datadog | |
Analytics | Visualization | QuickSight | - |
Streaming | streams | Kinesis | - |
Messages | SQS, SNS | Twilio | |
Persistence | Files | S3 | - |
Time Series | Timestream | InfluxDB, etc. | |
NoSQL | DynamoDB | Redis | |
SQL | Aurora | MySQL, PostgeSQL | |
Data warehouse | Redshift columnar Spectrum from S3 | Hadoop, Spark | |
Search | CloudSearch | ElasticSearch | |
Blockchain | QuantumLedger | - | |
Query/Transform | ETL, SQL | Glue, Athena, EMR | - |
Machine Learning | - | SageMaker | Tensorflow |
Image recognition | - | Rekognition | Algorithmia, Quandl |
Payment | - | - | Stripe, Paypal |
Geocoding | - | - | HERE, Loqate |
Best Practices
Functions need to be Stateless (not long-running tasks)
Functions that access resources (such as PaaS databases) should be in a private cloud, which AWS calls VPC (Virtual Private Cloud).
Functions that access resources over the public internet should run the function in a subnet with a NAT’d route to the internet.
NodeJs start up faster than Java.
There are 3 execution models:
- Synchronous (push event-driven pipelines) via API Gateway
- Asynchronous (event requests) via SNS, S3
- Stream-based changes via DynamoDB (NoSQL) to Kinesis
CloudWatch warming triggers every 5 minutes to keep code in memory to reduce warm-up time. This is to avoid Freeze/thaw errors.
- Limit data transformations to one per Lambda function.
-
Upgrade to latest NodeJs regularly
- Console
- Function Code
- Execution Roles
- Test Events
- Execution Results
- Monitoring analytics
AWS Lambda
Internally, AWS Lambda functions are run using AWS Firecracker open-source software managing lightweight VMs, to reduce startup time and memory overhead.
AWS Lex bot Lambda Blueprint provides a pre-configured patterns for building conversational interfaces : OrderFlowers, ScheduleAppointment (in Python), BookTrip (in NodeJs).
Reusable Layers in libraries (like Docker) reduce dependency size.
Step functions build visual workflows using State Types in state stores.
Search/browse generic pre-defined apps from the AWS Serverless Application Repository (SAR). Configure one and deploy it.
SAM
Although people say Terraform templates are more flexible …
The AWS Serverless Application Model (SAM) at https://github.com/awslabs/serverless-application-model was announced Nov 2016 to define the building blocks of Cloud Formation templates that access Amazon API Gateway APIs, AWS Lambda functions, and Amazon DynamoDB tables needed by serverless applications.
Templates
Two AWS evangelists created pre-built templates with manual instructions, which they walked though in 30 minutes on Twitch.tv:
-
Get an AWS IAM User with AWSCodeStarFullAccess.
CAUTION: Installation steps in Peter Sbarski’s book suggests providing “Administrator access to all services and resources.
- Login the AWS Console to select region N. Virginia.
- In AWS CodeStar. Start a project. Node.js. create role.
- 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)”.
More examples are at https://github.com/serverless/examples
-
Route 53 : a highly available & scalable DNS service which also manages traffic flow based on different routeing types e.g., Latency Based Routing, Geo DNS, and Weighted Round Robin as well as DNS failover.
-
CloudFront CDN: Content Distribution Network.
-
S3 : a static file storage which can store petabytes of storage if you want, with 99.999999% durability.
-
API Gateway : a REST-based service which allows you to create, publish, monitor and quickly scale & secure API services.
-
Lambda : to hold and process server side code, with no charges for server time between client requests.
-
MongoDB instead of DyanmoDB : a scalable database which guarantees the same consistent speed of read and write requests.
-
SES (Simple Email Service) : Mass emailing service, like MailChimp.
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.
PROTIP: The payoff for housing the back-end in a public cloud is for machine learning and AI services to be added. For example: https://github.com/aws-samples/aws-serverless-ember
Azure Functions
Marketing information is at https://azure.microsoft.com/services/functions
- Create an Azure Storage account.
- Search for “Function App” in the Azure Marketplace.
- Click Create.
-
Provide an app name that’s globally unique among all others public.
.azurewebsites.net
- Create or use a Resource Group to manage your similar functions as a group.
-
Select a Location closest to you.
PROTIP: Include the location as a suffix in your app name.
- Selection of Memory Allocation can be adjusted once you figure out how much after running a few times.
- Create.
-
Click Refresh or the “bell” icon to see messages related.
- After deployment, click the Resource Group, then the app name.
- Click “Webhood + API” and JavaScript language (or C#).
- Copy the Function URL and paste it in a browser.
- Click Logs to see the history tracking.
- 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).
Google Cloud Functions
https://cloud.google.com/functions
@googlecloud
Google Firebase
Iron.io and other on-premises
Iron.io has their Gesalt Framework
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:
-
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
-
Describe tests in a JSON file.
Serverless Coding Frameworks
- Shep (bustle.com)
- ClaudiaJs
- Apex provides a shim to support Go and languages not yet supported by Lambda,
-
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
- Google Cloud Functions for Firebase
- The Serverless Framework
Apex
http://apex.run/
Open sourced on GitHub/apex/apex
See https://github.com/apex/apex/blob/master/docs/infra.md
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
ClaudiaJs
https://www.manning.com/books/serverless-applications-with-nodejs
Serverless Applications with Node.js
Using AWS Lambda and Claudia.js
by Slobodan Stojanović and Aleksandar Simović
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:
- serverless-framework at https://github.com/serverless/serverless
- http://docs.serverless.com
- Roadmap at https://github.com/serverless/serverless/milestones includes runs in Microsoft Azure and IBM.
- serverless.com is the company’s home page.
- gitter.im/serverless/serverless for instant chatting
- serverlessconf.io conferences
- @GoServerless Twitter account
- YouTube channel
- Meetups on meetup.com and at ServerlessMeetups.com
- https://www.emitconference.com Emit Conference for “event-driven architectures” August in San Francisco
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:
-
Install Node.js as a pre-requisite since the framework is written in Node.js.
-
Install the serverless-framework
npm install -g serverless
-
Verify version:
serverless -v sls -v
1.42.3 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
-
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 install ....................... Install a Serverless service from GitHub or a plugin from the Serverless registry package ....................... Packages a 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 invoke ........................ Invoke a deployed function invoke local .................. Invoke function locally info .......................... Display information about the service logs .......................... Output the logs of a deployed function metrics ....................... Show metrics for a specific function 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 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 Plugins AwsConfigCredentials, Config, Create, Deploy, Emit, Info, Install, Invoke, Login, Logout, Logs, Metrics, Package, Plugin, PluginInstall, PluginList, PluginSearch, PluginUninstall, Print, Remove, Rollback, Run, SlStats
-
Verify where the executable is located:
command -v serverless
The response (with your account rather than “wilsonmar”):
/Users/wilsonmar/.nvm/versions/node/v9.11.1/bin/serverless
-
Get the location of files
find / -name serverless 2>/dev/null
The response (with your account rather than “wilsonmar”):
/usr/local/bin/serverless
In the serverless framework folder
-
Navigate to framework folders and files:
cd /usr/local/lib/node_modules/serverless ls -al
The response (with your account rather than “wilsonmar”):
CHANGELOG.md README.md lib package-lock.json scripts LICENSE.txt bin node_modules package.json
-
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
_meta Plugins
The heart of Serverless are its Plugins, which makes it extensible.
Plugins can be written in python, node.js, java, scala or C#.
Plugins need to be installed for each project that uses each.
-
List plugins come installed with the Framework :
ls _meta/
- Navigate your active directory to the root of your project.
-
Plugins are downloaded from GitHub by npm, so to install a plug-in, for example:
npm install --save-dev serverless-webpack-plugin webpack
See asprouse/serverless-webpack-plugin - Use Webpack to optimize your Serverless Node.js Functions.
In serverless.yml, this section:
plugins: - custom-serverless-plugin custom: custom-config-category: configBucket: configBucketName
-
serverless/serverless-meta-sync - Securely sync your the variables in your project’s
_meta/variables
across your team.npm install serverless-offline –save
-
dherault/serverless-offline - Emulate AWS Lambda and Api Gateway locally to speed up your development cycles.
-
kennu/serverless-plugin-hookscripts - Easily create shell script hooks that are run whenever Serverless actions are executed.
-
joostfarla/serverless-cors-plugin - Adds support for CORS (Cross-origin resource sharing).
-
Nopik/serverless-serve - Simulate API Gateway locally, so all function calls can be run via localhost.
-
serverless/serverless-client-s3 - Deploy and config a web client for your Serverless project to S3.
-
martinlindenberg/serverless-plugin-alerting - This Plugin adds Cloudwatch Alarms with SNS notifications for your Lambda functions.
-
serverless/serverless-optimizer-plugin - Optimizes your code for performance in Lambda. Supports coffeeify, babelify and other transforms
-
tmilewski/serverless-resources-validation-plugin - Adds support for validating your CloudFormation template.
-
Nopik/serverless-lambda-prune-plugin - Delete old versions of AWS lambdas from your account so that you don’t exceed the code storage limit.
-
daffinity/serverless-base-path-plugin - Sets a base path for all API Gateway endpoints in a Component.
-
arabold/serverless-test-plugin - A Simple Integration Test Framework for Serverless.
-
martinlindenberg/serverless-plugin-sns - This plugin easily subscribes your lambda functions to SNS notifications.
-
joostfarla/serverless-jshint-plugin - Detect errors and potential problems in your Lambda functions.
-
nishantjain91/serverless-eslint-plugin - Detect errors and potential problems in your Lambda functions using eslint.
-
SC5/serverless-mocha-plugin - Enable test driven development by creating test cases when creating new functions
-
HyperBrain/serverless-package-plugin - Package your lambdas without deploying to AWS.
-
arabold/serverless-sentry-plugin - Automatically send errors and exceptions to Sentry.
-
arabold/serverless-autoprune-plugin - Delete old AWS Lambda versions.
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
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
-
Create a folder to hold serverless projects. For example, you may choose another:
mkdir ~/gits/sls cd ~/gits/sls
Serverless Platform Online
-
Open the cloud dashboard:
serverless login
This opens a browser to:
-
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.
-
-
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
- Edit the name and click SAVE.
-
Return to the Terminal to see:
Serverless: You are now logged in
New local
-
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
--template
fServerless: 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
-
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.
-
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 stage: prod memorySize: 256 environment: ACCESS_KEY_VALUE: 123-access-key-value-456-abc 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.
Path
When using the AWS API Gateway,
path: hello/{id}
Events
Another http example:
method: post cors: true
-
The other lines beginning with # are comments that can be deleted.
Deploy function
-
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: <a target="_blank" href="https://github.com/serverless/examples"Serverless examples repository</a> 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
-
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
-
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
-
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
-
Remove all Functions, Events and Resources of the service, so charges are no longer accrued:
sls remove
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
Richard Moot (@wootmoot), Developer Evangelist @Square, wrote Super Simple Serverless eCommerce. Richard stores files in Gists, which will work as long as the files don’t ever change.
- In the Amazon console, use your AWS admin account within IAM, create an account with permissions to read and write S3.
- In S3, create a bucket to get a UNIQUE_ID.
- Create a product image and put in within your S3 bucket.
-
Edit the sample product.html file
- Replace UNIQUE_ID with the ID of the app in AWS.
- Replace “/0.jpeg” with the URI to the product image at: <img class=’productImage’ src=”/0.jpeg” />
- Replace “Square 1 Sticker” with your own product’s name.
My changes are stored in GitHub.
- Edit the sample index.htm
- Save a favicon.ico graphic file in the root.
- Save the main.css and normalize.css, plugin.js, main.js
- Save sample handler.js
- enable “Static website hosting” in Properties to have a publicly accessible site.
-
Edit the sample serverless.yml
- Replace YOUR_LOCATION_ID in serverless.yml
Richard also created Serverless Instant Checkout Links with Square
Alternately, choose another pre-defined app from this list:
-
https://zanon.io/posts/building-serverless-websites-on-aws-tutorial using the Serverless Framework 1.1, dated JAN 31, 2016 by Fred Eady.
-
serverless/serverless-graphql - Official Serverless boilerplate to kick start your project
-
serverless/serverless-starter - A simple boilerplate for new projects with a few architectural options
-
serverless/serverless-graphql-blog - A blog boilerplate that leverages GraphQL in front of DynamoDB to offer a minimal REST API featuring only 1 endpoint
-
laardee/serverless-authentication-boilerplate - A generic authentication boilerplate for Serverless framework
-
sc5/sc5-serverless-boilerplate - A boilerplate for test driven development of REST endpoints
-
[microapps/MoonMail] (https://github.com/microapps/MoonMail) - Build your own email marketing infrastructure using Lambda + SES from http://microapps.com/
-
Create a folder.
-
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)
-
Press Enter to accept the default
-
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)...
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
Production limits & prep
BLAH: The 64-character name limit.
By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 100. This is a safety limit to protect users from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, request a limit increase for concurrent executions here.
BLAH: Can’t search for logs across regions.
Troubleshooting
lumigo.io offers their lumigo-cli to troubleshoot Serverless apps.
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
Rock Stars
Yan Cui (@@theburningmonktheburningmonk.com from London) goes deeper than anyone else on Serverless.
-
Production-Ready Serverless: Operational Best Practices 9h LiveVideo course thru Manning. Subjects include:
- API Gateway, VPC
- Testing, Debugging, CI/CD, Canary Deployments
- Process real-time events with Kinesis & Lambda
- Logging, Monitoring, X-Ray, Correlation IDs, Performance, Error Handling
-
Lambda limits, Managing Configurations,
- I paid the $400 to enroll in his live on-line class</a> that began May 1, 2020.
Adnan Rahić (@adnanrahic, bookvar.co founder) $50 Packt Serverless JavaScript by Example [Video] December, 2017
Phillip Muens (@pmmuens, github.com/pmuens) from Germany
-
Has an informative blog at JustServerless.com
- https://github.com/pmuens/serverless-book
-
http://justserverless.com/blog/your-first-serverless-application/
-
$28.13 Learn Serverless ebook (5.7 MB in 5 PDFs), learnserverless.club, with the sample app at github.com/JustServerless/notes.
- Deprecated since Oct 2016 is https://github.com/JustServerless/learnserverless-book/issues
Matthew Fuller
- AWS Lambda: A Guide to Serverless Microservices Amazon Kindle book published 2016-01-11): $3.99
Jake Knowles
- AWS Lambda: Serverless Microservices Guide with Simple Instructions
John McKim @johncmckim blogs on Medium:
CNCF
- white paper PDF in repo https://github.com/cncf/wg-serverless from the Kubernetes folks
Andru Estes
- Creating a Simple AWS Lambda Function [30m] Jul 22, 2018
Building Serverless Web Applications with React & AWS Amplify video course by Nader Dabit.
Develop a Serverless Backend using Node.js on AWS Lambda by Nik Graf
Pluralsight video tutorials
-
Serverless Web Applications 9 Dec 2015 [2h 40m] by Rob Conery
-
Serverless Computing: The Big Picture 17 Apr 2019 [59m] by Richard Seroter (VP of Product Marketing at Pivotal)
Azure:
-
Building Serverless Applications in Azure 15 Aug 2017, 4h 7m by Mark Heath
Instead of giving you an emulator for local development, Azure give you the runtime. The whole box. The Azure Functions Core Tools is the same runtime that Azure uses in the cloud. Because you get the whole runtime, you can build any kind of function locally. Blob and Azure Message Queue Triggers as well. Not just HTTP triggers.
Open an Azure Functions project in VS Code, and you are prompted to setup an extension to run and debug functions, put a breakpoint in the gutter and hit the green button in the debug panel.
Google:
-
Architecting Serverless Big Data Solutions Using Google Dataflow
-
Architecting Event-driven Serverless Solutions Using Google Cloud Functions
Random Resources
Serverless Instant Checkout Links with Square – Square Corner Blog
A crash course on Serverless APIs with Express and MongoDB
how-to-write-your-first-aws-lambda-function
Taking Serverless and AI to the Next Level February 27, 2018 by Yaron Haviv, Tomer Rosenthal
Building Serverless Application Pipelines March 6, 2018 https://www.cncf.io/wp-content/uploads/2018/03/cncf-serverless-webinar.pdf by Sebastien Goasguen
Lists:
https://github.com/anaibol/awesome-serverless
https://techbeacon.com/50-best-starter-kits-resources-building-serverless-apps
Salesforce integration
https://apex.run
AWS IDE
Zip fileslocally and upload to Lambda
More on Clouds
This is one of a series on Cloud computing
- Dockerize apps
- Kubernetes container engine
- Hashicorp Vault and Consul for keeping secrets
- Hashicorp Terraform
- Ansible server configuration automation
- Serverless software app development
- Terraform (declarative IaC)
- Build load-balanced servers in AWS EC2
- AWS On-boarding (CLI install)
- AWS MacOS instances in the cloud)
- AWS Certifications
- AWS IAM admin.
- AWS Data Tools
- AWS Security
- AWS VPC Networking
- AWS X-Ray tracing
- AWS server deployment options
- AWS Lambda
- AWS Cloud Formation/cloud-formation/)
- AWS Lightsail
- AWS Deeplens AI
- AWS Load Balanced Servers using CloudFormation
-
Microtrader (sample microservices CI/CD to production Docker within AWS)
-
AWS Data Processing: Databases, Big Data, Data Warehouse, Data Lakehouse
- Google Cloud Platform
-
Bash Windows using Microsoft’s WSL (Windows Subsystem for Linux)
- Azure cloud introduction
- Azure Cloud Onramp (Subscriptions, Portal GUI, CLI)
- Azure Cloud Powershell
- PowerShell GitHub API programming
- PowerShell DSC (Desired State Configuration)
- PowerShell Modules
- Microsoft AI in Azure cloud
- Azure cloud DevOps
- Azure Networking
- Azure Storage
- Azure Compute
- Dynatrace cloud monitoring
- Digital Ocean
- Cloud Foundry