Skip to content

Commit 8061cf1

Browse files
authored
feat(sls): added example for nestjs (#394)
1 parent c4cd39e commit 8061cf1

18 files changed

+8101
-0
lines changed

examples/sls/nestjs/.eslintrc.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: __dirname,
6+
sourceType: 'module',
7+
},
8+
plugins: ['@typescript-eslint/eslint-plugin'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true,
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
},
25+
};

examples/sls/nestjs/.gitignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# compiled output
2+
dist
3+
node_modules
4+
.serverless
5+
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
pnpm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# OS
16+
.DS_Store
17+
18+
# Tests
19+
coverage
20+
.nyc_output
21+
22+
# IDEs and editors
23+
.idea
24+
.project
25+
.classpath
26+
.c9/
27+
*.launch
28+
.settings/
29+
*.sublime-workspace
30+
31+
# IDE - VSCode
32+
.vscode/*
33+
!.vscode/settings.json
34+
!.vscode/tasks.json
35+
!.vscode/launch.json
36+
!.vscode/extensions.json

examples/sls/nestjs/.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

examples/sls/nestjs/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM public.ecr.aws/docker/library/node:18.19-slim as builder
2+
RUN npm i -g pnpm
3+
4+
WORKDIR /app
5+
6+
COPY . .
7+
RUN pnpm install && pnpm run build
8+
9+
FROM public.ecr.aws/docker/library/node:18.19-slim as runner
10+
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.1 /lambda-adapter /opt/extensions/lambda-adapter
11+
12+
ENV PORT=8080 NODE_ENV=production
13+
ENV AWS_LWA_ENABLE_COMPRESSION=true
14+
15+
WORKDIR ${LAMBDA_TASK_ROOT}
16+
COPY --from=builder /app/package.json ./package.json
17+
COPY --from=builder /app/dist ./dist
18+
COPY --from=builder /app/node_modules ./node_modules
19+
20+
CMD [ "node", "dist/main.js" ]

examples/sls/nestjs/README.md

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# NestJS Example with Serverless Framework
2+
3+
Basic example of using the `aws-lambda-web-adapter` package with the NestJS framework, and the Serverless Framework version 3.
4+
5+
## Overview
6+
7+
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.
8+
9+
## Getting Started
10+
11+
Follow these steps to get the example up and running:
12+
13+
1. Install the project dependencies using pnpm / npm:
14+
15+
```bash
16+
pnpm install
17+
```
18+
19+
2. Deploy the application using the Serverless Framework:
20+
21+
```bash
22+
serverless deploy
23+
```
24+
25+
This command will deploy the application to your AWS account using the Serverless Framework and create the necessary AWS Lambda function.
26+
27+
3. Test the endpoint:
28+
29+
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.
30+
31+
32+
To tear down the app, use:
33+
34+
```bash
35+
serverless remove
36+
```
37+
38+
This command will performs the following actions:
39+
- Deletes the deployed AWS Lambda functions and associated resources.
40+
- Cleans up any event sources or triggers associated with the functions.
41+
- Deletes any additional resources provisioned by the Serverless Framework, such as AWS CloudFormation stacks or other infrastructure components.
42+
43+
## Author
44+
- [Lafif Astahdziq](https://lafif.me)

examples/sls/nestjs/nest-cli.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

examples/sls/nestjs/package.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "serverless-nestjs",
3+
"version": "0.0.1",
4+
"description": "",
5+
"author": "",
6+
"private": true,
7+
"license": "UNLICENSED",
8+
"scripts": {
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": "^10.0.0",
24+
"@nestjs/core": "^10.0.0",
25+
"@nestjs/platform-express": "^10.0.0",
26+
"reflect-metadata": "^0.1.13",
27+
"rxjs": "^7.8.1",
28+
"serverless": "^3.38.0"
29+
},
30+
"devDependencies": {
31+
"@nestjs/cli": "^10.0.0",
32+
"@nestjs/schematics": "^10.0.0",
33+
"@nestjs/testing": "^10.0.0",
34+
"@types/express": "^4.17.17",
35+
"@types/jest": "^29.5.2",
36+
"@types/node": "^20.3.1",
37+
"@types/supertest": "^6.0.0",
38+
"@typescript-eslint/eslint-plugin": "^6.0.0",
39+
"@typescript-eslint/parser": "^6.0.0",
40+
"eslint": "^8.42.0",
41+
"eslint-config-prettier": "^9.0.0",
42+
"eslint-plugin-prettier": "^5.0.0",
43+
"jest": "^29.5.0",
44+
"prettier": "^3.0.0",
45+
"source-map-support": "^0.5.21",
46+
"supertest": "^6.3.3",
47+
"ts-jest": "^29.1.0",
48+
"ts-loader": "^9.4.3",
49+
"ts-node": "^10.9.1",
50+
"tsconfig-paths": "^4.2.0",
51+
"typescript": "^5.1.3"
52+
},
53+
"jest": {
54+
"moduleFileExtensions": [
55+
"js",
56+
"json",
57+
"ts"
58+
],
59+
"rootDir": "src",
60+
"testRegex": ".*\\.spec\\.ts$",
61+
"transform": {
62+
"^.+\\.(t|j)s$": "ts-jest"
63+
},
64+
"collectCoverageFrom": [
65+
"**/*.(t|j)s"
66+
],
67+
"coverageDirectory": "../coverage",
68+
"testEnvironment": "node"
69+
}
70+
}

0 commit comments

Comments
 (0)