DEV Community

Cover image for Set Up SSL In NodeJS API With Certbot & AWS EC2 Server
Ashirbad Panigrahi
Ashirbad Panigrahi

Posted on • Updated on

Set Up SSL In NodeJS API With Certbot & AWS EC2 Server

Before you start

You have to make sure nodejs api cloned into aws ec2 instance and you already assigned a elastic ip to it.

So how can we add SSL?

We are going to use certbot and letsencrypt for creating SSL certificate of our NodeJS server, so connect with EC2 instance & let's start with following steps

Install Certbot on AWS EC2 linux Server

First of all run the command so that you will become the root user & you don't have to use the sudo command over & over

sudo su -
Enter fullscreen mode Exit fullscreen mode

Now install certbot

snap install --classic certbot
Enter fullscreen mode Exit fullscreen mode

Check the cerbot installed or not

certbot --version
Enter fullscreen mode Exit fullscreen mode

Let's Request for a SSL certificate

Before applying for SSL replace the A record of your domain/subdomain with the elastic ip that assigned to the EC2 instance

certbot certonly --standalone -d domain-or-subdomain.in
Enter fullscreen mode Exit fullscreen mode

Let's use these generated SSL certificates

Now open the main file where you start the server & modify

const fs = require('fs');
const https = require('https');
const express = require('express');

const app = express()

const options = {
  cert: fs.readFileSync('/etc/letsencrypt/live/domain-or-subdomain.in/fullchain.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/domain-or-subdomain.in/privkey.pem')
};

https.createServer(options, app).listen(443);
Enter fullscreen mode Exit fullscreen mode

Now our Node server can handle https requests

Top comments (1)

Collapse
 
verdev profile image
Giacomo Verde

Very good tutorial, thank you!