Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Serverless in production, an experience report (CoDe-Conf)

640 views

Published on

AWS Lambda has changed the way we deploy and run software, but this new serverless paradigm has created new challenges to old problems - how do you test a cloud-hosted function locally? How do you monitor them? What about logging and config management? And how do we start migrating from existing architectures?

In this talk Yan and Scott will discuss solutions to these challenges by drawing from real-world experience running Lambda in production and migrating from an existing monolithic architecture.

Published in: Technology
  • Be the first to comment

Serverless in production, an experience report (CoDe-Conf)

  1. 1. in production an experience reportan experience report what you should know before you go to production ServerlessServerless
  2. 2. Yan Cui http://theburningmonk.com @theburningmonk Principal Engineer @
  3. 3. “Netflix for sports” offices in London, Leeds, Katowice and Amsterdam
  4. 4. available in Austria, Switzerland, Germany, Japan and Canada, Italy and USA
  5. 5. available on 30+ platforms
  6. 6. ~1,000,000 concurrent viewers
  7. 7. “Netflix for sports” offices in London, Leeds, Katowice and Amsterdam We’re hiring! Visit engineering.dazn.com to learn more. follow @dazneng for updates about the engineering team. WE’RE HIRING!
  8. 8. apr, 2016
  9. 9. hey guys, vote on this post and I’ll announce a winner at 10PM tonight
  10. 10. 10PM traffic
  11. 11. 10PM traffic 70-100x
  12. 12. low utilisation to leave room for spikes EC2 scaling is slow, so scale earlier
  13. 13. lots of $$$ for unused resources
  14. 14. up to 30 mins for deployment deployment required downtime
  15. 15. - Dan North “lead time to someone saying thank you is the only reputation metric that matters.”
  16. 16. “what would good look like for us?”
  17. 17. be small be fast have zero downtime have no lock-step DEPLOYMENTS SHOULD...
  18. 18. FEATURES SHOULD... be deployable independently be loosely-coupled
  19. 19. WE WANT TO... minimise cost for unused resources minimise ops effort reduce tech mess deliver visible improvements faster
  20. 20. nov, 2016
  21. 21. 170 Lambda functions in prod 1.2 GB deployment packages in prod 95% cost saving vs EC2 15x no. of prod releases per month
  22. 22. time is a good fit
  23. 23. 1st function in prod! time is a good fit
  24. 24. ? time is a good fit 1st function in prod!
  25. 25. ALERTING CI / CD TESTING LOGGING MONITORING
  26. 26. Practices ToolsPrinciples what is good? how to make it good? with what?
  27. 27. Principles outlast Tools
  28. 28. 170 functions ? ? time is a good fit 1st function in prod!
  29. 29. SECURITY DISTRIBUTED TRACING CONFIG MANAGEMENT
  30. 30. evolving the PLATFORM
  31. 31. rebuilt search
  32. 32. Legacy Monolith Amazon Kinesis Amazon Lambda Amazon CloudSearch
  33. 33. Legacy Monolith Amazon Kinesis Amazon Lambda Amazon CloudSearchAmazon API Gateway Amazon Lambda
  34. 34. new analytics pipeline
  35. 35. Legacy Monolith Amazon Kinesis Amazon Lambda Google BigQuery
  36. 36. Legacy Monolith Amazon Kinesis Amazon Lambda Google BigQuery 1 developer, 2 days design production (his 1st serverless project)
  37. 37. Legacy Monolith Amazon Kinesis Amazon Lambda Google BigQuery “nothing ever got done this fast at Skype!” - Chris Twamley
  38. 38. - Dan North “lead time to someone saying thank you is the only reputation metric that matters.”
  39. 39. Rebuilt with Lambda
  40. 40. getting PRODUCTION READY
  41. 41. choose a tried-and-tested deployment framework, don’t invent your own
  42. 42. http://serverless.com
  43. 43. https://github.com/awslabs/serverless-application-model
  44. 44. http://apex.run
  45. 45. https://apex.github.io/up
  46. 46. https://github.com/claudiajs/claudia
  47. 47. https://github.com/Miserlou/Zappa
  48. 48. http://gosparta.io/
  49. 49. TESTING
  50. 50. amzn.to/29Lxuzu
  51. 51. Level of Testing 1.Unit do our objects do the right thing? are they easy to work with?
  52. 52. Level of Testing 1.Unit 2.Integration does our code work against code we can’t change?
  53. 53. handler
  54. 54. handler test by invoking the handler
  55. 55. Level of Testing 1.Unit 2.Integration 3.Acceptance does the whole system work?
  56. 56. Level of Testing unit integration acceptance feedback confidence
  57. 57. “…We find that tests that mock external libraries often need to be complex to get the code into the right state for the functionality we need to exercise. The mess in such tests is telling us that the design isn’t right but, instead of fixing the problem by improving the code, we have to carry the extra complexity in both code and test…” Don’t Mock Types You Can’t Change
  58. 58. “…The second risk is that we have to be sure that the behaviour we stub or mock matches what the external library will actually do… Even if we get it right once, we have to make sure that the tests remain valid when we upgrade the libraries…” Don’t Mock Types You Can’t Change
  59. 59. Don’t Mock Types You Can’t Change Services
  60. 60. Paul Johnston The serverless approach to testing is different and may actually be easier. http://bit.ly/2t5viwK
  61. 61. LambdaAPI Gateway DynamoDB
  62. 62. LambdaAPI Gateway DynamoDB Unit Tests
  63. 63. LambdaAPI Gateway DynamoDB Unit Tests Mock/Stub
  64. 64. is our request correct? is the request mapping set up correctly?is the API resources configured correctly? are we assuming the correct schema? LambdaAPI Gateway DynamoDB is Lambda proxy configured correctly? is IAM policy set up correctly? is the table created? what unit tests will not tell you…
  65. 65. most Lambda functions are simple have single purpose, the risk of shipping broken software has largely shifted to how they integrate with external services observation
  66. 66. optimize towards shipping working software, even if it means slowing down your feedback loop…
  67. 67. …if a service can’t provide you with a relatively easy way to test the interface in reality, then you should consider using another one. Paul Johnston
  68. 68. “…Wherever possible, an acceptance test should exercise the system end-to- end without directly calling its internal code. An end-to-end test interacts with the system only from the outside: through its interface…” Testing End-to-End
  69. 69. Legacy Monolith Amazon Kinesis Amazon Lambda Amazon CloudSearchAmazon API Gateway Amazon Lambda
  70. 70. Legacy Monolith Amazon Kinesis Amazon Lambda Amazon CloudSearchAmazon API Gateway Amazon Lambda Test Input
  71. 71. Legacy Monolith Amazon Kinesis Amazon Lambda Amazon CloudSearchAmazon API Gateway Amazon Lambda Test Input Validate
  72. 72. integration tests exercise system’s Integration with its external dependencies my code
  73. 73. acceptance tests exercise system End-to-End from the outside my code
  74. 74. integration tests differ from acceptance tests only in HOW the Lambda functions are invoked observation
  75. 75. CI + CD PIPELINE
  76. 76. Jenkins build config deploys and tests unit + integration tests deploy acceptance tests
  77. 77. if [ "$1" = "deploy" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE 'node_modules/.bin/sls' deploy -s $STAGE -r $REGION elif [ "$1" = "int-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run int-$STAGE elif [ "$1" = "acceptance-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run acceptance-$STAGE else usage exit 1 fi
  78. 78. if [ "$1" = "deploy" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE 'node_modules/.bin/sls' deploy -s $STAGE -r $REGION elif [ "$1" = "int-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run int-$STAGE elif [ "$1" = "acceptance-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run acceptance-$STAGE else usage exit 1 fi install Serverless framework as dev dependency
  79. 79. if [ "$1" = "deploy" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE 'node_modules/.bin/sls' deploy -s $STAGE -r $REGION elif [ "$1" = "int-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run int-$STAGE elif [ "$1" = "acceptance-test" ] && [ $# -eq 4 ]; then STAGE=$2 REGION=$3 PROFILE=$4 npm install AWS_PROFILE=$PROFILE npm run acceptance-$STAGE else usage exit 1 fi install Serverless framework as dev dependency mitigate version conflicts
  80. 80. http://alistair.cockburn.us/Hexagonal+architecture
  81. 81. build.sh allows repeatable builds on both local & CI
  82. 82. Auto Auto Manual
  83. 83. LOGGING
  84. 84. 2016-07-12T12:24:37.571Z 994f18f9-482b-11e6-8668-53e4eab441ae GOT is off air, what do I do now?
  85. 85. 2016-07-12T12:24:37.571Z 994f18f9-482b-11e6-8668-53e4eab441ae GOT is off air, what do I do now? UTC Timestamp API Gateway Request Id your log message
  86. 86. Me Logs are not easily searchable in CloudWatch Logs.
  87. 87. CloudWatch Logs
  88. 88. AWS Lambda invokes AWS Lambda stdout asynchronously any log aggregation service CloudWatch Logs
  89. 89. CloudWatch Logs AWS Lambda AWS Lambda stdout any log aggregation service asynchronously invokes
  90. 90.
  91. 91. CloudWatch Events
  92. 92. DISTRIBUTED TRACING
  93. 93. a user my followers didn’t receive my new post!
  94. 94. where could the problem be?
  95. 95. correlation IDs* * eg. request-id, user-id, yubl-id, etc.
  96. 96. wrap HTTP client & AWS SDK clients to forward captured correlation IDs
  97. 97. kinesis client http client sns client
  98. 98. use X-Ray for performance tracing
  99. 99. Amazon X-Ray
  100. 100. Amazon X-Ray
  101. 101. X-Ray traces do not span over async event sources
  102. 102. https://epsagon.com
  103. 103. MONITORING + ALERTING
  104. 104. no place to install agents/daemons
  105. 105. • invocation Count • error Count • latency • throttling • granular to the minute • support custom metrics
  106. 106. • same metrics as CW • better dashboard • support custom metrics https://www.datadoghq.com/blog/monitoring-lambda-functions-datadog/
  107. 107. my code
  108. 108. my code
  109. 109. my code internet internet press button something happens
  110. 110. those extra 10-20ms for sending custom metrics would compound when you have microservices and multiple APIs are called within one slice of user event
  111. 111. Amazon found every 100ms of latency cost them 1% in sales. http://bit.ly/2EXPfbA
  112. 112. no more background processing, other than what the platform provides
  113. 113. console.log(“hydrating yubls from db…”); console.log(“fetching user info from user-api”); console.log(“MONITORING|1489795335|27.4|latency|user-api-latency”); console.log(“MONITORING|1489795335|8|count|yubls-served”); timestamp metric value metric type metric namemetrics logs
  114. 114. CloudWatch Logs AWS Lambda ELK stack logs metrics CloudWatch
  115. 115. don’t forget to setup dashboards & alarms
  116. 116. CONFIG MANAGEMENT
  117. 117. design for easy & quick propagation of config changes
  118. 118. me Environment variables make it hard to share configurations across functions.
  119. 119. me Environment variables make it hard to implement fine-grained access to sensitive info.
  120. 120. config service goes here
  121. 121. SSM Parameter Store
  122. 122. Secrets Manager
  123. 123. sensitive data should be encrypted in-flight, and at-rest
  124. 124. enforce role-based access to sensitive configuration values
  125. 125. SSM Parameter Store HTTPS role-based access encrypted in-flight
  126. 126. SSM Parameter Store encrypt role-based access
  127. 127. SSM Parameter Store encrypted at-rest
  128. 128. HTTPS role-based access SSM Parameter Store encrypted in-flight
  129. 129. invest into a robust client library
  130. 130. fetch & cache at cold-start
  131. 131. https://github.com/middyjs/middy
  132. 132. https://www.cloudzero.com/blog/2018-cloud-and-serverless-ecosystem-survey take the survey and enter to win a $100 Amazon Gift Card understand the activity, cost, performance and relationships that occur in your cloud and serverless environments
  133. 133. @theburningmonk theburningmonk.com github.com/theburningmonk

×