Deploy your server to AWS EC2

You developed a backend server and wanted to deploy it, but the main question is how to keep it the most scalable and responding, that’s why AWS might came to your mind as a leader in the software scalability world.

One of the deployment AWS services is EC2 (Elastic Compute Cloud), it’s about a scalable and easy to use container instance that you control to start, restart and stop anytime you want smoothly.

In our case we’ll deploy a sample Nodejs server, so let’s get started 😊

Create a sample Nodejs Server :

To create our Nodejs server, we’ll use Express as a fast nodejs micro-framework

Start by creating a folder and name it sample_nodejs_server

then let’s create our root file : index.js, if you’re on Linux or MacOS, you can do this with the terminal :

Once you create the repo and the root file, go and initialize it with npm as an npm project :

Now let’s install express :

Then we’ll create a sample api service that says “Hello World”

That’s all for our Server, now let’s go create our AWS EC2 instance, go to your AWS console, find and click on the EC2 service

And click on “Instances” :

Click on “Launch instances” :

Next, choose your amazon machine image, it’s your VM based core image, in our case we’ll go for Ubuntu as AMI (Amazon Machine Image) :

Next step is to choose our Instance type, it’s about the instance capacities such as CPU, Memory and Instance data I/O Storage, be careful here cuz it depends on your need not all of types are included in the free tier and you might need more capacities

Then you’ll need to configure the rest of your instance’s details like the number of instances, subnet and IAM role (you’ll need this one if you wanna add some specific access privileges for your users), you can skip all this by default but one option is important and you’ll need it later is “enable CloudWatch detailed monitoring”, this will give you the whole details of your instance further state

Next, you can add more storage to your instance and device settings, but we’ll leave by default 8GiB with SSD and no encryption :

Then if you wanna add tags to your instance to identify it and give it among many others, but we’ll leave it without

The next step is the most important, cuz here we’ll define your security group i.e : the access types to your instance like if the access if public or has been routed with specific configuration :

As you can see above, we’ve added the HTTP and HTTPS access, but also at the last line a custom access from “anywhere” on the port “4000” cuz our server will be running on this port.

At last, review and launch your instance :

Once you click on “Launch” button, a pop-up will show up to ask you to create a key-pair, you’ll need this one to access your instance later, so just choose “create a new key-pair” , download it then click on “Launch Instances”.

This will take just few seconds to create your instance :

Now just click on your instance Id as in the image below to see your instance running :

Now you need to connect remotely to your instance to deploy your server, navigate to your instance summary and click on “Connect

Then just follow the instructions below :

In our case we’ll just open terminal and type :

where “medium_art_test.pem” is the key-pair that we generated, so be sure you put the right path to the file, and “ubuntu@ec2–3–90–17–45.compute-1.amazonaws.com” is our instance :

Now let’s bring in our nodejs server repo, so as you might guessed our repo has to be somewhere remotely accessible like on Github, so let’s just clone it :

After this, you’ll need to install nodejs on this instance to be able to run your server, but before this, you’ll need to update our AMI Ubuntu packages( this would be instinctive if you’re a Linux user), so just type :

Then you can install nodejs :

Be sure that “npm” was installed, otherwise you’ll have to do it as well :

Now just navigate to your folder and type :

Finally, start your server with the legendary command :

That’s 😉 .. To check if your server is well deployed, go to your AWS console, “Connect to instance” section and copy your instance public IP address

And paste it in a new browser tab with the port that you defined in your security group (in our case, it was the port 4000), so it’s like :

Voilà …

One last thing left, our server is running but it’s not permanently once we close the ssh connection, the server will stop, so if you wanna keep it running permanently then you’ve to use “pm2” a JS Nodejs processes manager, you can install it with :

Then run your project with “pm2 start index.js” instead of “node index.js” :

Abrakadabra 😉🧙‍♂️ .. our server is up and permanently on our instance, you can stop it with :

As you might noticed, AWS EC2 works as a VM, you can deploy servers as much as your instance performs, and to stop it all, you’ve just to stop your instance :

N.B 🚨: Remember, every time you stop and restart your instance, its public IP address changes, so be sure to have the right connection.

That’s all folks, hope you enjoyed it, keep discovering 😊😉

an IT lover , a curious full-stack developer