Aspecto blog

On microservices, OpenTelemetry, and anything in between

Easily deploy TypeScript project to AWS Lambda using Github Actions

deploy TypeScript project to AWS Lambda

Share this post

I recently worked on a small TypeScript project which I wanted to deploy as a Lambda to AWS.

My project had multiple files and some npm dependencies.

I found some resources online, but none of them really hit the spot.

Some didn’t explain how to include the node_modules, some didn’t explain how to do it with TypeScript and some just handled a single file.

In this post, I’ll describe how to prepare the project for deployment and show a detailed example of doing it using Github Actions.

Project Structure

Let’s assume our project structure looks like this:

 ├── package.json
 ├── tsconfig.json
 ├── node_modules
 │    ├── module_1
 │    ├── module_2
 │    └── etc..
 └── src
      ├── index.ts
      └── utils.ts

This is our tsconfig.json :

  "compilerOptions": {
    "module": "commonjs",
    "target": "ES2017",
    "lib": ["ES2019"],
    "rootDir": "src",
    "outDir": "dist",
    "declaration": false, 
    "sourceMap": false,
    "inlineSources": false,
    "types": ["node"]

And index.ts contains as the main handler, and uses utils.ts, something like this:

import { APIGatewayEvent, Context } from 'aws-lambda';
import { doSomething } from './utils';
export const handler = async (event: APIGatewayEvent, context: Context) => { 
    // Do some other stuff…

Lambda example (taking advantage of TS using aws-lambda types lib)


AWS Lamda can only run js files!

Also, AWS Lambda can’t install node_modules for us.With the default settings, AWS Lambda supports either:

  • Deploying a single index.js file directly.
  • Upload a zip file containing all the project files, which has the index.js in the zip root. So we need to zip the project correctly, without including the parent directory.

We also need to make sure the node_modules are part of this zip file.

Drake meme Expected Zip file structure
Expected Zip file structure

Prepare for deployment

1. Build the project

We will use TypeScript’s tsc command to transpile the project into js files.

After building, this will be our project structure:

 ├── package.json
 ├── tsconfig.json
 ├── node_modules
 ├── dist
 │    ├── index.js
 │    └── utils.js
 └── src
      ├── index.ts
      └── utils.ts

2. Move node_modules to dist folder

As mentioned before, AWS Lambda can’t install the node_modules for us, so we need to assure they are located correctly before creating our zip file.

3. ZIP

The project needs to be zipped in the required structure, without the parent directory. The easiest way I found to do it, is to run the following command from the project root:

$ (cd dist && zip -r ../ .)

The zip is now ready for deployment.

Deploy using Github Actions

The deployment part will be done using appleboy/lambda-action, and can be easily substituted with a corresponding AWS CLI command, which comes preinstalled on Github Actions CI machines.

Here’s a detailed example of a Github action:

name: Deploy
    branches: [ master ]
    runs-on: ubuntu-latest
      - name: Checkout Code
        uses: actions/checkout@v2
      - name: Install Dependencies
        run: yarn
        # build command should be set in your package.json scripts as: "build": "tsc"
      - name: Build
        run: yarn build
      - name: Move node_modules to dist
        run: mv node_modules dist/node_modules
      - name: Zip
        run: (cd dist && zip -r ../ .)
      - name: Deploy to AWS
        uses: appleboy/lambda-action@master
          aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws_region: ${{ secrets.AWS_REGION }}
          function_name: my-cool-function

That’s it! Your Lambda is now ready for work.

Spread the word

Subscribe for more distributed applications tutorials and insights that will help you boost microservices troubleshooting.