Serverless-offline Lambda timeout 504
Asked Answered
J

0

6

I am serving local NestJS backend using serverless-offline plugin. Sometimes it serves and runs without any issues but sometimes I receive "× [504] - Lambda timeout" error in terminal after any request I make. Doesn't matter if it's GET or POST etc. The whole error is like this:

{"errorMessage":"[504] - Lambda timeout.","errorType":"LambdaTimeoutError","stackTrace":["Error: [504] - Lambda timeout.","at #timeoutAndTerminate (file:///C:/Users/z0044vnt/Desktop/diplomka/application-compliance-suite-monorepo/node_modules/serverless-offline/src/lambda/LambdaFunction.js:283:11)","at async LambdaFunction.runHandler (file:///C:/Users/z0044vnt/Desktop/diplomka/application-compliance-suite-monorepo/node_modules/serverless-offline/src/lambda/LambdaFunction.js:305:16)","at async file:///C:/Users/z0044vnt/Desktop/diplomka/application-compliance-suite-monorepo/node_modules/serverless-offline/src/events/http/HttpServer.js:602:18","at async exports.Manager.execute (C:\\Users\\z0044vnt\\Desktop\\diplomka\\application-compliance-suite-monorepo\\node_modules\\@hapi\\hapi\\lib\\toolkit.js:60:28)","at async internals.handler (C:\\Users\\z0044vnt\\Desktop\\diplomka\\application-compliance-suite-monorepo\\node_modules\\@hapi\\hapi\\lib\\handler.js:46:20)","at async exports.execute (C:\\Users\\z0044vnt\\Desktop\\diplomka\\application-compliance-suite-monorepo\\node_modules\\@hapi\\hapi\\lib\\handler.js:31:20)","at async Request._lifecycle (C:\\Users\\z0044vnt\\Desktop\\diplomka\\application-compliance-suite-monorepo\\node_modules\\@hapi\\hapi\\lib\\request.js:370:32)"]}

The NestJS app won't even start so I suspect this may be a problem with my local windows machine or the lambda configuration, but timeout for the lambda is set for 29sec and it time outs way earlier. The local server starts but time outs all of the requests. This is how it looks like in the terminal after I serve it with sls offline --stage local --ignoreJWTSignature --noAuth:

npm run be:local

> [email protected] be:local
> nx run backend:serve:local


> nx run backend:serve:local

Starting Offline at stage local (eu-central-1)
No errors found.
Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
           * main: application-compliance-suite-backend-local-main

 │                                                                           │
 │   ANY     | http://localhost:3000/{proxy*}                                │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │   GET     | http://localhost:3000/swagger                                 │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │   GET     | http://localhost:3000/swagger/{proxy*}                        │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │   OPTIONS | http://localhost:3000/{proxy*}                                │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │   GET     | http://localhost:3000/local/swagger                           │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │   GET     | http://localhost:3000/local/swagger/{proxy*}                  │
 │   POST    | http://localhost:3000/2015-03-31/functions/main/invocations   │
 │                                                                           │
 └───────────────────────────────────────────────────────────────────────────┘

Server ready: http://localhost:3000 🚀
GET /local/swagger (λ: main)
× [504] - Lambda timeout.
ANY /favicon.ico (λ: main)
× [504] - Lambda timeout.

The lambda config in serverless.yml:

functions:
    main:
        handler: src/main.handler
        memorySize: 256
        timeout: 29
        role: !GetAtt MainLambdaFunctionIAMRole.Arn
        vpc: ${file(serverless/sls-mappings.yml):stageMap.${self:provider.stage}.vpc}
        layers:
            - !Ref DependenciesLambdaLayer
        environment:
            STAGE: ${self:provider.stage}
            NO_COLOR: true
            REGION: ${self:custom.environmentVariables.${self:provider.stage}.region}
            DB_SECRET: ${self:custom.environmentVariables.${self:provider.stage}.dbSecret}
        events:
            - httpApi: # ApiGatewayV2
                  method: any
                  path: /{proxy+}
                  authorizer:
                      name: customAuthorizer
            - httpApi: # Escape swagger
                  method: get
                  path: /swagger
            - httpApi: # Escape swagger
                  method: get
                  path: /swagger/{proxy+}
            - httpApi: # escaping the OPTIONS call
                  method: options
                  path: /{proxy+}
            - httpApi: # Escape swagger base url staged
                  method: get
                  path: /${self:provider.stage}/swagger
            - httpApi: # Escape swagger staged
                  method: get
                  path: /${self:provider.stage}/swagger/{proxy+}

My package.json:

{
    "name": "application-compliance-suite-monorepo",
    "version": "0.0.0",
    "license": "MIT",
    "scripts": {
        "nx": "nx",
        "be:local": "nx run backend:serve:local",
        "fe:local": "nx run frontend:serve:local"
    },
    "private": true,
    "devDependencies": {
        "@angular-devkit/build-angular": "16.0.0",
        "@angular-eslint/eslint-plugin": "16.0.1",
        "@angular-eslint/eslint-plugin-template": "16.0.1",
        "@angular-eslint/template-parser": "16.0.1",
        "@angular/cli": "16.0.0",
        "@angular/compiler-cli": "16.0.0",
        "@angular/language-service": "16.0.0",
        "@nestjs/testing": "9.1.1",
        "@ngxs/devtools-plugin": "^3.8.1",
        "@ngxs/logger-plugin": "^3.8.1",
        "@nx/angular": "16.1.4",
        "@nx/cypress": "16.1.4",
        "@nx/esbuild": "16.1.4",
        "@nx/eslint-plugin": "16.1.4",
        "@nx/jest": "16.1.4",
        "@nx/js": "16.1.4",
        "@nx/linter": "16.1.4",
        "@nx/nest": "16.1.4",
        "@nx/node": "16.1.4",
        "@nx/workspace": "16.1.4",
        "@types/aws-lambda": "^8.10.110",
        "@types/jest": "^28.1.1",
        "@types/node": "18.7.1",
        "@types/node-rsa": "^1.1.1",
        "@types/passport-jwt": "^3.0.6",
        "@types/uuid": "^9.0.0",
        "@typescript-eslint/eslint-plugin": "5.58.0",
        "@typescript-eslint/parser": "5.58.0",
        "aws-cdk": "^2.62.0",
        "aws-cdk-lib": "^2.62.0",
        "constructs": "^10.1.231",
        "copy-webpack-plugin": "^11.0.0",
        "cypress": "^12.2.0",
        "esbuild": "0.17.17",
        "eslint": "~8.15.0",
        "eslint-config-prettier": "8.1.0",
        "eslint-plugin-cypress": "^2.10.3",
        "eslint-plugin-prettier": "^4.2.1",
        "jest": "^29.5.0",
        "jest-environment-jsdom": "^29.5.0",
        "jest-preset-angular": "13.1.0",
        "ng-packagr": "^16.0.1",
        "ngx-translate-testing": "^6.1.0",
        "nx": "16.1.4",
        "postcss": "^8.4.5",
        "postcss-import": "~14.1.0",
        "postcss-preset-env": "~7.5.0",
        "postcss-url": "~10.1.3",
        "prettier": "^2.6.2",
        "serverless": "^3.26.0",
        "serverless-offline": "^12.0.4",
        "serverless-plugin-reducer": "^4.0.1",
        "serverless-webpack": "^5.11.0",
        "ts-jest": "29.1.0",
        "ts-node": "10.9.1",
        "typescript": "5.0.4"
    },
    "dependencies": {
        "@angular/animations": "16.0.0",
        "@angular/cdk": "16.0.0",
        "@angular/common": "16.0.0",
        "@angular/compiler": "16.0.0",
        "@angular/core": "16.0.0",
        "@angular/forms": "16.0.0",
        "@angular/material": "16.0.0",
        "@angular/platform-browser": "16.0.0",
        "@angular/platform-browser-dynamic": "16.0.0",
        "@angular/router": "16.0.0",
        "@automapper/classes": "^8.7.7",
        "@automapper/core": "^8.7.7",
        "@automapper/nestjs": "^8.7.7",
        "@automapper/types": "^6.3.1",
        "@aws-sdk/client-secrets-manager": "^3.264.0",
        "@aws-sdk/client-sqs": "^3.272.0",
        "@fastify/aws-lambda": "^3.0.0",
        "@fastify/static": "^6.10.2",
        "@fastify/swagger": "^8.3.1",
        "@nestjs/common": "9.1.1",
        "@nestjs/config": "^2.3.0",
        "@nestjs/core": "9.1.1",
        "@nestjs/jwt": "^10.0.0",
        "@nestjs/passport": "^9.0.0",
        "@nestjs/platform-fastify": "^9.2.0",
        "@nestjs/schematics": "9.1.0",
        "@nestjs/swagger": "6.3.0",
        "@nestjs/typeorm": "^9.0.1",
        "@ngx-translate/core": "^14.0.0",
        "@ngx-translate/http-loader": "^7.0.0",
        "@ngxs/store": "^3.8.1",
        "angular-auth-oidc-client": "^15.0.3",
        "axios": "^1.2.3",
        "class-transformer": "^0.5.1",
        "class-validator": "^0.14.0",
        "css-vars-ponyfill": "^2.4.8",
        "node-rsa": "^1.1.1",
        "passport": "^0.6.0",
        "passport-custom": "^1.1.1",
        "pg": "^8.11.0",
        "reflect-metadata": "^0.1.13",
        "rxjs": "~7.5.0",
        "tslib": "^2.5.0",
        "typeorm": "^0.3.16",
        "zone.js": "^0.13.0"
    }
}

And I am running Windows 10, Node 18.16.0. I've also checked my laptop utilization (CPU,..) and that's also fine. Do you know why this happens and how to get rid of it?

Jett answered 6/6, 2023 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.