Manuel
Manuel

Reputation: 9522

elastic beanstalk node application within subdirectory

How can I force elastic beanstalk nodejs application to look for the server application within a subdirectory server?

I'm building a modern SPA which has server and client application within one git project. Thus elastic beanstalk must not look for the server application within the root of the repository but within the subfolder ./server/. Client SPA is coded within ./client/ and build into an public folder within the server folder structure during build process. The fully build client version is checked in into git and must not be build while roleout.

So elastic beanstalk must npm install and npm run within the server sub directory.

Example folder structure:

~ git-root
|-- client
|    |-- ...
|
|-- server
|   |-- package.json
|   |-- src
|   |-- public
|   |-- (node_modules)

Upvotes: 12

Views: 2884

Answers (3)

I have a monorepository with NodeJS and Yarn, and I solved this problem this way:

  • In the root, add a package.json, with the following entries:
...
"scripts": {
    ...
    "build": "cd ./$APP_FOLDER && yarn && yarn build",
    "start": "cd ./$APP_FOLDER && yarn start",
  },

Ps: In the subdirectory that contains the actual project, it must have it's own package.json as well

  • In the Environment variables for the Elastic Beanstalk application (configuration -> software -> Environment Properties), add APP_FOLDER as an environment property with the folder name. enter image description here

Ps: I also had some issues building and running the application properly using yarn, so I added two .ebextensions to solve my problem:

  • The first one was responsible to install yarn in the ElasticBeanstalk's machine:

.ebextensions:
--- install_yarn.config
--- post_actions.config

For the file install_yarn.config (to install yarn in the machine):

commands:
  01_install_yarn:
    command: |
      set -e
      npm i -g yarn
      ln -s "$(npm bin --global)"/yarn /usr/bin/yarn
    test: "! yarn -v"
  • I was using CodeBuild to build the code (run yarn build and transpile Typescript to Javascript), but when the built code reached ElasticBeanstalk, it came without node_modules properly installed. So I also added a container_command to reinstall node_modules properly:

post_actions.config

container_commands:
    install_app_with_yarn:    
        command: "ls && yarn && cd ./$APP_FOLDER && yarn"

After doing this, I finally could see the beautiful green light from ElasticBeanstalk's health check:

enter image description here

Upvotes: 0

Kieran
Kieran

Reputation: 951

If your desired package.json is within a subdirectory instead of the recommended root level location, you can add Buildfile and Profile files in your git root dir to respectively provide custom build and run commands to AWS Elastic Beanstalk:

~ git-root
|-- client
|    |-- ...
|
|-- server
|   |-- package.json
|   |-- src
|   |-- public
|   |-- (node_modules)
|
|--Buildfile
|--Procfile

Buildfile

install: cd server && npm install

Procfile

web: cd server && npm run

AWS documentation for Buildfile & Procfile

Upvotes: 2

Paul
Paul

Reputation: 49

Elastic beanstalk is using some server configuration which can be changed. To change that configuration you can ssh to that machine or you can force proper configuration every application deployment.

The second approach is possible with adding custom code into .ebextensions folder placed in the root folder of your app.

More info about custom commands .ebextensions can be found i.e here:

Upvotes: 1

Related Questions