How do you author a TypeScript Lambda function? Th...
# aws
b
How do you author a TypeScript Lambda function? There's no convenient esbuild integration that automatically transpiles the code for you.
f
one option here is you can forego esbuild with pulumi's "magic" functions (https://www.pulumi.com/docs/clouds/aws/guides/lambda/#using-magic-lambda-functions)
b
I'm puzzled. Where's the Lambda function there?
f
the arrow function (whose body in that example consists of the comment
// Your Lambda code here.
) becomes an AWS Lambda function
slightly more useful example:
Copy code
docsBucket.onObjectCreated("docsHandler", (event: aws.s3.BucketEvent) => {
    if (event && event.Records) {
        for (const record of event.Records) {
            // do something with the s3 bucketEvent record
        }
    }
});
b
I probably didn't explain well, my bad
I was asking for the equivalent of NodejsFunction, since you cannot run TS on Lambda, you need to transpile it to JS.
Or you're telling me that the code block you wrote will automatically create a Lambd afunction with Nodejs 20 runtime and transpile whatever you write in the handler?
f
Yep! It will do all that for you. Keep in mind the project will need to import any AWS packages the lambda will use. e.g. expanding on the example above
Copy code
// also a dependency in your pulumi project's package.json file
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";

// ... more code ...

docsBucket.onObjectCreated("docsHandler", (event: aws.s3.BucketEvent) => {
    // TODO: do something with dynamodb in this lambda
    const client = new DynamoDBClient({});

    if (event && event.Records) {
        for (const record of event.Records) {
            // do something with the s3 bucketEvent record
        }
    }
});
if you need nodejs 20 (I think the default is 16) there's code there under
Customizing the Lambda
to allow you to config the generated lambda