Skip to content

Instantly share code, notes, and snippets.

@mohanpedala
Created March 21, 2019 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohanpedala/ca00a5f4e8cd35aac1684ecc805f3c90 to your computer and use it in GitHub Desktop.
Save mohanpedala/ca00a5f4e8cd35aac1684ecc805f3c90 to your computer and use it in GitHub Desktop.
Sample nodejs app
Getting the Node.js Application
For this first application, we’re going to be using a multi-application Node.js system that we’ll need to pull from GitHub. The first thing that we need to do is install Node.js itself:

[root] $ curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
[root] $ yum install -y nodejs
Once Node.js is installed, we’re going to clone our repository and run the multiple applications required:

[root] $ mkdir /srv/www/
[root] $ cd /srv/www/
[root] $ git clone https://github.com/CloudAssessments/s3photoapp
[root] $ cd s3photoapp
[root] $ make install
Before we can start our applications, we need to configure our AWS credentials.

Configuring AWS Credentials
We need to have access to S3 and DynamoDB, so we’ll create a user in AWS IAM that has the following permissions:

AmazonS3FullAccess
AmazonDynamoDBFullAccess
Once we’ve created the user, we’ll need to utilize the ACCESS_KEY_ID and SECRET_ACCESS_KEY values in some of the services we’ll create.

Running Node.js Services
Before we can get back to configuring NGINX, we’re going to set up systemd services for all three of the Node.js applications. The photo-filter has the simplest configuration:

/etc/systemd/system/photo-filter.service

[Unit]
Description=photo-filter Node.js service
After=network.target

[Service]
Restart=always
User=nobody
Group=nobody
Environment=NODE_ENV=production
ExecStart=/bin/node /srv/www/s3photoapp/apps/photo-filter/server.js

[Install]
WantedBy=multi-user.target
Both the photo-storage and web-client services need additional environment variables for interacting with AWS:

/etc/systemd/system/photo-storage.service

[Unit]
Description=photo-storage Node.js service
After=network.target

[Service]
Restart=always
User=nobody
Group=nobody
Environment=NODE_ENV=production
Environment=AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
Environment=AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
ExecStart=/bin/node /srv/www/s3photoapp/apps/photo-storage/server.js

[Install]
WantedBy=multi-user.target
/etc/systemd/system/web-client.service

[Unit]
Description=S3 Photo App Node.js service
After=network.target photo-filter.target photo-storage.target

[Service]
Restart=always
User=nobody
Group=nobody
Environment=NODE_ENV=production
Environment=AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID
Environment=AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
ExecStart=/srv/www/s3photoapp/apps/web-client/bin/www

[Install]
WantedBy=multi-user.target
Finally, let’s start and enable our services:

[root] $ systemctl start photo-storage
[root] $ systemctl enable photo-storage
[root] $ systemctl start photo-filter
[root] $ systemctl enable photo-filter
[root] $ systemctl start web-client
[root] $ systemctl enable web-client
In the next lesson, we’ll investigate how to configure NGINX as a reverse-proxy in front of our Node.js applications.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment