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 -
Now install certbot
snap install --classic certbot
Check the cerbot installed or not
certbot --version
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
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);
Now our Node server can handle https requests
Top comments (1)
Very good tutorial, thank you!