/serverless-ts-starter

A serverless typescript starter with offline, unit tests and environment variables support.

Primary LanguageJavaScriptMIT LicenseMIT

Serverless TypeScritp starter

A serverless starter that adds TypeScript syntax, serverless-offline, environment variables and unit test support. Based on the aws-nodejs-typescript template provided by the serverless framework.

Serverless TypeScript starter uses the serverless-webpack plugin, serverless-offline, ts-loader, jest and ts-jest.

  • ES7 syntax in your handler functions
    • Use import, export, async and await
  • Package your function using webpack
  • Run API Gateway locally
    • Use serverless offline start
  • Support for unit tests
    • Run npm test to run your tests
  • Sourcemaps for proper error messages
    • Error message show the correct line numbers
    • Works in production with CloudWatch
  • Automatic support for multiple handler files
    • No need to add a new entry to your webpack.config.js
  • Add environment variables for your stages

Starter function

Here's the code for the sample function:

import { APIGatewayProxyHandler } from 'aws-lambda';
import 'source-map-support/register';

export const hello: APIGatewayProxyHandler = async (event, _context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({
      message: `Go Serverless TypeScript v1.0! ${(await message({ time: 1, copy: 'Your function executed successfully!'}))}`,
      input: event,
    }, null, 2),
  };
}

const message = ({ time, ...rest }) => new Promise((resolve, _reject) =>
  setTimeout(() => {
    resolve(`${rest.copy} (with a delay)`);
  }, time * 1000)
);

Requirements

Installation

To create a new Serverless project.

$ serverless install --url https://github.com/AYM1607/serverless-ts-starter --name my-project

Enter the new directory

$ cd my-project

Install the Node.js packages

$ npm install

Usage

To run unit tests on your local environment

$ npm test

To run a function on your local environment

$ serverless invoke local --function hello

To simulate API Gateway locally using serverless-offline

$ serverless offline start

We use Jest to run our tests. You can read more about setting up your tests here.

Deploy your project

$ serverless deploy

Deploy a single function

$ serverless deploy function --function hello

Maintainers

Serverless TypeScript starter is maintained by Mariano Uvalle (@AYM1607).