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

.
├── nodemon.json
├── package-lock.json
├── package.json
├── src
│   ├── index.local.ts
│   ├── index.ts
│   └── function.ts
├── tsconfig.json
├── .babelrc
└── webpack.config.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.

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

npm i --save-dev \
  ## install required babel deps
  @babel/core \
  @babel/preset-env \
  @babel/preset-typescript \
  ## webpack deps and loaders required
  webpack \
  webpack-cli \
  babel-loader \
  cache-loader \
  fork-ts-checker-webpack-plugin \
  ## finally install TypeScript
  typescript

#webpack #aws #typescript #programming #developer

Webpack 5 Builds for AWS Lambda Functions with TypeScript
2.85 GEEK