on
Using Typescript with AWS Lambda
My 2018 side projects have nearly all included the use of AWS Lambda. From Alexa skills to APIs to notification services, I’ve become adept at building and deploying Lambda-based services this year. As a front-end developer in my day job, I’m most comfortable coding in Javascript. But Javascript feels like the wrong tool for a middle tier or back-end service. The lack of types make these services unnecessarily difficult to read and reason about. Instead of switching to a reasonable language (read golang) I decided to write all my Lambda services in Typescript, and I haven’t once looked back.
Github Link to Typescript Hello World for Lambda
The above linked github project is a recipe of sorts that I use for my Lambda projects. It includes a unit testing strategy using mocha/chai. I’ve found Sinon critical to testing most Lambda services as well, simply due to the nature of those services. The package.json also includes scripts that I use for build and deployment using AWS Codebuild and Pipeline. I don’t need to expound the virtues of Typescript any more than has already been done, I would however like to stress how much more readable my middle tier and back-end services have become since making the switch and how much better the tooling for Typescript is.
The repository README has a comprehensive set of instructions for spinning up the project, but I’ve put a step-by-step guide below for convenience if needed.
Hello World - Step by Step
- Clone repository
git clone https://github.com/roodmichael/typescript-lambda-hello-world.git
- Install dependencies
cd typescript-lambda-hello-world/
npm install
- Run test script
npm run test
- Run deploy script
npm run deploy:beta
- Bundled code is now in build/bundle.zip. Upload this code to your lambda.