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:
Casting aside the Nodemon file, the important parts for the build are basically
You might also note I have both an
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 let's 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.
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:
Once installed, we can set up Babel and TypeScript.
Babel Run Command File
.babelrc, add the following:
For those unfamiliar with Babel, we are telling it to use the
@babel/preset-typescript plugins, and adding some configuration options to the env preset to target Node 12 (which is what normally I target in AWS lambda).
Setting Up TypeScript
This part you will need to adjust to flavour, but here is the config that I have for the Twitter bot:
Given that we are using Webpack, the config above likely doesn't need to be so complex with
outDir etc, but I will leave it to you to figure out what options you want there.
In this example, I am expecting that you are using Webpack 5.
Given I don't use Webpack for local development of the lambda, I always have it set for production.
Here we tell Webpack to set the
src/index.ts as the entry point and to convert to
We set our Babel and Cache loaders to test and compile any
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
"build": "webpack" to your
"scripts" key in the
package.json file and you are ready to roll!
npm run build, let Webpack work its magic and then see the single-file output in
Testing Your 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.
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
- aws cdk
- aws lambda
Deploy a simple Python lambda function with the TypeScript AWS CDK to LocalStack.
Deploy a Kubernetes Fargate on EKS cluster using the AWS CDK.
Use the ECS patterns library to deploy a simple Fargate cluster to LocalStack.
- aws cdk
Use the TypeScript AWS CDK to deploy an S3 Bucket fronted by a CloudFront CDN and Route53 record.
1,200+ PEOPLE ALREADY JOINED ❤️️
Get fresh posts + news direct to your inbox.
No spam. We only send you relevant content.