10 tips for serverless backends with nodejs and aws lambda

25
10 Tips For Serverless Backends With NodeJS and AWS Lambda By Jim Lynch Nov 16, 2016

Upload: jim-lynch

Post on 16-Apr-2017

304 views

Category:

Software


0 download

TRANSCRIPT

Page 1: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

10 Tips For Serverless Backends

With NodeJS and AWS Lambda

By Jim Lynch Nov 16, 2016

Page 2: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

About MeSDET at HBO

@webWhizJim

WebStorm Ambassador

Lambda Consultant

github.com/JimTheMan/Lambda-Talk

slides: tinyurl.com/jxzzpml

Page 3: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Where Can I Run Serverless Backend Code?

Microsoft Azure (Offers C#, PHP, and BASH in addition to NodeJS, Python, and Java)

AWS Lambda (NodeJS, Python, Java)

Google Cloud Functions (NodeJS) Note: Currently in closed Alpha)

Page 4: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

It’s Way Cheaper Than Traditional Servers!

Especially high cost savings if switching to serverless from underutilized servers.

Excellent for lean startup operations, large corporations looking to optimize, and everyone in between!

Billed for usage in increments of 100ms.

Lambda costs: $0.0000002 per request and $0.00001667 per GB-second used.*

* https://aws.amazon.com/lambda/pricing/

Page 5: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

It’s Way Easier Than Traditional Servers!

You don’t have to worry about choosing an instance size, scaling up, clustering, etc.

No need to fix crashes, apply patches, update OS’s, or all other painful server maintenance tasks!

No need to spin up any servers!

You don’t need server security experts make sure your servers are safe and locked down.

Page 6: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 7: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

The 10 Tips…

Page 8: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 9: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 1Start With Hello World

Test it by calling to it with the aws-sdk.

Sign up for an AWS account.

Create a Simple Lambda function in the AWS console.

Test it right in the AWS console.

Page 10: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 2Pass Data To and From Your Lambda Functions

The event object holds data passed into the lambda function as either an object or array.

Lambda’s callback function takes two arguments: an error object and a success object.

Page 11: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 3 Set Up REST Endpoints For Your Lambda Functions

The AWS service “API Gateway” lets exposing your Lambda functions via HTTPS.

You can call your Lambda function via ordinary POST request and pass data as query string parameters.

Can automatically create endpoints in API Gateway from Swagger definitions (and also export API’s as Swagger definitions or Swagger+Postman Extensions).

Page 12: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 13: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 4Secure Your Lambda Functions With CORS and Keys!

CORS allows you to tell AWS to run the Lambda function only for requests that originate from a specified domain or domains.

Returns generic “permission denied” error to all other domains.

Key to preventing abuse of your publicc endpoint or “DDOS attacks” on your Lambda functions.

Easily add “API Key Authorization” in API Gateway.

Page 14: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 15: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 5: Send Notification Emails With SES

Send yourself or others emails with AWS SES (Simple Email Service)

Register emails by entering them into IAM and clicking the link in a verification email.

You can also easily hook into other services like Twilio to blast out emails to unverified addresses.

Page 16: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 17: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 6: Send Text Messages With SNS

Incoming SNS texts can also be a trigger that kick off Lambda functions.

Easily to create groups for subscriptions, applications, or topics.

Use SNS to send programmatically send texts from your Lambda functions!

Page 18: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 19: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 7: Schedule Lambda Functions to Run With Cron Expressions

Set up Lambda functions to be run at some specific point in the future.Use the trigger “Cloudwatch Events - Schedule”

In Cloudwatch you can make a rule with a schedule defines as a cron expression

Page 20: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 21: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 8 Use Cloudwatch To Read Your Logs

Any console.log statements in your Lambda functions are saved in the logs under the AWS service “Cloudwatch”.

The complete history is saved from every time the function is ever run.

Saved other data about that execution as well eg. data & time, milliseconds billed, etc.

Page 22: 10 Tips For Serverless Backends With NodeJS and AWS Lambda
Page 23: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 9 Add Any NodeJS Library!

You don’t need to include the aws-sdk node library.

Install node packages as usual into a node_modules folder. Then upload that along with the function as a zip.

Although it’s very easy to tie into other AWS services, you are perfectly free to use Lambda with any other outside serviced or NodeJS library.

Page 24: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Tip 10 Create Lambda Functions That Call to IoT Devices!

Lambda has built in triggers to allows IoT devices to kick off functions.

Create scheduled tasks that push messages out to IoT devices (lock doors after 9pm, start a coffee week days at 8am, etc).

What you can do is only limited by your imagination!

Some examples to get your started: https://github.com/awslabs/aws-iot-examples

Page 25: 10 Tips For Serverless Backends With NodeJS and AWS Lambda

Thanks!twitter.com/webWhizJim

github.com/JimTheMan/Lambda-Talk

slides: tinyurl.com/jxzzpml

wisdomofjim.com