Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS Lambda Crash Course

79c320d5af1feee21c58b8cb21692d38?s=47 Randall Hunt
December 05, 2014

AWS Lambda Crash Course

A quick intro to AWS Lambda and how to write and play with functions.

79c320d5af1feee21c58b8cb21692d38?s=128

Randall Hunt

December 05, 2014
Tweet

Transcript

  1. AWS Lambda Crash Course J. Randall Hunt @jrhunt

  2. Astronomy Picture Of The Day

  3. Agenda • What Is Lambda? • Lambda Console • Lambda

    CLI • APOD Function • Tips and Tricks
  4. 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
  5. Just One Knob 128 MB 1 GB Memory

  6. Just Two Policies • Invocation Role: really only needs lambda's

    invoke-async permissions • Execution Role: depends on the function
  7. 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
  8. Limits • Invocation Limits: • 1024 processes or threads •

    1024 file descriptors • Asynchronous (not good for when you need immediate response)
  9. 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
  10. Traditional Infrastructure • New picture uploaded to S3 • Create

    SQS queue • Create EC2 Workers • Write fault tolerant logic to connect all of it.
  11. 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
  12. Code Walkthrough https://github.com/ranman/apod_twitter

  13. 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
  14. Tips and Tricks • For sharing functions you need 3

    things: • package.json listing modules needed • event.json listing sample event • execution policy you expect
  15. CLI Tricks • zip tmp.zip -r index.js node_modules

  16. 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
  17. FAQ • More languages coming? • More invocation possibilities on

    the way? • More event sources on the way? • Limits?