Slide 1

Slide 1 text

AWS Lambda Crash Course J. Randall Hunt @jrhunt

Slide 2

Slide 2 text

Astronomy Picture Of The Day

Slide 3

Slide 3 text

Agenda • What Is Lambda? • Lambda Console • Lambda CLI • APOD Function • Tips and Tricks

Slide 4

Slide 4 text

What Is AWS Lambda? • A zero-administration compute platform • “Just the code” without needing to define the underlying compute resources or OS • Asynchronous functions • Event driven from other services • …Or triggered externally and even chained

Slide 5

Slide 5 text

Just One Knob 128 MB 1 GB Memory

Slide 6

Slide 6 text

Just Two Policies • Invocation Role: really only needs lambda's invoke-async permissions • Execution Role: depends on the function

Slide 7

Slide 7 text

Pricing • You have a discrete task that uses a finite # of resouces: RAM + Time. • Workers pick up and complete tasks. • First 1 million requests are free • $0.20 per 1 million requests thereafter • 400,000 GB-Seconds of free compute time per month • $0.00001667 per GB-second after

Slide 8

Slide 8 text

Limits • Invocation Limits: • 1024 processes or threads • 1024 file descriptors • Asynchronous (not good for when you need immediate response)

Slide 9

Slide 9 text

APOD Twitter • User signs up to have their twitter background/ image changed to the astronomy picture of the day. • We gather their OAuth token and store in a database • We loop through the tokens with workers and post the updates

Slide 10

Slide 10 text

Traditional Infrastructure • New picture uploaded to S3 • Create SQS queue • Create EC2 Workers • Write fault tolerant logic to connect all of it.

Slide 11

Slide 11 text

AWS Lambda Infrastructure • Write a lambda function that responds to an S3 upload • Write a lambda function that responds to a new user being added in DynamoDB

Slide 12

Slide 12 text

Code Walkthrough https://github.com/ranman/apod_twitter

Slide 13

Slide 13 text

Tips and Tricks • Node.js is asynchronous: http://callbackhell.com • Use a library like async or nesting to make sure your context.done() calls don't cause things to finish too early. • External modules need to be compiled with the same version of libc that's on the Lambda boxes (use Amazon Linux AMI and you should be fine) • console.log() is great for debugging but finding CloudWatch logs is a pain

Slide 14

Slide 14 text

Tips and Tricks • For sharing functions you need 3 things: • package.json listing modules needed • event.json listing sample event • execution policy you expect

Slide 15

Slide 15 text

CLI Tricks • zip tmp.zip -r index.js node_modules

Slide 16

Slide 16 text

CLI Tricks aws lambda upload-function \ --function-name twitterUpdate \ --function-zip tmp.zip \ --runtime "nodejs" \ --role "arn:aws:iam:::role/lambda_exec_role" \ --handler "index.handler" \ --mode "event" \ --timeout 30

Slide 17

Slide 17 text

FAQ • More languages coming? • More invocation possibilities on the way? • More event sources on the way? • Limits?