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

Implementing a custom aws lambda runtime using Crystal

Implementing a custom aws lambda runtime using Crystal

This presentation gives an introduction into the concept of function as a service. After a quick introduction into crystal it is shown how easy it is to write a custom AWS lambda runtime using the crystal programming language.

This presentation was held at the serverless munich meetup in early 2019.

Alexander Reelsen

January 22, 2019
Tweet

More Decks by Alexander Reelsen

Other Decks in Programming

Transcript

  1. Custom AWS lambda runtimes
    ...and a crystal demo
    Alexander Reelsen
    @spinscale
    [email protected]

    View Slide

  2. There is no serverless architecture.
    It's just someone else's execution
    environment.
    Alex R.
    THE TRUTH[TM]

    View Slide

  3. WHO DAT?
    About me
    ▸ Working distributed at Elastic since 2013
    ▸ Elasticsearch developer
    ▸ Dislikes running own infrastructure
    ▸ Basketball & JVM fan, husband, dad

    View Slide

  4. AND NOW FOR A LESSON IN...
    Agenda
    ▸ Custom AWS Lambda runtimes
    ▸ Introduction into crystal lang
    ▸ A crystal based runtime

    View Slide

  5. AWS Lambda

    View Slide

  6. FAAS

    View Slide

  7. node.js

    View Slide

  8. python

    View Slide

  9. go

    View Slide

  10. java

    View Slide

  11. JVM

    View Slide

  12. startup time

    View Slide

  13. dependencies

    View Slide

  14. JIT

    View Slide

  15. 2015: custom runtime?

    View Slide

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

    View Slide

  17. 2018: custom runtime!

    View Slide

  18. all the languages!

    View Slide

  19. runtime flow

    View Slide

  20. CUSTOM
    RUNTIME

    View Slide

  21. CUSTOM
    RUNTIME
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  22. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  23. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    GET /2018-06-01/runtime/invocation/next
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  24. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    GET /2018-06-01/runtime/invocation/next
    Lambda-Runtime-Aws-Request-Id: 123
    {
    "body": "{ \"foo\": \"bar\" }",
    "requestContext": "{ ... }",
    "headers" : { ... }
    }
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  25. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    POST /2018-06-01/runtime/invocation/123/response
    {
    "body": "{ \"foo\": \"bar\" }",
    "headers" : { ... }
    }
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  26. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    POST /2018-06-01/runtime/invocation/123/response
    {
    "body": "{ \"foo\": \"bar\" }",
    "headers" : { ... }
    }
    HTTP OK 202
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  27. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    POST /2018-06-01/runtime/invocation/123/error
    {
    "statusCode": 500,
    "body" : "..."
    }
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  28. AWS
    ENDPOINT
    CUSTOM
    RUNTIME
    POST /2018-06-01/runtime/init/error
    {
    "errorMessage" : "...",
    "errorType" : "..."
    }
    AWS_LAMBDA_RUNTIME_API=localhost:12345
    _HANDLER="my_handler"
    /bin/bootstrap

    View Slide

  29. crystal

    View Slide

  30. readable

    View Slide

  31. fast

    View Slide

  32. statically typed

    View Slide

  33. batteries included

    View Slide

  34. View Slide

  35. testing

    View Slide

  36. benchmarks

    View Slide

  37. metaprogramming

    View Slide

  38. fibers

    View Slide

  39. channels

    View Slide

  40. boehm gc

    View Slide

  41. c bindings

    View Slide

  42. dependency mgmt

    View Slide

  43. web frameworks

    View Slide

  44. binaries

    View Slide

  45. static binaries

    View Slide

  46. View Slide

  47. View Slide

  48. View Slide

  49. demo

    View Slide

  50. summary

    View Slide

  51. easy

    View Slide

  52. $$$

    View Slide

  53. static binaries

    View Slide

  54. aws library

    View Slide

  55. examples

    View Slide

  56. static sites, dynamic

    View Slide

  57. slack hook

    View Slide

  58. github hook

    View Slide

  59. location tracking

    View Slide

  60. View Slide

  61. scheduled jobs

    View Slide

  62. emails

    View Slide

  63. reverse geocoder

    View Slide

  64. thank you

    View Slide

  65. HYPER HYPER
    Links
    ▸ https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html
    ▸ https://docs.aws.amazon.com/lambda/latest/dg/runtimes-walkthrough.html
    ▸ https://crystal-lang.org
    ▸ https://github.com/spinscale/crystal-aws-lambda
    ▸ AWS Lambda Under the Hood: https://www.youtube.com/watch?v=QdzV04T_kec
    ▸ Firecracker: https://firecracker-microvm.github.io/
    ▸ Boehm GC: http://www.hboehm.info/gc/
    ▸ FaastRuby: https://faastruby.io/getting-started-crystal/
    ▸ Google Cloud Functions: https://github.com/sam0x17/gcf.cr
    ▸ Crystal on OpenFaas: https://github.com/TPei/crystal_openfaas

    View Slide

  66. View Slide

  67. @spinscale
    [email protected]

    View Slide

  68. questions?

    View Slide