├── .prettierrc ├── .github └── FUNDING.yml ├── nest-cli.json ├── tsconfig.build.json ├── src ├── app.service.ts ├── main.ts ├── app.module.ts ├── app.controller.ts ├── swagger.ts ├── app.controller.spec.ts └── index.ts ├── test ├── jest-e2e.json └── app.e2e-spec.ts ├── tsconfig.json ├── .gitignore ├── serverless.yml ├── .eslintrc.js ├── package.json └── README.md /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "all" 4 | } -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: rdlabo 4 | -------------------------------------------------------------------------------- /nest-cli.json: -------------------------------------------------------------------------------- 1 | { 2 | "collection": "@nestjs/schematics", 3 | "sourceRoot": "src" 4 | } 5 | -------------------------------------------------------------------------------- /tsconfig.build.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "./tsconfig.json", 3 | "exclude": ["node_modules", "dist", "test", "**/*spec.ts"] 4 | } 5 | -------------------------------------------------------------------------------- /src/app.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@nestjs/common'; 2 | 3 | @Injectable() 4 | export class AppService { 5 | getHello(): string { 6 | return 'Hello World!'; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /test/jest-e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "moduleFileExtensions": ["js", "json", "ts"], 3 | "rootDir": ".", 4 | "testEnvironment": "node", 5 | "testRegex": ".e2e-spec.ts$", 6 | "transform": { 7 | "^.+\\.(t|j)s$": "ts-jest" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { AppModule } from './app.module'; 3 | 4 | async function bootstrap() { 5 | const app = await NestFactory.create(AppModule); 6 | await app.listen(3000); 7 | } 8 | bootstrap(); 9 | -------------------------------------------------------------------------------- /src/app.module.ts: -------------------------------------------------------------------------------- 1 | import { Module } from '@nestjs/common'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | @Module({ 6 | imports: [], 7 | controllers: [AppController], 8 | providers: [AppService], 9 | }) 10 | export class AppModule {} 11 | -------------------------------------------------------------------------------- /src/app.controller.ts: -------------------------------------------------------------------------------- 1 | import { Controller, Get } from '@nestjs/common'; 2 | import { AppService } from './app.service'; 3 | 4 | @Controller() 5 | export class AppController { 6 | constructor(private readonly appService: AppService) {} 7 | 8 | @Get() 9 | getHello(): string { 10 | return this.appService.getHello(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "declaration": true, 5 | "removeComments": true, 6 | "emitDecoratorMetadata": true, 7 | "experimentalDecorators": true, 8 | "allowSyntheticDefaultImports": true, 9 | "target": "es2017", 10 | "sourceMap": true, 11 | "outDir": "./dist", 12 | "baseUrl": "./", 13 | "incremental": true, 14 | "skipLibCheck": true 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # compiled output 2 | /dist 3 | /node_modules 4 | 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | 13 | # OS 14 | .DS_Store 15 | 16 | # Tests 17 | /coverage 18 | /.nyc_output 19 | 20 | # IDEs and editors 21 | /.idea 22 | .project 23 | .classpath 24 | .c9/ 25 | *.launch 26 | .settings/ 27 | *.sublime-workspace 28 | 29 | # IDE - VSCode 30 | .vscode/* 31 | !.vscode/settings.json 32 | !.vscode/tasks.json 33 | !.vscode/launch.json 34 | !.vscode/extensions.json -------------------------------------------------------------------------------- /serverless.yml: -------------------------------------------------------------------------------- 1 | service: serverless-nestjs 2 | 3 | provider: 4 | name: aws 5 | runtime: nodejs12.x 6 | region: us-east-1 7 | 8 | plugins: 9 | - serverless-offline 10 | 11 | package: 12 | exclude: 13 | - .git/** 14 | - src/** 15 | - test/** 16 | - e2e/** 17 | - nodemon.json 18 | - README.md 19 | 20 | functions: 21 | index: 22 | handler: dist/index.handler 23 | events: 24 | - http: 25 | cors: true 26 | path: '/' 27 | method: any 28 | - http: 29 | cors: true 30 | path: '{proxy+}' 31 | method: any 32 | -------------------------------------------------------------------------------- /src/swagger.ts: -------------------------------------------------------------------------------- 1 | import { NestFactory } from '@nestjs/core'; 2 | import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; 3 | import { AppModule } from './app.module'; 4 | 5 | async function bootstrap() { 6 | const app = await NestFactory.create(AppModule); 7 | 8 | const options = new DocumentBuilder() 9 | .setTitle('Cats example') 10 | .setDescription('The cats API description') 11 | .setVersion('1.0') 12 | .addTag('cats') 13 | .build(); 14 | const document = SwaggerModule.createDocument(app, options); 15 | SwaggerModule.setup('api', app, document); 16 | 17 | await app.listen(3001); 18 | } 19 | bootstrap(); 20 | -------------------------------------------------------------------------------- /src/app.controller.spec.ts: -------------------------------------------------------------------------------- 1 | import { Test, TestingModule } from '@nestjs/testing'; 2 | import { AppController } from './app.controller'; 3 | import { AppService } from './app.service'; 4 | 5 | describe('AppController', () => { 6 | let app: TestingModule; 7 | 8 | beforeAll(async () => { 9 | app = await Test.createTestingModule({ 10 | controllers: [AppController], 11 | providers: [AppService], 12 | }).compile(); 13 | }); 14 | 15 | describe('getHello', () => { 16 | it('should return "Hello World!"', () => { 17 | const appController = app.get(AppController); 18 | expect(appController.getHello()).toBe('Hello World!'); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import * as request from 'supertest'; 2 | import { Test } from '@nestjs/testing'; 3 | import { AppModule } from './../src/app.module'; 4 | import { INestApplication } from '@nestjs/common'; 5 | 6 | describe('AppController (e2e)', () => { 7 | let app: INestApplication; 8 | 9 | beforeAll(async () => { 10 | const moduleFixture = await Test.createTestingModule({ 11 | imports: [AppModule], 12 | }).compile(); 13 | 14 | app = moduleFixture.createNestApplication(); 15 | await app.init(); 16 | }); 17 | 18 | it('/ (GET)', () => { 19 | return request(app.getHttpServer()) 20 | .get('/') 21 | .expect(200) 22 | .expect('Hello World!'); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | parser: '@typescript-eslint/parser', 3 | parserOptions: { 4 | project: 'tsconfig.json', 5 | sourceType: 'module', 6 | }, 7 | plugins: ['@typescript-eslint/eslint-plugin'], 8 | extends: [ 9 | 'plugin:@typescript-eslint/recommended', 10 | 'plugin:prettier/recommended', 11 | ], 12 | root: true, 13 | env: { 14 | node: true, 15 | jest: true, 16 | }, 17 | ignorePatterns: ['.eslintrc.js'], 18 | rules: { 19 | '@typescript-eslint/interface-name-prefix': 'off', 20 | '@typescript-eslint/explicit-function-return-type': 'off', 21 | '@typescript-eslint/explicit-module-boundary-types': 'off', 22 | '@typescript-eslint/no-explicit-any': 'off', 23 | }, 24 | }; 25 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { APIGatewayProxyHandler } from 'aws-lambda'; 2 | import { NestFactory } from '@nestjs/core'; 3 | import { AppModule } from './app.module'; 4 | import { Server } from 'http'; 5 | import { ExpressAdapter } from '@nestjs/platform-express'; 6 | import * as awsServerlessExpress from 'aws-serverless-express'; 7 | import * as express from 'express'; 8 | 9 | let cachedServer: Server; 10 | 11 | const bootstrapServer = async (): Promise => { 12 | const expressApp = express(); 13 | const adapter = new ExpressAdapter(expressApp); 14 | const app = await NestFactory.create(AppModule, adapter); 15 | app.enableCors(); 16 | await app.init(); 17 | return awsServerlessExpress.createServer(expressApp); 18 | } 19 | 20 | export const handler: APIGatewayProxyHandler = async (event, context) => { 21 | if (!cachedServer) { 22 | cachedServer = await bootstrapServer() 23 | } 24 | return awsServerlessExpress.proxy(cachedServer, event, context, 'PROMISE') 25 | .promise; 26 | }; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "serverless-nestjs", 3 | "version": "0.0.0", 4 | "description": "description", 5 | "author": "", 6 | "license": "MIT", 7 | "scripts": { 8 | "prebuild": "rimraf dist", 9 | "build": "nest build", 10 | "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", 11 | "start": "nest start", 12 | "start:dev": "nest start --watch", 13 | "start:debug": "nest start --debug --watch", 14 | "start:prod": "node dist/main", 15 | "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", 16 | "test": "jest", 17 | "test:watch": "jest --watch", 18 | "test:cov": "jest --coverage", 19 | "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", 20 | "test:e2e": "jest --config ./test/jest-e2e.json" 21 | }, 22 | "dependencies": { 23 | "@nestjs/common": "^8.1.1", 24 | "@nestjs/core": "^8.1.1", 25 | "@nestjs/platform-express": "^8.1.1", 26 | "aws-serverless-express": "^3.4.0", 27 | "express": "^4.17.1", 28 | "reflect-metadata": "^0.1.13", 29 | "rimraf": "^3.0.2", 30 | "rxjs": "^7.4.0" 31 | }, 32 | "devDependencies": { 33 | "@nestjs/cli": "^8.1.3", 34 | "@nestjs/schematics": "^8.0.4", 35 | "@nestjs/swagger": "^5.2.0", 36 | "@nestjs/testing": "^8.1.1", 37 | "@types/aws-lambda": "^8.10.59", 38 | "@types/express": "^4.17.13", 39 | "@types/jest": "^27.0.2", 40 | "@types/node": "^16.11.1", 41 | "@types/supertest": "^2.0.11", 42 | "@typescript-eslint/eslint-plugin": "^4.29.2", 43 | "@typescript-eslint/parser": "^4.29.2", 44 | "eslint": "^7.32.0", 45 | "eslint-config-prettier": "^8.3.0", 46 | "eslint-plugin-import": "^2.25.4", 47 | "eslint-plugin-prettier": "^3.4.1", 48 | "jest": "^27.3.0", 49 | "prettier": "^2.4.1", 50 | "serverless-offline": "^6.9.0", 51 | "source-map-support": "^0.5.20", 52 | "supertest": "^6.1.6", 53 | "swagger-ui-express": "^4.3.0", 54 | "ts-jest": "^27.0.7", 55 | "ts-loader": "^9.2.6", 56 | "ts-node": "^10.3.0", 57 | "tsconfig-paths": "^3.11.0", 58 | "typescript": "^4.4.4" 59 | }, 60 | "jest": { 61 | "moduleFileExtensions": [ 62 | "js", 63 | "json", 64 | "ts" 65 | ], 66 | "rootDir": "src", 67 | "testRegex": ".spec.ts$", 68 | "transform": { 69 | "^.+\\.(t|j)s$": "ts-jest" 70 | }, 71 | "coverageDirectory": "../coverage", 72 | "testEnvironment": "node" 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # serverless-nestjs 2 | This is an example of creating a function that runs as nestjs using the serverless framework. 3 | Sample publish. https://mmjdx4zxmc.execute-api.ap-northeast-1.amazonaws.com/dev/ 4 | 5 | ## What is changed. 6 | 7 | ### add 8 | - `src/index.ts` 9 | - `src/swagger.ts` 10 | - `serverless.yml` 11 | 12 | ### change 13 | - `package.json` 14 | 15 | ## How to use 16 | ### Prepare 17 | 18 | ``` 19 | $ npm install @nestjs/cli serverless -g 20 | $ git clone git@github.com:rdlabo/serverless-nestjs.git 【projectName】 21 | $ cd 【projectName】 22 | $ npm install 23 | $ npm start 24 | ``` 25 | 26 | ### Development 27 | #### use NestCLI 28 | 29 | ``` 30 | $ npm start 31 | ``` 32 | 33 | ``` 34 | $ npm start 35 | > serverless-nestjs@0.0.0 start /Users/sakakibara/dev/serverless-nestjs 36 | > nest start 37 | 38 | [Nest] 3905 - 11/29/2019, 4:40:49 PM [NestFactory] Starting Nest application... 39 | [Nest] 3905 - 11/29/2019, 4:40:49 PM [InstanceLoader] AppModule dependencies initialized +20ms 40 | [Nest] 3905 - 11/29/2019, 4:40:49 PM [RoutesResolver] AppController {/}: +6ms 41 | [Nest] 3905 - 11/29/2019, 4:40:49 PM [RouterExplorer] Mapped {/, GET} route +3ms 42 | [Nest] 3905 - 11/29/2019, 4:40:49 PM [NestApplication] Nest application successfully started +4ms 43 | ``` 44 | 45 | Then browse http://localhost:3000 46 | 47 | #### use serverless-offline 48 | __after also doing an: `npm run build`__ 49 | 50 | ```bash 51 | $ sls offline 52 | ``` 53 | 54 | ``` 55 | $ sls offline 56 | Serverless: Starting Offline: dev/us-east-1. 57 | 58 | Serverless: Routes for index: 59 | Serverless: ANY / 60 | Serverless: ANY /{proxy*} 61 | 62 | Serverless: Offline listening on http://localhost:3000 63 | ``` 64 | 65 | Then browse http://localhost:3000 66 | 67 | ## How to Deploy 68 | ```bash 69 | $ npm run build && sls deploy 70 | ``` 71 | 72 | ## Options 73 | ### Hot start 74 | See : https://serverless.com/blog/keep-your-lambdas-warm/ 75 | 76 | These behavior can be fixed with the plugin serverless-plugin-warmup 77 | 78 | 1 Install the plugin 79 | 80 | ``` 81 | $ npm install serverless-plugin-warmup --save-dev 82 | ``` 83 | 84 | 2 Enable the plugin 85 | 86 | ``` 87 | plugins: 88 | - '@hewmen/serverless-plugin-typescript' 89 | - serverless-plugin-optimize 90 | - serverless-offline 91 | - serverless-plugin-warmup 92 | 93 | custom: 94 | # Enable warmup on all functions (only for production and staging) 95 | warmup: 96 | - production 97 | - staging 98 | ``` 99 | 100 | ### Use Swagger for development 101 | 102 | ``` 103 | $ npx ts-node src/swagger.ts 104 | ``` 105 | 106 | ``` 107 | [Nest] 6890 - 2019-03-24 15:11 [NestFactory] Starting Nest application... 108 | [Nest] 6890 - 2019-03-24 15:11 [InstanceLoader] AppModule dependencies initialized +11ms 109 | [Nest] 6890 - 2019-03-24 15:11 [RoutesResolver] AppController {/}: +224ms 110 | [Nest] 6890 - 2019-03-24 15:11 [RouterExplorer] Mapped {/, GET} route +2ms 111 | [Nest] 6890 - 2019-03-24 15:11 [NestApplication] Nest application successfully started +2ms 112 | ``` 113 | 114 | Then browse http://localhost:3001/api 115 | 116 | **This function is for development.** If you want to use production, change package.json dependencies and serverless.yml. 117 | --------------------------------------------------------------------------------