TypeScript Support

Use serverless.ts with async exports — full TypeScript configuration support

Overview

sls.tf supports TypeScript configuration files (serverless.ts) via an external Node.js parser. The parser handles export default, async function exports, and Promise-returning exports, so you can write your serverless configuration with full TypeScript type safety.

Prerequisites

The TypeScript parser requires Node.js and either ts-node or the sls-tf NPM package in your project:

npm install --save-dev ts-node @types/node @serverless/typescript

Enable TypeScript parsing

Set config_format = "typescript" and point config_path at your serverless.ts file:

main.tf

module "serverless" {
  source        = "./modules/sls.tf"
  config_path   = "${path.root}/serverless.ts"
  config_format = "typescript"
}

Static export

The simplest form — a typed constant exported as default:

serverless.ts

import type { AWS } from '@serverless/typescript';

const config: AWS = {
  service: 'my-api',
  frameworkVersion: '3',
  provider: {
    name: 'aws',
    runtime: 'nodejs20.x',
    region: 'us-east-1',
    stage: process.env.STAGE ?? 'dev',
  },
  functions: {
    api: {
      handler: 'dist/handler.main',
      events: [
        {
          http: {
            path: '/{proxy+}',
            method: 'any',
            cors: true,
          },
        },
      ],
    },
  },
};

export default config;

Async export

The parser also handles async exports, useful when you need to fetch secrets or configuration at parse time:

serverless.ts (async)

import type { AWS } from '@serverless/typescript';

const getConfig = async (): Promise<AWS> => {
  // Async setup — e.g. read from a secrets file
  const stage = process.env.STAGE ?? 'dev';

  return {
    service: 'my-api',
    provider: {
      name: 'aws',
      runtime: 'nodejs20.x',
      region: 'us-east-1',
      stage,
    },
    functions: {
      hello: {
        handler: `dist/handlers/${stage}.handler`,
        events: [{ http: { path: '/hello', method: 'get' } }],
      },
    },
  };
};

export default getConfig;

Environment variables during parse

Pass environment variables into the TypeScript parser via Terraform:

module "serverless" {
  source        = "./modules/sls.tf"
  config_path   = "${path.root}/serverless.ts"
  config_format = "typescript"

  environment_vars = {
    STAGE      = "production"
    AWS_REGION = "eu-west-1"
  }
}

How it works

During terraform plan / terraform apply, sls.tf calls an external data source that executes a Node.js script via ts-node. The script imports your serverless.ts, resolves any async exports, and returns the config as JSON. The parser looks for ts-node first in your project's local node_modules, then falls back to the module's own scripts directory.

  • Supports export default value, export default function, and export default asyncFn
  • TypeScript errors surface as clean terraform plan errors
  • No separate compilation step required — ts-node is used directly

Troubleshooting

  • Cannot find module 'ts-node': Run npm install --save-dev ts-node typescript in your project root.
  • TypeScript compilation errors: Fix type errors in serverless.ts before running terraform plan.
  • Async export not resolving: Ensure your default export is either a value or a zero-argument async function returning a Promise.