Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

amazon web services - Deploying NestJS application on Elastic Beanstalk

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?

Full explanation:

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).

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.

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.

I have added a Procfile with:

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

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

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: > [email protected] 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! [email protected] 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 [email protected] start script.

So I am getting 502 when entering the env url.

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?

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

question from:https://stackoverflow.com/questions/65864374/deploying-nestjs-application-on-elastic-beanstalk

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • 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.

  • 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.

  • 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).

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

    • 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
  • 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.

Cheer!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...