Transcript
Page 1: Node withoutservers aws-lambda

Node Without Servers: Event-Driven Computing with AWS LambdaBrian Klaas Johns Hopkins Bloomberg School of Public Health

[email protected] @brian_klaas www.iterateme.com

Page 2: Node withoutservers aws-lambda

Events

Page 3: Node withoutservers aws-lambda
Page 4: Node withoutservers aws-lambda
Page 5: Node withoutservers aws-lambda
Page 6: Node withoutservers aws-lambda
Page 7: Node withoutservers aws-lambda
Page 8: Node withoutservers aws-lambda
Page 9: Node withoutservers aws-lambda
Page 10: Node withoutservers aws-lambda
Page 11: Node withoutservers aws-lambda

What is and how does it work?

What good can do for me?

Page 12: Node withoutservers aws-lambda

+ =

Page 13: Node withoutservers aws-lambda

Code Memory setting Timeout setting

Page 14: Node withoutservers aws-lambda

Event

Page 15: Node withoutservers aws-lambda

Full AWS Linux AMI (ami-dfc39aef, Linux Kernel 3.14.35-28.38.amzn1.x86_64)

Node v0.10.33 ImageMagick AWS JS SDK v2.1.22

Up to 1024MB RAM and 512MB of ephemeral disk storage

Page 16: Node withoutservers aws-lambda

*

* May not actually be Docker

Page 17: Node withoutservers aws-lambda

Every event Every config change Every code change

= new ( )

Page 18: Node withoutservers aws-lambda
Page 19: Node withoutservers aws-lambda

Lambda = one function

Page 20: Node withoutservers aws-lambda

exports.handler = function(event, context) {console.log('Hello', event);// more JS goes herecontext.done(null, 'Success');

}

Page 21: Node withoutservers aws-lambda

Initialization code

Handler code

Code terminatesTimeoutcontext.done context.success context.failAll callbacks finished

Event

Context

?

Page 22: Node withoutservers aws-lambda

Never assume Lambda will re–use a container.

Page 23: Node withoutservers aws-lambda

Pricing: Charged by Compute TimeFree TierFirst million requests are free

400,000 GB-seconds of compute time

220 hours of free compute time per month @ 512MB of RAM

Paid Tier$0.20 per 1 million requests thereafter

$0.00001667 for every GB-second

http://aws.amazon.com/lambda/pricing

* You also have to pay for in/out data transfer fees

Page 24: Node withoutservers aws-lambda

Event–Driven Computing

Page 25: Node withoutservers aws-lambda

Event notification Run a compute cycle Shut down

Page 26: Node withoutservers aws-lambda

Where do events come from?

Page 27: Node withoutservers aws-lambda

DynamoDB

S3 AWS SDK

Kinesis

Cognito

SNS

Custom EventsCloudFormation

Page 28: Node withoutservers aws-lambda

S3Fast, durable, cheap storage Events on file put/post, copy

Page 29: Node withoutservers aws-lambda

Many AWS services can post to SNS

GitHub has a post-commit hookSNS

Page 30: Node withoutservers aws-lambda

All SDKs support Lambda function invocation

Java, Ruby, Node, Python, JavaScript, PHP, .NET, iOS, Android, and the CLI

AWS SDK

Custom Events

Page 31: Node withoutservers aws-lambda

Lambda in Action

Page 32: Node withoutservers aws-lambda

1Template Example

Page 33: Node withoutservers aws-lambda

IAM Identity Access Management

Page 34: Node withoutservers aws-lambda

IAMUsers Groups Roles

AccessKey + SecretKey

Page 35: Node withoutservers aws-lambda

IAM

Page 36: Node withoutservers aws-lambda

IAM

Roles = JSON{ "Version":"2008-10-17", "Id":"http referrer policy example", "Statement":[ { "Sid":"Allow get requests referred by www.mysite.com and mysite.com", "Effect":"Allow", "Principal":"*", "Action":"s3:GetObject", "Resource":"arn:aws:s3:::example-bucket/*", "Condition":{ "StringLike":{ "aws:Referer":[ "http://www.mysite.com/*", "http://mysite.com/*" ] } } } ] }

Page 37: Node withoutservers aws-lambda

How do we know it’s done?

Page 38: Node withoutservers aws-lambda

When responding to events from within AWS, we don’t.

Page 39: Node withoutservers aws-lambda

4Notifying that It’s Done

Page 40: Node withoutservers aws-lambda

Options for notifying that work is completeSQS SNS (http, SMS, email) DynamoDB table File updating in S3

Page 41: Node withoutservers aws-lambda

Only one Lambda function per S3 bucket.

Page 42: Node withoutservers aws-lambda

5Custom Events

Page 43: Node withoutservers aws-lambda

AWS SDK

Custom Events

AWS CLI

Page 44: Node withoutservers aws-lambda

Event body [Event context]

Page 45: Node withoutservers aws-lambda

Custom events are synchronous calls

Default invocation-type: RequestResponse invocation-type: Event for async events

Page 46: Node withoutservers aws-lambda

Invoking via the CLI using ColdFusion

Page 47: Node withoutservers aws-lambda

Invoking via the CLI aws lambda invoke --function-name myFunctionName --payload '{"key1":"value1", "key2":"value2", "key3":"value3"}' outfile.txt

Name of your Lambda function

Response is written to a file. This is the name of that file.

JSON passed in to the Lambda function as the “event” structure.

Note you can specify --payload file://input.txt to use a file instead.

Page 48: Node withoutservers aws-lambda

On–demand Node functionality

Page 49: Node withoutservers aws-lambda

What Good is Lambda Anyway?

Page 50: Node withoutservers aws-lambda

Asynchronous task server Background job processor

Page 51: Node withoutservers aws-lambda

S3 Event Handler

Page 52: Node withoutservers aws-lambda

Media conversion serviceImageMagick Native modules AWS Elastic Transcoder

Page 53: Node withoutservers aws-lambda

Real–time analytics aggregator

Page 54: Node withoutservers aws-lambda

Microservice backend?

Page 55: Node withoutservers aws-lambda

Lambda is great when every event is independent and can be processed incrementally.

Page 56: Node withoutservers aws-lambda

Beyond JS/Node

Page 57: Node withoutservers aws-lambda

Statically compile on an Amazon Linux AMI Install the module as part of your Node app on the same AMI ZIP up the function, binary, and the node_modules folder

Native Node Modules

https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

Page 58: Node withoutservers aws-lambda

Make sure it’s compiled for the Linux AMD64 architecture Make sure it can run standalone or is visible to /bin/bash/ or /usr/bin/python

Use child_process.spawnSync() to make sure the child process finishes

before context.success/done() executes

Include the executable in the ZIP file you upload to Lambda

Any Compiled Executable

https://aws.amazon.com/blogs/compute/running-executables-in-aws-lambda/

Page 59: Node withoutservers aws-lambda

Native Java support announced April 9

Java

Page 60: Node withoutservers aws-lambda

Go Do!

Page 61: Node withoutservers aws-lambda

Code at github.com/brianklaas

Session evaluation!Brian Klaas Johns Hopkins Bloomberg School of Public Health

[email protected] @brian_klaas www.iterateme.com


Top Related