Serverless - Lessons Learned - Nike Brown Bag

Serverless - Lessons Learned - Nike Brown Bag

Brown Bag @ Nike on 03/2019.
- Serverless Deployment Framework (Serverless, Terraform, AWS SAM)
- Deployment Best Practises
- Serverless Performance (Coldstart, VPC, Runtimes, GraalVM)

4f58cc9aea7be8afbeede693832984c8?s=128

Driss Amri

March 28, 2019
Tweet

Transcript

  1. Serverless on AWS LESSONS LEARNED Driss Amri @drams88

  2. Who is Driss Amri? Blogger at drissamri.be Traveler, Diver, Developer

  3. Agenda • Deployment - Frameworks - Best practises • Performance

    - Cold start - VPC - Runtimes
  4. “Everything we hear is an opinion, not a fact. Everything

    we see is a perspective, not the truth.” - Marcus Aurelius, Mediations
  5. Deployment toolkits • Terraform • Serverless Framework • AWS Serverless

    Application Model (SAM)
  6. Serverless Framework • Most widely used Serverless toolkit • Released

    in 2015 • Simple Developer Experience • Focus on Functions, not Cloud Infrastructure • Cloud-agnostic - AWS provider: - Compiles to CloudFormation - deployed as CloudFormation Stack
  7. Serverless Framework

  8. Serverless Framework CLI serverless create --template aws-java-gradle --name sls-app serverless

    deploy --region eu-west-1 serverless remove
  9. None
  10. None
  11. AWS SAM • Official AWS deployment framework • Released in

    2016 • Built on top of CloudFormation - Deployed as CloudFormation Stack
  12. AWS SAM

  13. AWS SAM CLI sam init --runtime java8 sam package \

    --output-template-file packaged.yaml \ --s3-bucket bucketname sam deploy \ --template-file packaged.yaml \ --stack-name sam-app \ --capabilities CAPABILITY_IAM \ --region eu-west-1 aws cloudformation delete-stack --stack-name sam-app
  14. AWS Serverless Application Model (SAM)

  15. Deployment Frameworks AWS SAM Terraform + Easy to use +

    Official AWS Support + SAM Local Docker - Missing basic features - AWS only + Great for infrastructure + Cloud agnostic - Verbose - No extra Serverless support - Steap learning curve Serverless Framework + Easy to use + Cloud agnostic + Biggest community + Easy local testing* - Limited local testing for Java
  16. Deployment best practises • Two phase deployment & destroy -

    Stateful resources (DynamoDB, RDS, SQS, …) - Stateless resources (Lambda, Gateway, Cloudwatch, ...) • Safe deployments - Canary - Linear - AllAtOnce
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. Performance

  24. Latency UX • Synchronous requests - User waiting: < 1

    sec - AWS & Google: 100-500 ms delays has huge impact • Asynchronous requests - Depends on requirements - Batch reports < 30 sec - Any request > 5 sec should provide feedback
  25. None
  26. When do coldstarts happen? • First invocation instance • Concurrent

    invocations • After provider resource clean up • After deployment & configuration change
  27. The Burning Monk - I’m afraid you’re thinking about AWS

    Lambda cold starts all wrong
  28. The Burning Monk - I’m afraid you’re thinking about AWS

    Lambda cold starts all wrong
  29. The Burning Monk - I’m afraid you’re thinking about AWS

    Lambda cold starts all wrong
  30. The Burning Monk - I’m afraid you’re thinking about AWS

    Lambda cold starts all wrong
  31. None
  32. None
  33. None
  34. None
  35. - https://youtu.be/QdzV04T_kec - https://www.slideshare.net/AmazonWebServices/a-serverless-journey-aws-lambda-un der-the-hood-srv409r1-aws-reinvent-2018 AWS re:Invent 2018

  36. When to use a VPC? • Access to Amazon RDS,

    Elasticache, RedShift, … • Private API Gateway • On-premise services over VPN/Direct Connect • ...
  37. None
  38. None
  39. Runtime Coldstart Performance

  40. None
  41. The Burning Monk - AWS Lambda Compare Coldstart

  42. Minimize bootstrap • Keep function single purposed • Minimize dependencies

    & use lightweight frameworks - serverless-optimizer-plugin, webpack,… • Prefer low overhead runtime (node.js, Python, Go) • Choose the best memory size - 2x Memory = > 2x CPU = Possibly Faster & Cheaper
  43. AWS Lambda Power Tuning AWS Lambda Power Tuning

  44. Real world example

  45. Runtime: Java Global app init: Static + Constructor Java runtime

    initialization: ~200 ms Function execution • Java 8 + Gradle • DynamoDB AWS SDK 2 + AWS HTTP Client + Dagger 2 Latency: ~3.9s drissamri/aws-lambda-runtime-coldstart - java
  46. Runtime: node.js • Node.js v8 + TypeScript + Webpack •

    DynamoDB Client drissamri/aws-lambda-runtime-coldstart - nodejs Node.js runtime initialization: ~200 ms Global app init Function execution Latency: ~560ms
  47. None
  48. AWS Lambda Custom Runtime • Implement AWS Lambda in ANY

    language • Runtime triggers the Lambda function handler • Can be included in the Function deployment - Executable file: bootstrap
  49. None
  50. GraalVM

  51. First.. What is Graal?

  52. a dynamic just-in-time compiler that improves efficiency and speed

  53. A (JIT) compiler for Java transforms bytecode to machine code

  54. None
  55. -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI -XX:+UseJVMCICompiler

  56. What about GraalVM?

  57. None
  58. None
  59. GraalVM • Create native images: self contained executable • Lightning

    fast startup • Low memory consumption • Polyglot programming
  60. GraalVM

  61. GraalVM Limitations

  62. Cloud-Native Frameworks

  63. Quarkus

  64. GraalVM Lambda • Possible with AWS Lambda Custom Runtime •

    Framework support: - Micronaut: workable - Quarkus: experimental - Spring Fu: experimental
  65. GraalVM Lambda

  66. Conclusion • Deployment ✅ Serverless Framework or AWS SAM ✅

    Safe deployments with CodeDeploy ✅ Separate stateful and stateless parts of build • Performance ✅ Critical latency: nodejs ✅ Keep an eye on GraalVM
  67. Learn...

  68. Get started! Amazon Web Services — a practical guide https://github.com/open-guides/og-aws

    AWS Certification preparation https://bit.ly/2aroFYb Serverless framework https://serverless.com AWS Well-Architected https://aws.amazon.com/architecture/well-architected The Burning Monk - Yan Cui https://theburningmonk.com/
  69. Links AWS re:Invent 2017: Become a Serverless Black Belt https://www.youtube.com/watch?v=oQFORsso2go

    A Serverless Journey: Under the Hood of AWS Lambda https://youtu.be/QdzV04T_kec