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.27k stars 362 forks source link

Support for array payload accepting functions #226

Closed mstastny36 closed 3 months ago

mstastny36 commented 5 months ago

Hello,

I might have a naive question. I have function that accepts an array of events - the one that is used for AppSync batching resolver.

No matter what am I doing, I cannot make the power tuning tool work with that type of function. I have been searching internet, going through the documentation and did a lot of trial and error. It either sends single object to the lambda, making lambda fail, or the tool itself throws an exception about weighted payload.

{
  "cause": {
    "errorType": "Error",
    "errorMessage": "Invalid weighted payload structure",
    "trace": [
      "Error: Invalid weighted payload structure",
      "    at Object.module.exports.generatePayloads (/var/task/utils.js:388:19)",
      "    at Runtime.module.exports.handler (/var/task/executor.js:49:28)",
      "    at processTicksAndRejections (node:internal/process/task_queues:96:5)"
    ]
  },
  "error": "Error"
}

Does this tool support such lambdas? And if so, how can I properly pass an array of events in the payload without them being weighted?

alexcasalboni commented 5 months ago

Hi @mstastny36, thanks for reaching out :)

Unfortunately, this is meant to be a feature, but it turns out to be a bug in your case.

The tool assumes that payloads are always either a string or an object. If you use a list, it assumes you want to use a special weighted payload structure (see code here and documentation here).

I think we'll need to address this (not so uncommon) case and introduce an explicit useWeightedPayload parameter to avoid confusion.

For the time being, I would recommend the following workaround:

  1. change your code to accept an object such as {"records": [...]}
  2. power-tune the function by passing an object-shaped payload

In the meantime, we'll update the tool to support list-shaped payloads.

mstastny36 commented 5 months ago

Alright, the workaround sounds very well. Thanks a lot for that idea.

It was clear from the documentation that array is always treated as weighted payload. However, it wasn't that clear how to proceed for my use case.

Can I somehow help getting that flag to the repo ? The idea of the new flag seems great, at first I was thinking maybe detecting whether the array is weighted or not based on the weight property, but your proposal might be more error proof for people.

alexcasalboni commented 5 months ago

Yeah, my only concern is that it would be a backward-incompatible change, in case someone is passing an array-shaped weighted payload without any additional flag. But it shouldn't be such a big deal as long as we document it well.

Very happy if you'd like to submit a PR, otherwise I'll work on it later this month myself (or someone else here at AWS).

alexcasalboni commented 3 months ago

@mstastny36 the fix is now merged 👌 🎉

It doesn't require any additional flag or breaking change, so it should just work out of the box with your function now 🚀

I'm closing this issue for now, please feel free to reopen or add comments if you have questions/doubts.