Giter Club home page Giter Club logo

sam-typescript's Introduction

Note: Regarding dependency vulnerabilities (specifically lodash etc.): I know that this exists. However, I won't update the dependencies because this project is not deployed, nor should it be deployed immediately, in production. If you want to use it in your project, remember to update the dependencies yourself. After all, it's just a demo project!

TypeScript with AWS Serverless Application Model

This is a sample project based on AWS SAM's hello-world template project, which demonstrates how you can use TypeScript with SAM/Lambda (which is not currently supported by SAM/Lambda itself).

Prerequisites

  1. AWS SAM CLI
  2. Node.js 12 or NPM

Usage

Two ways to do this:

Use my script that automatically watches and builds the TypeScript files; it also starts a local SAM instance:

cd hello-world
npm install
npm run start-api

or run the build process manually and use SAM CLI directly:

cd hello-world
npm install
npm run build
cd ..
sam local start-api

Caveat

  • You need to reload the script when either (1) template.yaml is modified, or (2) you have installed an extra dependency

Running Tests

cd hello-world
npm install
npm run test

Explanation

Since AWS Lambda does not natively support TypeScript (which is understandable because it is not a runtime), you need to:

  • manually compile your TS files into JS files.
  • copy all dependencies to your /dist folder (which contains the built artefacts that are going to be deployed to Lambda).
  • start a local SAM API instance (i.e. run sam local start-api)

Difference between this project and AWS's official hello-world template

  • Tests are contained in *.spec.ts
  • TypeScript! (added tsconfig.json)
  • A bit more sample code (e.g. example.service.ts) to demonstrate imports
  • template.yaml points to /dist instead of /hello-world
  • More dependencies to help automate the build process (tsc, ts-node, concurrently, cpx, mkdirp; all of which are listed under devDependencies)

Next steps

  • Reorganise the code here. This repository is based on AWS SAM's official hello-world template, which, in my opinion, is shoddily organised. AWS SAM supports running multiple languages and packages on one stack, but working with multiple languages on one monolithic stack is a recipe for disaster and headaches, as you can't leverage language-specific features in your project (e.g. npm scripts in this case). If you require multiple languages on one stack, consider breaking your stack into multiple stacks, with each stack being a microservice. If you want to see how I do it in my latest project, see news-neutrality-scraper
  • Use webpack! Webpack allows you to ahem add extra build steps without making your package.json scripts messy. Webpack also has a bunch of plugins which you can easily integrate to make your TS/Lambda development way nicer.

Questions or Issues?

Chuck them in as an issue! I'll try to help, but keep in mind that this is just a hobby project of mine.

sam-typescript's People

Contributors

nijin39 avatar rocki80 avatar

Stargazers

 avatar  avatar KiHong Kim avatar  avatar Ailee / Wongyung koh avatar leehee avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.