DEV Community

Cover image for First Look at Lambda Powertools TypeScript

First Look at Lambda Powertools TypeScript

Matt Morgan on January 10, 2022

AWS Senior Solutions Architect Sara Gerion announced on January 5, 2022 that Lambda Powertools TypeScript had entered public beta. Lambda Powertool...
Collapse
 
loujaybee profile image
Lou (🚀 Open Up The Cloud ☁️)

Really nice write-up, Matt!

Collapse
 
aditmodi profile image
Adit Modi

Hi @elthrasher,
Nice article, I was looking forward to exploring the lambda powertools and This article is a good starting point.

lambda powertools is great at providing best practices around observability and will be helpful for developers. Really helpful stuff.

Collapse
 
ijemmy profile image
ijemmy

Hi Matt,
I work on this project. Thanks a lot for reviewing the tool :) This is very valuable for us. We have been pushing ourselves to publish beta version to get a feedback like this so we could reiterate on the right path.

Totally agree on the point that decorator doesn't work on functions. Making functions into a class just to use the decorator feature really bugs me. This led to Middy support as an alternative. We (maintainers) are also concerned if it's commonly used enough. So we ended up supporting all 3 options.

Collapse
 
elthrasher profile image
Matt Morgan

Hey ijemmy, thanks for reading! I think you all made the right choice in supporting all three methods. I've been a middy user for years and I like what it provides, but have always disliked the API (while understanding why they made it that way). I'll probably stick with decorators and see how that goes, but you'll need the other methods anyway to support users who don't want to use transpilers.

Anyway, enjoy the positive feedback! Your team did a great job on this in spite of limitations in the language!

Collapse
 
gerardolima profile image
Gerardo Lima

Classes without state, decorators, ... it's feels like a great option for folks who miss java in early 2000s

Collapse
 
elthrasher profile image
Matt Morgan • Edited

I don't miss the build times.

build times

Collapse
 
gerardolima profile image
Gerardo Lima

hey, @matt, maybe my point wasn't clear; what I meant is that I don't miss using stateless classes as namespaces for static methods and neither do I miss introducing magic on the codebase with anotations; I see these as bad practices on TypeScript codebases and I prefer to avoid them as much as I can and keeping the codebase clean and readable.

Thread Thread
 
elthrasher profile image
Matt Morgan

I can appreciate that perspective. I like using decorators (and I liked annotations in Java) because they reduce boilerplate code. There are definitely pros and cons and one thing to consider is the developers and teams that will need to interact with the code.

I've used decorators in express apps for years and I'm a big fan of putting something like @Authorized on a route instead of if(!sessionNotValid(req)) throw Error(401) or something like that. Remains to be seen whether I'll like using decorators in Lambda as much, but I can think of a lot more boilerplate that could be rolled up....

Thread Thread
 
gerardolima profile image
Gerardo Lima

In Node applications, I don't use decorators and neither do I use if ... statements to check for authorization inside handlers -- that's what's the middlewares are for...

Nevertheless, at the end of the day, it's a matter of preference. Just keep in mind that support for decorators in typescript is not stable and you might have your build pipeline constrained to the tools that support it and you may encounter some issues when upgrading tsc compiler, for example.

Collapse
 
drupsys profile image
DR • Edited

Well... this solution was designed by someone who has never heard a word "Patterns", just spray syntactic sugar everywhere indiscriminately to solve all your problem.

Could have just used a decorator pattern, would have been backwards compatible with JavaScript, work with any kind of function, need no classes and actually be more compact than the code above...

Collapse
 
drupsys profile image
DR • Edited

sorry, I'm a bit annoyed in case you can't tell, but seriously we couldn't just do something like this?

import { decorate, Tracer } from '@aws-lambda-powertools/tracer';
import type { Context } from 'aws-lambda';
import type { Payment } from '../models/payment';

const {
    captureLambdaHandler,
    superDuperSpecialStuff,
} = Tracer({ serviceName: 'paymentCollections' }); // This can't be a class anymore, because javascript...

const handler = async (
    input: { Payload: { Payment: Payment } },
    _context: Context,
): Promise<{ Status: number; Payment: Payment }> => {
    const min = 0;
    const max = 1;
    const Status = Math.floor(Math.random() * (max - min + 1)) + min;

    return { Status, Payment: input.Payload.Payment };
}

export default decorate(
    handler,
    captureLambdaHandler,
    superDuperSpecialStuff,
    // ...
);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
elthrasher profile image
Matt Morgan

What you want is supported using middy. You can use class decorators, middy-style function "decorators", or inline imperative statements. Check the docs and you'll see each example has a "Middy Middleware" example.

Collapse
 
michaelbrewer profile image
Michael Brewer

At least one of the DX improvements have been made :)

twitter.com/dreamorosi/status/1486...