Skip to content

feat(serverless-nestjs): added example implementation of nestjs with … #394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/sls/nestjs/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
36 changes: 36 additions & 0 deletions examples/sls/nestjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# compiled output
dist
node_modules
.serverless

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
coverage
.nyc_output

# IDEs and editors
.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions examples/sls/nestjs/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
20 changes: 20 additions & 0 deletions examples/sls/nestjs/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
FROM public.ecr.aws/docker/library/node:18.19-slim as builder
RUN npm i -g pnpm

WORKDIR /app

COPY . .
RUN pnpm install && pnpm run build

FROM public.ecr.aws/docker/library/node:18.19-slim as runner
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter

ENV PORT=8080 NODE_ENV=production
ENV AWS_LWA_ENABLE_COMPRESSION=true

WORKDIR ${LAMBDA_TASK_ROOT}
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules

CMD [ "node", "dist/main.js" ]
44 changes: 44 additions & 0 deletions examples/sls/nestjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# NestJS Example with Serverless Framework

Basic example of using the `aws-lambda-web-adapter` package with the NestJS framework, and the Serverless Framework version 3.

## Overview

The article ["Container Image Support for AWS Lambda"](https://www.serverless.com/blog/container-support-for-lambda) on the Serverless Framework blog introduces the ability to use container images with AWS Lambda, allowing developers to have full control over the execution environment and access to custom runtimes and libraries.

## Getting Started

Follow these steps to get the example up and running:

1. Install the project dependencies using pnpm / npm:

```bash
pnpm install
```

2. Deploy the application using the Serverless Framework:

```bash
serverless deploy
```

This command will deploy the application to your AWS account using the Serverless Framework and create the necessary AWS Lambda function.

3. Test the endpoint:

Once the deployment is complete, the Serverless Framework will provide you with the endpoint URL. You can test the endpoint by sending an HTTP request to that URL. You should receive a "Hello, World!" response.


To tear down the app, use:

```bash
serverless remove
```

This command will performs the following actions:
- Deletes the deployed AWS Lambda functions and associated resources.
- Cleans up any event sources or triggers associated with the functions.
- Deletes any additional resources provisioned by the Serverless Framework, such as AWS CloudFormation stacks or other infrastructure components.

## Author
- [Lafif Astahdziq](https://lafif.me)
8 changes: 8 additions & 0 deletions examples/sls/nestjs/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
70 changes: 70 additions & 0 deletions examples/sls/nestjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "serverless-nestjs",
"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",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"serverless": "^3.38.0"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Loading