Skip to content

Instantly share code, notes, and snippets.

@odev954
Last active January 26, 2023 13:05
Show Gist options
  • Save odev954/15dd16fe68648a5041841f6d383b0af9 to your computer and use it in GitHub Desktop.
Save odev954/15dd16fe68648a5041841f6d383b0af9 to your computer and use it in GitHub Desktop.
AWS Lambda Nest Microservice Essentials
import { NestFactory } from '@nestjs/core';
import { ExpressAdapter } from '@nestjs/platform-express';
import { INestApplication } from '@nestjs/common';
import { AppModule } from './app.module';
import { Express } from 'express';
import * as express from 'express';
import { createServer } from 'aws-serverless-express';
import { IBootstrapper } from './abstract/IBootstrapper';
import { Server } from 'http';
export class NestBootstrapper implements IBootstrapper {
private cachedServer: Server;
private async createExpressApp(
expressApp: Express,
): Promise<INestApplication> {
const app = await NestFactory.create(
AppModule,
new ExpressAdapter(expressApp),
);
return app;
}
public async bootstrap(): Promise<Server> {
if (!this.cachedServer) {
const expressApp = express();
const app = await this.createExpressApp(expressApp);
await app.init();
this.cachedServer = createServer(expressApp);
}
return this.cachedServer;
}
}
FROM public.ecr.aws/lambda/nodejs:16
COPY package.json nest-cli.json tsconfig.build.json tsconfig.json src/ ${LAMBDA_TASK_ROOT}/
RUN npm i
RUN npm run build
CMD [ "dist/main.handler" ]
import { Server } from 'http';
export interface IBootstrapper {
/**
* Bootstraps the application.
*/
bootstrap(): Promise<Server>;
}
import { Context } from 'aws-lambda';
import { proxy, Response } from 'aws-serverless-express';
import { NestBootstrapper } from './bootstrapper';
export async function handler(event: any, context: Context): Promise<Response> {
const bootstrapper = new NestBootstrapper();
const server = await bootstrapper.bootstrap();
return proxy(server, event, context, 'PROMISE').promise;
}
{
"name": "my-microservice",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix"
},
"dependencies": {
"@aws-sdk/client-dynamodb": "^3.252.0",
"@aws-sdk/lib-dynamodb": "^3.252.0",
"@nestjs/common": "^9.0.0",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@types/aws-lambda": "^8.10.109",
"@types/aws-serverless-express": "^3.3.5",
"@types/uuid": "^9.0.0",
"aws-lambda": "^1.0.7",
"aws-serverless-express": "^3.4.0",
"nest": "^0.1.6",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"uuid": "^9.0.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^9.0.0",
"@types/express": "^4.17.13",
"@types/jest": "29.2.4",
"@types/node": "18.11.18",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "29.3.1",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"supertest": "^6.1.3",
"ts-jest": "29.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "4.1.1",
"typescript": "^4.7.4"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment