In the previous part of this blog, we have learnt to create and SSH to an EC2 instance. In this part, we will setup and run a NodeJS server on it 😎
Let's split the work into 3 smaller tasks. First of all, we need to configure our instance to accept traffics coming to Node server's port. Next, after installing
node, we will clone the source code and get the server running.
Let's add another security group for this purpose. This is pretending that our Node server will run on port 3000.
MyInstance: ... SecurityGroups: ... - !Ref NodeServerGroup ... NodeServerGroup: Type: AWS::EC2::SecurityGroup Properties: GroupName: NodeServer GroupDescription: Allow traffics to instance in port 3000 SecurityGroupIngress: - CidrIp: 0.0.0.0/0 FromPort: 80 IpProtocol: tcp ToPort: 3000
First, we need to SSH to the instance:
ssh -i <pem-key-name.pem> ec2-user@<public_ip_v4>
If the instance asks you to run any updates, go ahead and do so:
sudo su sudo yum update
Next, we need to install
nvm in our instance:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash . ~/.nvm/nvm.sh
Let's verify if
nvm has been successfully installed:
We could then install
nvm install node ... node -v
Let's make a Node Express app and push it to GitHub so that we could clone the source code to our instance. I have made a dead simple one that you might use as a reference: vinhlee95/express-ec2. Keep in mind that you will have to create your own repository to add SSH key generated in EC2 instance to your settings.
We could do that by this command:
After following the instructions (remember to note down the passphrase), you should have a SSH key in place. Let's get it and add to GitHub settings:
Your newly generated SSH key will be shown in the Terminal. Copy it and and it in your GitHub SSH settings (Settings -> SSH and GPG keys -> New SSH key).
After having the SSH key saved, you should be able to clone your application's source code to EC2 instance:
git clone firstname.lastname@example.org:your-repo.git
After it is done, you should see application's code under your current directory:
Let's first install all of dependencies:
We will use pm2 - a Node process manager to start our server. Let's install it:
npm install -g pm2 ... pm2 -v pm2 start app.js
Our server should be running now 🥂 Let's show server's logs in the terminal:
Cheers! Things are working now. Let's test it by open the instance's public DNS in the browser! You could get the URL in Description section in the bottom of AWS EC2's console. Remember that we need to specify our server's running port in the URL as well.
Note that you might want to delete CloudFormation stack after done playing around to avoid any extra charge from AWS 😉
- Configure EC2's instance Inbound rules to accept traffic from server port.
nodein the instance
- Use Git to clone source code to the instance. In order to do that in the Linux instance, we need to:
- Generate SSH key in the instance and add it to GitHub settings
- Install Git and clone the repo
- Install app's dependencies along with PM2
- Run the Node.js server