Giter Club home page Giter Club logo

aws-lambda-power-tuning's Introduction

AWS Lambda Power Tuning - made with serverless

Step Functions state machine generator for AWS Lambda Power Tuning.

The state machine is designed to be quick and language agnostic. You can provide any Lambda Function as input and the state machine will estimate the best power configuration to minimize cost. Your Lambda Function will be executed in your AWS account (i.e. real HTTP calls, SDK calls, cold starts, etc.) and you can enable parallel execution to generate results in just a few seconds.

How to deploy the state machine

First, install the Serverless Framework and configure your AWS credentials:

$ npm install serverless -g
$ serverless config credentials --provider aws --key XXX --secret YYY

Now, you can quickly install this service as follows:

$ serverless install -u https://github.com/alexcasalboni/aws-lambda-power-tuning

The Serverless Framework will download and unzip the repository, but it won't install dependencies. Don't forget to install npm dependencies before generating the state machine:

$ cd aws-lambda-power-tuning
$ npm install

Then you can generate the dynamic state machine by providing your AWS Account ID. Optionally, you can specify the AWS region and a comma-separated list of RAM values:

$ npm run generate -- -A ACCOUNT_ID [-R eu-west-1] [-P 128,256,512,1024]

Finally, you can deploy everything:

$ serverless deploy

How to execute the state machine

Once the state machine and all the Lambda Functions have been deployed, you will need to execute the state machine and provide an input object.

You will find the new state machine here. Enter the state machine named LambdaPowerStateMachine and click "New execution". Here you can provide the execution input and an execution id (see section below for the full documentation):

{
    "lambdaARN": "your-lambda-function-arn",
    "num": 10
}

As soon as you click "Start Execution", you'll be able to follow the execution flow on the state machine chart. Here is a sample screenshot:

state-machine

Once the execution has completed, you will find the execution results in the "Output" tab of the "Execution Details" section. The output will contain the optimal RAM configuration and its corresponding average cost per execution.

State Machine Input

The AWS Step Functions state machine accepts the following parameters:

  • lambdaARN (required, string): ARN of the Lambda Function you want to optimize
  • num (required, integer): the # of invocations for each power configuration (recommended: between 10 and 100)
  • payload (string or object): the static payload that will be used for every invocation
  • parallelInvocation (false by default): if true, all the invocations will be executed in parallel (note: depending on the value of num, you may experience throttling when setting parallelInvocation to true)

State Machine Output

The AWS Step Functions state machine will return the following outputs:

  • power: the optimal power configuration
  • cost: the corresponding average cost (per invocation)
  • duration: the corresponding average duration (per invocation)

State Machine Internals

The AWS Step Functions state machine is composed by four Lambda Functions:

  • initializer: create N versions and aliases corresponding to the power values provided as input (e.g. 128MB, 256MB, etc.)
  • executor: execute the given Lambda Function num times, extract execution time from logs, and compute average cost per invocation
  • cleaner: delete all the previously generated aliases and versions
  • finalizer: compute the optimal power value (current logic: lowest average cost per invocation)

Initializer, cleaner and finalizer are executed only once, while the executor is used by N parallel branches of the state machine (one for each configured power value). By default, the executor will execute the given Lambda Function num consecutive times, but you can enable parallel invocation by setting parallelInvocation to true. Please note that the total invocation time should stay below 300 seconds (5 min), which means that the average duration of your functions should stay below 3 seconds with num=100, 30 seconds with num=10, and so on.

Contributing

Contributors and PRs are always welcome!

Tests and coverage

Install dev dependencies with npm install --dev. Then run tests with npm test, or coverage with npm run coverage.

Current test coverage: 95%.

aws-lambda-power-tuning's People

Contributors

alexcasalboni avatar hscheib avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.