How to upload local code to EC2 - node.js

I wanna run my nodejs codes on Amazon EC2.
I use this code to test(using vi to code on 64-bit Amazon Linux)
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(80);
console.log('Server running at http://12.34.56.78/');
and It really works.
But now how can I upload my local nodejs code (in my computer now)to amazon EC2, I use 64-bit Amazon Linux

There's not a unique way to achieve that task. You could use various approaches, each of them with its pro's and con's.
An easy solution would be to use a bare git repository in the server you want to upload the code to, and push your code to that remote repository. You could even use git hooks to automate the deployment and npm install when pushing new code.
One thing I'd recommend you is that, as EC2 instance storage is volatile, you probably should automate the server setup & configuration using something like Opscode's Chef. Either that or implement some incremental backups for your EBS volumes.

You can also use something like fabric. http://docs.fabfile.org/en/1.8/ I've found it very quick to get things done:
from fabric.api import put, run, task
def run_your_app():
run("node js command to run your app")
#task
def put_your_file():
put("localfilename", "remoteFilename")
run_your_app();
Save this to 'fabfile.py' and then your run it from the command line:
fab -H <your hostname or ip> put_your_file
Also more on the fabric operations here:
http://docs.fabfile.org/en/1.8/api/core/operations.html

Related

How to containerize a NodeJS app with cron jobs using Docker?

I have a NodeJS app consisting of a REST API and an overnight maintenance (cron) job. Currently running on Debian Linux.
What is the best practice do Dockerize it?
I can use the official "node" Docker image, however that doesn't contain a crontab.
I can use the official "alpine" Docker image (and install NodeJS in it) however I lose the possibility of upgrading NodeJS with the easy of pulling a new version of the official image.
What is the best way to achieve this?
Solution #1
Use a node cron package. I added some code example from a real project. Every hour I scrape a website. You can set time and place your recurring task inside cron.schedule. You can use it a separate project or combine with your api code.
import { Scraper } from './controller/scraper.js';
import cron from 'node-cron'
cron.schedule('0 0 */1 * * *', () => {
console.log('running a task every hour');
const scraper = new Scraper()
const data = scraper.getData()
});
Solution #2
Docker official website doesn't advice running process managers with containers. But you can use if you need. I use pm2 as a process manager. It can be used for running an app for some intervals and it can restart your app if it crashes.
Links that will help you:
https://pm2.keymetrics.io/
https://pm2.keymetrics.io/docs/usage/docker-pm2-nodejs/
My use case:
Restart app if it crashes.
Run app.js file for each hour.
Use official nodejs docker image and build docker image with your code.
You need to run two containers, one for Rest API and other for cron job.
For cron job you dont need conrtab inside the docker image but schedule the image run on the host machine like below:
This will ensure to run docker image at required interval.
crontab -e
* */2 * * * docker run -it app/cron-image:v1

Create a file on remote server with nodejs

What is the easiest way to create a file on a remote server (can be accessed with ssh) in NodeJS?
Example: I work on my local computer, and there exists a remote server with ip 192.168.1.100. I would like to create an empty text file on this server, in path "/home/users/share".
I tried using some scp library in NodeJS, but could not copy my file to the remoted server.
What do you mean?
If file is created in path, that ssh has access, and rights are set OK, then file be acceseable.
Do you want to generate data on the server and save it?
https://nodejs.org/api/fs.html#fs_fs_writefile_file_data_options_callback
Choose a file from the disk and upload it to the server? eg: https://www.w3schools.com/nodejs/nodejs_uploadfiles.asp
If I am correct, you want to create file on different server using Nodejs and want to access the file using ssh.
If your server has linux then you can use rsync command from terminal.
E.g rsync --chmod=u+rwx,g+rwx,o+rwx /path/to/file server:/path/to/file.
And if you want it done from Nodejs then you can use child_process library to execute all terminal commands.
const { exec } = require('child_process');
exec('rsync --chmod=u+rwx,g+rwx,o+rwx /path/to/file server:/path/to/file', (err, () =>
if (err) {
// node couldn't execute the command
return;
}
}));

How to run jester in Apache server

I want to use jester in production server.
However jester can run only in terminal.app with sync running.
I want to know how to run jester asynchronous like php-pfm.
I wrote this code:
import jester, asyncdispatch, json
routes:
get "/":
resp "Hello World!"
get "/users/#id":
var data = %*{"id": #"id"}
resp $data, "application/json"
runForever()
And now, I run this code:
nim c -r cgi.nim
How can I run this behind an Apache server?
After you compiled (with nim c -r cgi.nim) you should have an executable called cgi. You have to launch this on the background, maybe using something like supervisor.
Then you configure Apache to ReverseProxy towards your cgi nim program, that is listening at http://127.0.0.1:5000 by default.
Here you have a complete tutorial using systemd+watchdog instead of supervisor, and nginx instead of Apache: https://github.com/nim-lang/Nim/wiki/Tutorial:-Creating-a-(micro)-service

SSH to a created EC2 instance through NodeJS implementation

My code looks like below: -
AWS.config.update({ region: 'us-east-1' });
var ec2 = new AWS.EC2();
// Create the EC2 instance
ec2.describeInstances(function (err, data) {
if (err) {
res.status(500).json(err);
} else {
res.status(201).json(data);
}
});
The above code creates the EC2 instance perfectly. Now, my requirement is that I want to "ssh to the created instance" from my NodeJS code programatically. What step should I follow to achieve this. BTW, the whole idea is, once I could ssh to the EC2 instance programatically, the next step I will do is to install Docker and other softwares in that created Instance programatically
Thanks
As long as you have all the necessary information to be able to connect to and authenticate with your EC2 instance via SSH, you could use a module like ssh2 to connect programmatically to execute commands, transfer files, etc.
I think I'm a little late for this question but I also had this problem a few months ago and it really took me a few days to find a solution.
The solution is :
ssh -tt -o StrictHostKeyChecking=no -i "ec2-instance-key.pem" ec2-user#PUBLIC_DNS sh ./shellScript.sh
This line of code connects to the EC2 instance and executes a shell script. You can either run a single shell script which has all the commands you want to execute or execute them via the ssh command.
You'll need the authentication key for the instance , as you can see in the command.
Hope this helps someone someday !

Using NSSM to start a NodeJs process as a windows service is not working

I have seen countless articles on how to use NSSM (http://nssm.cc/) to start a NodeJS process.
So, I have the following simple NodeJS file:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<p>Hello World</p>');
}).listen(8000);
console.log('Server running on http://localhost:8000/');
I am using this command to install the NodeJS file as a windows service:
"C:\Program Files\SimpleNode\nssm.exe" install SimpleNode "C:\Program Files\SimpleNode\node.exe" "C:\Program Files\SimpleNode\simple.js"
The service is installed. When I start it I get an error message, the services is in the Paused state and I see the following error in Event Viewer:
GetProcessTimes() failed: The handle is invalid.
This should be pretty simple. I have tried using a domain account that has local admin rights. I have tried a couple of different port numbers. The app does work correctly when I start it from the command line.
MORE NOTES:
This is running on 64-bit Windows 2008 R2 server. I have made sure I am running all 64-bit executables for both NSSM and Node. I have also tried using 32-bit executables for both.
Can anyone tell me what I am missing? Can someone else replicate this issue?
Found the issue.
The problem is that is that the path to the simple.js file has a space in it (Good Old "Program Files"). You have to escape the quotes with a backslash for NSSM to interpret it correctly. The correct installation command line is:
"C:\Program Files\SimpleNode\nssm.exe" install SimpleNode "C:\Program Files\SimpleNode\node.exe" \"C:\Program Files\SimpleNode\simple.js\"
It sounds like you don't have access to ports for some reason. try setting the service to run as administrator (server Manager>Services>Servicename on windows server 2008) and see what happens.
NSSM usually works fine with Node.js so this is probably a permissions issue. Review this tutorial showing how to setup Node.js with our commercial application to troubleshoot. And feel free to use the 30-day trial too as it it may return a more helpful error message indicating what the problem is.

Resources