alexcasalboni / aws-lambda-power-tuning

AWS Lambda Power Tuning is an open-source tool that can help you visualize and fine-tune the memory/power configuration of Lambda functions. It runs in your own AWS account - powered by AWS Step Functions - and it supports three optimization strategies: cost, speed, and balanced.
Apache License 2.0
5.29k stars 363 forks source link

Does this tool support Lambda ARNs that contain a version number / alias #166

Closed uglow closed 2 years ago

uglow commented 2 years ago

Use Case: Tuning two versions of a Lambda.

When I specified the lambdaARN as `"arn:aws:lambda:ap-southeast-2:1234567890:my-function:25", the CleanupOnError step contained this error:

 {
    "Error": "Error",
    "Cause": "{\"errorType\":\"Error\",\"errorMessage\":\"Invalid ARN: arn:aws:lambda:ap-southeast-2:1234567890:function:my-function:25\",\"trace\":[\"Error: Invalid ARN: arn:aws:lambda:ap-southeast-2:1234567890:my-function:25\",\"    at Object.module.exports.regionFromARN (/var/task/utils.js:505:15)\",\"    at Object.module.exports.lambdaClientFromARN (/var/task/utils.js:511:25)\",\"    at Object.module.exports.getLambdaPower (/var/task/utils.js:123:26)\",\"    at Runtime.module.exports.handler (/var/task/initializer.js:17:38)\",\"    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)\"]}"
  }

Is it possible to specify particular versions of a Lambda function in the lambdaARN?

alexcasalboni commented 2 years ago

Hi @uglow, thanks for asking :)

This is not supported because of how the tool works internally.

Let me explain more in detail. The overall logic looks like this (pseudo-code):

Start with valid ARN ($LATEST)

--- Initializer step
Set $LATEST power to 128MB
Publish new function version X
Create new function alias "RAM128"

Set $LATEST power to 256MB
Publish new function version X+1
Create new function alias "RAM256"

... do the same for all the configured power values ...

--- Executor step
Invoke aliases [RAM128, RAM256, ...] in parallel, multiple times

Analyze invocation logs and identify optimal values

--- Cleaner step
Delete all those aliases and versions

But why does it work like this? Because using versions and aliases lets us run the whole process in parallel. Otherwise, longer running functions would take minutes/hours to power-tune.

For example, imagine a function that runs for 60 seconds on average, you want to test 5 power values with num=50. With parallelInvocation=false, the power-tuning process would take over 4 hours (60*50*5 seconds) since every single invocation runs in series. Even with parallelInvocation=true it would still take 5 minutes (60*5 seconds). If you run everything in parallel using versions/aliases, it will take just 60 seconds in total :)

If you look at the Lambda PublishVersion API (or CLI command), you can only publish a new version starting from $LATEST. In other words, you can't say "Please publish a new version X, starting from this specific version or alias". You can only modify $LATEST and publish new versions from there.

I hope this is useful to understand some of the internals and why the tool is implemented this way. I think it makes sense to say that the ultra-fast power-tuning advantage outweighs the need for power-tuning a specific version or alias.

But maybe I'm missing something. Could you share more about what you're trying to achieve by power-tuning a specific version that is different from $LATEST?

uglow commented 2 years ago

Thanks for the detailed explanation! I think parallel executions are more important.

The use case I had in mind was to compare the performance of an old Lambda (by specifying the version) against the latest one.