Search code examples
node.jsamazon-web-servicesamazon-elastic-beanstalklerna

How to invoke multiple commands via EB's NodeCommand?


Context

Given a simple Node app on AWS's Elastic Beanstalk (like eb-node-express-sample) and the default nodecommand.config file like this:

option_settings:
  aws:elasticbeanstalk:container:nodejs:
    NodeCommand: npm start

Question

How can I invoke multiple bash commands in place of npm start? I'd love to be able to do the following:

    NodeCommand: echo 'Hello' && npm start

or:

    NodeCommand: |
      echo 'Hello'
      npm start

but it seems that everything after the first word is actually passed in as a single argument, leading to the following output in /var/log/nodejs/nodejs.log:

'Hello' && npm start
'Hello' && npm start
'Hello' && npm start
...

# Logged repeatedly as the server tries and fails to start,
# apparently invoking `echo "'Hello' && npm start"` each time...

Is there any way to get around this silly limitation and directly run multiple bash commands?

Motivation

I have a Lerna app that, rather than having any useful start script in the root directory, has a start script in packages/api/. My particular need here is to be able to change directories before running start, but I'm looking for a generic solution to this problem of running multiple commands.


Solution

  • My workaround is to add a script to package.json:

    "scripts": {
      "start": "node app.js",
      "start:eb": "echo 'Hello' && npm start"
    }
    

    Usage:

    option_settings:
      aws:elasticbeanstalk:container:nodejs:
        NodeCommand: npm run start:eb
    

    In case it's relevant to others, I got here because of an ambiguous 502 Bad Gateway error from nginx due to the server not starting, but absolutely nothing in /var/log/nodejs/nodejs.log. I finally realized it was due to the NodeCommand being mangled by AWS.