How to Automatically Start a Node.Js Application in Amazon Linux Ami on Aws

How can I automatically start a node.js application in Amazon Linux AMI on aws?

One way is to create an upstart job. That way your app will start once Linux loads, will restart automatically if it crashes, and you can start / stop / restart it by sudo start yourapp / sudo stop yourapp / sudo restart yourapp.

Here are beginning steps:

1) Install upstart utility (may be pre-installed if you use a standard Amazon Linux AMI):

sudo yum install upstart

For Ubuntu:

sudo apt-get install upstart

2) Create upstart script for your node app:

in /etc/init add file yourappname.conf with the following lines of code:

#!upstart
description "your app name"

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env NODE_ENV=development

# Warning: this runs node as root user, which is a security risk
# in many scenarios, but upstart-ing a process as a non-root user
# is outside the scope of this question
exec node /path_to_your_app/app.js >> /var/log/yourappname.log 2>&1

3) start your app by sudo start yourappname

Auto start nodejs app on Amazon AMI

you can use User data to do what you want, read http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html

Create new EC2 instance from the console as you'll do normally, when you get to Configure Instance Details page, you'll see a field User data, just enter a shell script to start your node application

You can review the output of the execution at /var/log/cloud-init-output.log

How to run nginx and Node.js with EC2 (linux) server startup

This has nothing to do with autoscaling. It most often has to do with the EC2 AMI (Amazon Machine Image) that the autoscaler is launching your EC2 instances with, and possibly also with the "user metadata" that you are passing to the instance when it launches. These are the only two things that impact what an EC2 instance does when and after it starts up, up until it starts communicating with the outside world.

So what you need to do is create an AMI that is set up so that the right things launch when an EC2 instance is launched from that AMI. What you'd do is take the AMI you want to use as a starting point, launch that AMI into an instance, make the necessary changes and installations you want, and then save off a new AMI. Then change your autoscaling group to launch new instances with that new AMI.

You could alternately send a script in your "user metadata" that launches things, but this is rarely what you want to do". Most of the time, you want to have your AMI do the right thing.

It's also possible that you are using some sort of post-boot provisioner, like Chef, Ansible or Chef Habitat. If you are, that's where you'd set all of this stuff up. You'd want that system to do the work you're describing. But if you're doing that, what I have said earlier still applies. For this to work, you'd often have also built a custom AMI that has parts of the provisioning system already built into it, so that that system can connect into it and provision it. It's possible for these systems to start from a generic AMI as well. It depends on the system.

AWS: Steps to pass a node.js application to EC2

Without going into a lot of detail about automated building procedures, the steps usually go as follows:

  • Build Code
    -- Here, your source code is built and transpiled into a distributable format, which usually goes into a dist/ folder.

  • Upload your distributable code.
    -- Here, all of the files you have built should be uploaded (manually or automatically) to your EC2 instance.

  • Run a startup script
    -- Here, any project startup code should be run in order to actually start your server.

You don't need babel in production because your project should already have been built by that point. However, if you are building on the EC2 instance, instead of just uploading your dist, then you will need it.

In order to turn your EC2 into a routable, reachable web server, you will need to configure some security and routing policies on AWS. You will need to ensure that the instance has a routable IP (or you can use the automatically generated DNS provided by AWS). Secondly, you'll need to ensure that your security policy allows port 80 (at the very least, and any additional ports you need to interact with the server - for HTTPS, SSH or something else.)

Once you have all this in place, you should be good.

EDIT

If you want to serve static HTML pages, you will have to ensure that you have set up your EC2 container as a web server with something like Apache. However, I would recommend that you run your Node Server exclusively from the server and host your static webpack bundle on S3 as a static website.

EDIT 2

  • Here's an introduction to setting up your EC2 instance for node. - https://medium.com/@nishankjaintdk/setting-up-a-node-js-app-on-a-linux-ami-on-an-aws-ec2-instance-with-nginx-59cbc1bcc68c
  • Here's an introduction to setting up a static website with S3. - https://docs.aws.amazon.com/AmazonS3/latest/dev/WebsiteHosting.html

AWS EC2 AMI User Data run PM2

I recommend using pm2 startup command to create init services script

Manual:

https://pm2.keymetrics.io/docs/usage/startup/

Example:

https://futurestud.io/tutorials/pm2-restart-processes-after-system-reboot

How do I enable continuous deployment of Node.js applications to Amazon EC2?

That auto-deploy mechanism is implemented with Git Hooks. The most likely hook used is post-update.

It's a simple bash script that is executed on a git push; put one in a git repository on your EC2 server including the code to re-run NPM (if needed) and restart your code.

That's should do it. :)



Related Topics



Leave a reply



Submit