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
b
The examples you show always have an event trigger like an object being created in S3 or DynamoDB
What if I just want to create a Lambda function URL?
How do you write that in TypeScript using Pulumi?
f
If I'm understanding your question correctly, you can make use of "magic functions" without associating them with an event via an `aws.lambda.CallbackFunction`:
the
fn
resource there will get created as a Lambda function
b
I see
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
But I cannot find anything related to how to change the node runtime version
f
oh, yeah, apologies - the example there provides/adds
timeout
which is one of the many optional properties you can specify. you're looking for
runtime
, so... something like:
👍 1