Webpack 5 Builds for AWS Lambda Functions with TypeScript
By: Dennis O'Keeffe
Tags: Typescript, JavaScript, Webpack, Lambda, AWS

Webpack 5 Builds for AWS Lambda Functions with TypeScript

In a previous post, I wrote about self-destructing tweets which runs as an AWS Lambda function every night at midnight.

While that post was about the code itself, most of the AWS CDK infrastructure information had been written in a previous post about sending a serverless Slack message which demonstrated how to run an AWS Lambda on a cron timer.

Today’s post will be a short overview that bridges these together: it shows how I bundled the TypeScript code from the Twitter post with node modules and prepare it for deployment.

The Folder Structure

I am making assumptions here. The most “complex” set up I normally have for Lambdas is to write them in TypeScript and use Babel for transpilation.

Given this will be a familiar standing for most, let’s work with that.

Here is how most of my lambdas following this structure will look from within the function folder:

https://gist.github.com/okeeffed/9b1e7edc86caff76179d434850f063c0.js

You might also note I have both an index.ts and index.local.ts file. index.ts in my project is generally the entry point for the lambda, where the index.local.ts file is normally just used for local development where I swap out my lambda handler for code that lets me run.

Both generally import the main function from another file (here denoted as function.ts ) and just call it.

Webpack will bundle everything into one file later, so it is fine for me to structure the folder however I see fit.

Also note: as pointed out in the comments by Maximilian, bundling node modules into the output from Webpack is not always agood idea if your npm packages require binaries. The same goes for any builds that require dynamic imports at runtime. Use your judgement on whether or not to bundle your node modules into the Webpack build, but I will be doing another write up on using Lambda layers instead to get around the requirement to build one single output.

Setting Up Your Own Project

Inside of a fresh npm project that houses a TypeScript lambda, we need to add to required Babel and Webpack dependencies:

https://gist.github.com/okeeffed/83313ff75f314653c67760251571320d.js

Babel Run Command File

Inside of .babelrc , add the following:

https://gist.github.com/okeeffed/6640da43291ded9bf3ea0fbc88105d0c.js

Setting Up TypeScript

This part you will need to adjust to flavour, but here is the config that I have for the Twitter bot:

https://gist.github.com/okeeffed/2f2ce20124863b1b0b2ff1153158b01b.js

Webpack

In this example, I am expecting that you are using Webpack 5.

In webpack.config.js :

https://gist.github.com/okeeffed/d63721ddc9715a181a5129cf14d00955.js

Here we tell Webpack to set the src/index.ts as the entry point and to convert to commonjs .

We set our Babel and Cache loaders to test and compile any ts or js file that it finds from that entry point.

Given that we are not using Node Externals which avoids bundling node modules, then any node modules required will also be compiled into the output.

That means that the output in dist/index.js which run our project without node modules installed, which is perfect for AWS Lambda!

Running A Build

A "build": "webpack" to your "scripts" key in the package.json file and you are ready to roll!

Run npm run build , let Webpack work its magic and then see the single-file output in dist/index.js .

Testing Your Projects

I use lambda-local for testing the build before deployment with the AWS CDK. It targets Nodejs, which is perfect for your TypeScript/JavaScript projects!

Follow the instructions on the website to install and give it a whirl! If things run smoothly, you can be confident with your deployment.

Conclusion

This post focused purely on the build process. As mentioned in the intro, some of my other posts will cover writing lambda functions and the actual AWS CDK deployments.

Resources and Further Reading

Image credit: Jess Bailey

Originally posted on my blog.