在Elastic Beanstalk上部署NestJS应用程序 [英] Deploying NestJS application on Elastic Beanstalk

查看:77
本文介绍了在Elastic Beanstalk上部署NestJS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将NestJS应用程序部署到AWS Elastic beantalk,但没有成功,有人可以一步一步地写到我如何实现这一目标吗?

I am trying to deploy my NestJS application to AWS elastic beanstalk, but did not had any success, can someone please write step by step how can I achieve that?

完整说明:

我有一个带有typeorm的nestjs应用,但未将其配置为可与RDS一起使用,因此我们暂时将其保留(也许有一个连接,idk).

I have a nestjs app with typeorm but didn’t configured it to work with RDS, so we will leave it for now (maybe there is a connection, idk).

首先,我创建了一个CodePipline,当我将新版本推送到github仓库时,它将自动将整个仓库部署到可在节点12.x上运行的eb实例.

First of all I made a CodePipline that when I am pushing new version to my github repo it automatically deploying the whole repo to my eb instance that works on node 12.x.

现在,我希望每次git push时,实例都将安装依赖项,构建嵌套应用程序,并从/dist/main.js启动服务器.

Now, I want that on every git push, the instance will install the dependencies, build the nest app, and start the server from the /dist/main.js.

我添加了一个具有以下内容的Procfile:

I have added a Procfile with:

web: npm install && npm run build && npm run start:prod

我还在main.ts上配置的EB上添加了PORT环境变量,当找不到该端口时使用8080.

I have also added PORT environment variable on EB that configured on main.ts and when not found to use 8080.

我的package.json脚本就像一个新创建的嵌套应用程序:

And my package.json scripts are like a newly created nest app:

  "scripts": {
    "prebuild": "rimraf dist",
    "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"
  }

在部署时,我在日志中看到它做了类似的事情:

When deploying I see in the logs that it did something like this:

Jan 23 20:01:14 web: > app@0.0.1 start /var/app/current
Jan 23 20:01:14 ip-172-31-17-171 web: > sudo npm i -g @nestjs/cli && node dist/main
Jan 23 20:01:14 web: We trust you have received the usual lecture from the local System
Jan 23 20:01:14 web: Administrator. It usually boils down to these three things:
Jan 23 20:01:14 web: #1) Respect the privacy of others.
Jan 23 20:01:14 web: #2) Think before you type.
Jan 23 20:01:14 web: #3) With great power comes great responsibility.
Jan 23 20:01:14 web: sudo: no tty present and no askpass program specified
Jan 23 20:01:14 web: npm ERR! code ELIFECYCLE
Jan 23 20:01:14 web: npm ERR! errno 1
Jan 23 20:01:14 web: npm ERR! app@0.0.1 start: `sudo npm i -g @nestjs/cli && node dist/main`
Jan 23 20:01:14 web: npm ERR! Exit status 1
Jan 23 20:01:14 web: npm ERR!
Jan 23 20:01:14 web: npm ERR! Failed at the app@0.0.1 start script.

所以输入env网址时得到502.

So I am getting 502 when entering the env url.

那么也许这是npm的权限问题?或者我只需要以某种方式部署/dist文件夹?您认为问题是什么?

So maybe it is a permission issue with npm? Or I need to deploy on some way only the /dist folder? What do you think the problem is?

这是我第一次尝试将后端服务器部署到eb:)

It is my first time trying to deploy a backend server to eb :)

推荐答案

  • 一段时间以来,我一直难以将nestjs应用程序部署到AWS Elastic beantalk中,因此我决定在此处进行指导.我在这里逐步介绍了部署指南,以部署简单的应用程序.

    • I got stuck with the deploying the nestjs app to AWS elastic beanstalk for a while so that I decide make the guide here. I make the deploy guide step by step here for deploying the simple app.

      为什么通常部署会失败,因为它不知道NEST依赖项(不安装dev依赖项)和Typescript代码,这些代码需要在上传到服务器之前进行编译.

      The reason WHY the deploying is usually FAILED because it does not know NEST dependencies (it does not install dev dependencies) and Typescript code which need to compile before uploading to server.

      就我而言,使用压缩方式来制作zip文件(您可以使用命令行 eb ,但为简单起见,请尝试首先使用它).

      In my case, Use the compression for making the zip file (you can use the command line eb but try to use this first for simple).

      首先,准备好zip文件,这对于避免部署时出现FAILED非常重要.

      Firstly, Preparing the zip file, this is really IMPORTANT for avoiding the FAILED while it is deploying.

      • 您需要创建 dist 文件夹.为此,我们首先删除dist文件夹,然后运行命令 nest build (*这是为我们编译Typescript而执行的 tsc ).
      • 现在,我们有了 dist 文件夹,接下来需要做的是将package.json(*这个为我们安装了DEPENDENCIES的文件)复制到 dist 文件夹,以使服务器知道安装的依赖关系,而我们没有在此处添加 node_module .
      • 在package.json中,为 nest start 运行 yarn start ,但AWS不知道 Nest命令,因此我们需要将其指向通过使用 node main.js 删除(此文件位于dist文件中,您需要正确创建main.js的路径).为此,我们创建了 Procfile (参考
      • You need to make dist folder. For doing this, we remove the dist folder first and run the command nest build (* this one do the tsc for compile Typescript for us).
      • Now, we have the dist folder, the next thing we need to do is copying the package.json ( * this one installing the DEPENDENCIES for us ) into dist folder to let server know installing the dependencies that we do not bring the node_module here.
      • In package.json, yarn start to run nest start but the AWS does not know Nest command so that we need to point it out by using node main.js (this file is in dist file, you need to make the path to main.js correctly). For doing this, we create the Procfile (reference this) and add the conntent is web: node src/main.js. Remember copying it into the dist file.
      • Compressing file correctly as this

      其次,从AWS控制台创建应用程序或环境,然后上传zip文件并获得成功状态.如果不是,请检查日志文件以了解其失败原因.也许不是 nest命令等...

      Secondly, from AWS console creating the application or environment then uploading the zip file and get successful status. If not, check the log file to know why it failed. Maybe it does not the nest command, etc...

      希望本指南对您有帮助.

      Hoping this guide will help you.

      加油!

      这篇关于在Elastic Beanstalk上部署NestJS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆