1 of 19

uh what is pm2?

(and a bonus guide on how AMI/Templates)

a persontaton by danny wong

slightly updated bc SDC is over and we kinda don’t need the last half of this presentation (still included in case anyone wanted to reference it !)

2 of 19

3 of 19

Some cool features

  • Keeps your node app running even after you exit out of the SSH session
  • Node apps can run in the background without a terminal open
  • Allows for auto restart of your node app after a crash
  • A task manager like interface directly in the CLI to view information for each app running
  • Treat it like a server to serve static files which then you can use something like nginx to be a reverse proxy for your localhost

If we had better instances, we could also leverage multiple CPU cores to load balance for us with PM2’s cluster mode (but t.2 micro is sad and only has 1 CPU core)

4 of 19

  • Make sure you have npm installed on your system of course!
  • In your CLI, run npm i -g pm2 , this will install pm2 globally.
  • Once you have that installed, cd into the root directory of the app you want to run
  • Run the command pm2 start npm —name “name of app” — start and this should start up your node app
  • In order to confirm it is running or check your list of running apps, you can check with pm2 list or pm2 l

Very simple install

5 of 19

pm2 show (process number)

shows some useful information about that selected process

6 of 19

you can also use pm2 monit to bring up this cool CLI that shows you all the apps you have running and monitor each of their logs

7 of 19

Now how can we use this for SDC????

  • Keep your node app running after you close SSH
  • Restarts your app if it ever crashes
  • Starts up the app without having to SSH back into the instance if you ever need to reboot your EC2 instance (and also a pretty cool thing later after this is set up)

8 of 19

So how do we accomplish that last point from that last slide you may ask??

So in order to have PM2 startup when the EC2 instance starts(boots up)(whatever), we need to do the following

  • I hope u followed the steps in the earlier slide
  • Run pm2 startup in your CLI, this will generate a command for you to run in order to create the startup script.

The following slide will be what it looks like after you run that command

9 of 19

10 of 19

  • The command created the script AND started up the pm2-ubuntu.service
  • Now run pm2 save to save the current list of processes that need to be run when your EC2 instance boots up
  • To check if your service is running, you can use the following command to check systemctl status pm2-ubuntu.service
      • IMPORTANT: Don’t freak out, the process will say inactive(dead) for now, you just have to do a sudo reboot to get the service running for the first time after installation.
        • You WILL have to wait for the EC2 instance to fully reboot in order to SSH back in to check so just give it a minute or two

11 of 19

Here’s that pretty cool thing I mentioned earlier

So if we want to create more EC2 instances to add to our load balancer, we wouldn’t want to manually go and set up the servers every time right?

12 of 19

Here’s a mini tutorial on how to create an image from an existing EC2 instance

(basically copy pasting your entire base EC2 instance)

13 of 19

  • Go to your instances portal on AWS
  • Right click your server you want to create an image of
  • Go down to image and templates
  • Click on Create Image

14 of 19

  • Name it something
  • Anything really
  • And then click create image at the bottom

15 of 19

Now we can use this in conjunction with Launch Templates

Hit that button

16 of 19

  • Fill out the name
  • Choose the image you want to launch with
  • Hit create launch template
    • The rest of the settings below this screenshot are all of the same settings you set for the chosen instance so the .pem key, security group, etc will all be the same

17 of 19

  • Go to launch an instance like how you usually would but this time click “Launch instance from template”
    • It might not work the first time if you immediately tried to create an instance based on a fresh image
    • That image just hasn’t processed all the way yet so give it a minute or two to do so before creating your new EC2 instance

18 of 19

Congrats! You’ve now created a carbon copy of your first instance

No need to set up node/npm or do git clone to set up the server

The even cooler thing now, is that because we set up pm2 to auto start our node app AND set up pm2 to start up as a service when the instance first boots, there’s no need to SSH into the instance AT ALL to get anything set up! (asterisk)

You can simply grab the URL from your AWS portal and add it to your nginx loadbalance config file! (make sure to include the port if you didn’t do port-forwarding!! )

asterisk: this is assuming you set everything up you may have needed already.

i.e.

  • uncapping your ulimit permanently so your system can open more files
  • Maybe setting up the monitoring service of your choice
  • Anything else you can think of ?

19 of 19

It will name it the same name as your template instance so make sure to change it later so you can easily identify your new instances