使用无服务器将NestJS部署到AWS Lambda

问题描述 投票:0回答:1

我正在尝试部署一个 NestJS

lambdalith
(整个 http 服务器作为一个 lambda 函数)。

这是我在

lambda.ts
目录中的
src
文件

import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { customExceptionFactory } from './pipes/validation.pipe';
import { Callback, Context, Handler } from 'aws-lambda';
import serverlessExpress from '@codegenie/serverless-express';
import { NestExpressApplication } from '@nestjs/platform-express';

let server: Handler;

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule, {
    cors: true,
  });

  app.setGlobalPrefix('api/v1');

  app.useGlobalPipes(
    new ValidationPipe({
      transform: true,
      whitelist: true,
      exceptionFactory: customExceptionFactory,
    }),
  );

  await app.init();

  const expressApp = app.getHttpAdapter().getInstance();
  return serverlessExpress({ app: expressApp });
}

export const handler: Handler = async (
  event: any,
  context: Context,
  callback: Callback,
) => {
  server = server ?? (await bootstrap());
  return server(event, context, callback);
};

这是我的

serverless.yaml

service: service-name

plugins:
  - serverless-offline
  - serverless-dotenv-plugin

provider:
  name: aws
  runtime: nodejs18.x
  stage: ${opt:stage, 'dev'}
  region: ${opt:region, 'region-here'}
  memorySize: 512
  logRetentionInDays: 1

package:
  exclude:
    - node_modules/**
    - dist/**

functions:
  main:
    handler: dist/lambda.handler
    events:
      - http:
          method: ANY
          path: /{proxy+}

这是我触发此部署的 GitHub 操作

name: dev deploy

on:
  push:
    branches:
      - test-branch

env:
  AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
  AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18.x]
    steps:
      - uses: actions/checkout@v3
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: node_modules/.bin/sls deploy
          --verbose
          --stage dev

部署会完成,但是每当我尝试调用 api 调用时,我都会看到

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'lambda'\nRequire stack:\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'lambda'",
    "Require stack:",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1087:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)",
    "    at async start (file:///var/runtime/index.mjs:1282:23)",
    "    at async file:///var/runtime/index.mjs:1288:1"
  ]
}

我尝试删除

serverless.yaml
中包下的排除子句,但由于超出了zip文件大小而无法部署。

typescript aws-lambda nestjs github-actions serverless
1个回答
0
投票

我也面临同样的问题,你能解决这个问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.