Data-driven & cost-aware performance tuning for AWS Lambda

Data-driven & cost-aware performance tuning for AWS Lambda

How can we combine cost optimization and performance tuning into a single data-driven process, and even integrate it into our CI/CD pipelines? This talk will showcase an open-source tool to optimize Lambda functions resource allocation without requiring complex refactors or architectural changes, while showcasing charts and cost/performance patterns of real-world use cases.

36ca8ae7e19067021d0e39c0b72acc2a?s=128

Alex Casalboni

February 21, 2020
Tweet

Transcript

  1. 1.

    Alex Casalboni Sr. Developer Advocate, AWS @alex_casalboni Data-driven & cost-aware

    performance tuning for AWS Lambda @ 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved
  2. 2.

    About me • Software Engineer & Web Developer • Data

    science background • Worked in a US startup for 4.5 years • ServerlessDays Milan co-organizer • AWS customer since 2013
  3. 5.
  4. 6.

    AWS operational responsibility models On-premises Cloud Less More Compute Virtual

    machine Amazon EC2 AWS Elastic Beanstalk AWS Lambda AWS Fargate Databases MySQL MySQL on EC2 Amazon RDS MySQL Amazon Aurora Aurora Serverless Amazon QLDB/DynamoDB Storage Storage Amazon S3 Messaging ESBs Amazon MQ Amazon Kinesis Amazon EventBridge/SNS/SQS Analytics Hadoop Hadoop on EC2 Amazon EMR Amazon Elasticsearch Service Amazon Athena
  5. 7.

    Anatomy of a Lambda function Handler() function Function to be

    executed upon invocation Event object Data sent during Lambda function Invocation Context object Methods available to interact with runtime information (request ID, log group, more) import json def lambda_handler(event, context): # TODO implement return { 'statusCode': 200, 'body': json.dumps('Hello World!') }
  6. 8.

    The request lifecycle Bootstrap the runtime Start your code Cold

    start Warm start Download your code Start new micro vm AWS optimization Your optimization
  7. 12.

    Avoid «monolithic» functions Reduce deployment package size Micro/Nano services Optimize

    dependencies (and imports) E.g. up to 120ms faster with Node.js SDK Optimization best practices (cold start) Minify/uglify production code Browserify/Webpack Lazy initialization of shared libs/objs Helps if multiple functions per file
  8. 13.

    Provisioned Concurrency for AWS Lambda New! Managed solution to avoid

    “cold starts” You set provisioned concurrency for individual functions No code changes required Integrated with AWS Auto Scaling Provisioning rampup (500/min) Wide support (CloudFormation, Terraform, Serverless Framework, Datadog, Epsagon, Lumigo, Thundra, etc.)
  9. 14.

    Externalize orchestration Avoid idle/sleep – delegate to Step Functions Transform,

    not Transport Minimize data transfer (S3 Select, advanced filtering, etc.) Discard uninteresting events asap Trigger config (S3 prefix, SNS filter, etc.) Fine-tune resources allocation Don’t guesstimate function memory Lambda Destinations Simplified chaining (async) and DLQ Keep in mind retry policies Very powerful (and configurable), but not free Optimization best practices (performance/cost)
  10. 15.

    Externalize orchestration Avoid idle/sleep – delegate to Step Functions Transform,

    not Transport Minimize data transfer (S3 Select, advanced filtering, etc.) Discard uninteresting events asap Trigger config (S3 prefix, SNS filter, etc.) Fine-tune resources allocation Don’t guesstimate function memory Lambda Destinations Simplified chaining (async) and DLQ Keep in mind retry policies Very powerful (and configurable), but not free Optimization best practices (performance/cost)
  11. 17.

    CPU-bound example “Compute 1,000 times all prime numbers <= 1M”

    128 MB 11.722 sec $0.024628 256 MB 6.678 sec $0.028035 512 MB 3.194 sec $0.026830 1024 MB 1.465 sec $0.024638
  12. 18.

    CPU-bound example “Compute 1,000 times all prime numbers <= 1M”

    128 MB 11.722 sec $0.024628 256 MB 6.678 sec $0.028035 512 MB 3.194 sec $0.026830 1024 MB 1.465 sec $0.024638
  13. 20.

    Cost-aware performance optimization 310ms 400ms 294ms 300ms 5% performance optimization

    25% cost optimization 480ms 500ms 408ms 500ms 15% performance optimization 0% cost optimization A B
  14. 22.

    Data-driven cost & performance optimization for AWS Lambda Available as

    a SAR app Easy to integrate with CI/CD Meet AWS Lambda Power Tuning github.com/alexcasalboni/aws-lambda-power-tuning
  15. 37.
  16. 38.

    Takeaways Optimization best practices (what are you trying to optimize?)

    Avoid cold starts with Provisioned Concurrency Memory Power Optimal resources allocation can be automated (CI/CD) Think in terms of workload categories and cost/performance patterns Visualize optimal trade-offs with /alexcasalboni/aws-lambda-power-tuning
  17. 40.

    Alex Casalboni Sr. Developer Advocate, AWS @alex_casalboni @ 2019, Amazon

    Web Services, Inc. or its Affiliates. All rights reserved Thank you!