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.4k stars 372 forks source link

Migrate sdk v2 to sdk v3 #225

Closed mettke closed 8 months ago

mettke commented 9 months ago

This is a follow up PR of #222 and migrates aws sdk v2 to aws sdk v3. It also contains the commit from #222 and should only be merged afterwards.

alexcasalboni commented 8 months ago

I have rebased and pushed a couple of minor test improvements (to keep 100% coverage), then figured out that tests included some pretty big assumptions on how the SDK handles errors. Now I'm using the right error class instead of checking the error type/message.

Thank you again for this amazing contribution 🙏 🚀

mettke commented 8 months ago

Yeah. In fact it is quite a shame that it is not that easy to actually mock aws calls. I mean sure you have that mocking lib, but it does not give you an idea of how errors actually look like and what to expect. One is kinda left in the dark. But your changes look good. Happy to help.

alexcasalboni commented 8 months ago

Indeed, that's why I started working on integration tests.

Even if the mock library isn't perfect, it does make sense to mock API calls for unit tests (and keep those tests/assumptions aligned to reality) and then automatically run integration tests to catch unintended assumptions or regressions.

Also, I noticed we might have used a different (less optimized) approach with something like this:

const {Lambda} = require("@aws-sdk/client-lambda")
// this is an aggregated client that includes/imports all commands
const lambdaClient = new Lambda()

and then use it like this:

module.exports.createLambdaAlias = (lambdaARN, alias, version) => {
    const params = {
        FunctionName: lambdaARN,
        FunctionVersion: version,
        Name: alias,
    };
    return lambdaClient.createAlias(params));
};

which has an almost identical syntaxt as SDK v2 (just without the .promise():

return lambdaClient.createAlias(params).promise();

The main benefit would have been fewer code changes and no command classes to import. The obvious disadvantage is that you don't benefit from the SDKv3 modularity & performance.

I'm totally ok with the more optimized approach we have now 👌

mettke commented 8 months ago

Interesting. I did not see that this is also possible. To be honest, I did not work with v3 too much, I basically used the documentation to migrate one to the next and it shows you the other way to use it. Even though I'm not sure how I feel about having Lambda reference the Class [1] and an Interface [2] 😅. Who uses the same name for two different structures...

[1] https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-lambda/Class/Lambda/ [2] https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-lambda/Interface/Lambda/