22

Is it possible to have an EC2 instance running, listening on port 443, without a load balancer? I'm trying right now in my Node.JS app but it doesn't work when I call the page using https://. However, if I set it to port 80 everything works fine with http://.

I had it working earlier with a load balancer and route53, but I don't want to pay $18/mo for an ELB anymore, especially when I only have one server running.

Thanks for the help

4 Answers 4

13

You're right, if it's only the one instance and you feel like you don't need to be prepared for large increases in traffic, you shouldn't have to pay for an ELB.

From a high-level standpoint you'll have to go through the following steps:

  1. Install an nginx server to serve your NodeJS application.
  2. Install your SSL certificates on the nginx server.

    -- Either do this manually, ssh'ing into the server and installing the certs as described here.

    -- OR include the necessary files in your application (I believe this only works for elastic beanstalk?) which will overwrite the nginx configuration files automatically as described here.

  3. Make sure nginx is listening on port 443 (should've been completed in the previous step)
  4. Open the EC2 server's security group corresponding to where you want traffic to enter the server (port 80 / port 443)
Sign up to request clarification or add additional context in comments.

3 Comments

Just one catch. When you talk about "your SSL certificate", are you refering to an amazon certificate ? - Is it possible to use an amazon certificate for this ? - Some sources claim that amazon certificates can only be used for AWS load balancers and aws cloudfront
AWS Certificate Manager can generate public and private certs. Public certificates are signed such that they can be publicly verified by clients such as standard browsers. But AWS public certs cannot be exported. They can only be bound to other AWS services (load balancers, etc.) The private certs generated by AWS can be exported, but are not publicly signed. So you can manually install them in your client and server code for secure communications, but a browser will not be able to validate them. More details can be found here: aws.amazon.com/certificate-manager/faqs
Just want to add to this. When Let's Encrypt updates, it updates via port 80. So you have to keep port 80 open as well.
13

Is it possible? Yes of course. It sounds like you had an SSL certificate installed on the ELB and now you've deleted the ELB. You will have to install an SSL certificate on the EC2 server now. You can't use AWS ACM SSL certificates without an ELB or CloudFront distribution. If you don't want to pay for either of those services you will have to obtain an SSL certificate elsewhere.

2 Comments

I have an SSL certificate from Namecheap, and it's uploaded into IAM. When I would set the ELB up, I would just select to use the existing SSL certificate from IAM. How can I install it on my EC2? Do I need to SSH into it or something? @MarkB
Yes, you would have to SSH into the server and manually install the certificate. You won't be able to use IAM to manage the certificate in this scenario.
2

For our projects (much like the other poster described) we used this setup:

  1. nginx as load balancer and proxy for all calls on port 80 (no direct call to node.js server on port 3000 which is closed to the public)
  2. pm2 as process manager for Node.js (and for deployment)
  3. keymetrics.io for monitoring
  4. Nodejs v6.9.3 boron/lts (through NVM)
  5. Mongodb 3.2 with WiredTiger Engine (Compose.io)
  6. Amazon EC2 instances for hosting (Amazon Linux not Ubuntu)

This setup works very well for us. And in this setup we're able to setup SSL without using the amazon load balancers.

2 Comments

But did you use the AWS certificates ? or did you obtain some from a third party ?
Third party. We use letsencrypt.org
1

Once you have your certificate files, it's not so hard. You can even do this without Nginx.

Let's first create an express webserver

const app = express();

For the sake of example, you could put a static website inside a folder.

const wwwFolder = express.static(path.join(__dirname, '/../www'));
app.use(wwwFolder);

Next, yYou basically need to read your certificate files

const key = readFileSync(__dirname + '/ssl/privkey.pem', 'utf8');
const cert = readFileSync(__dirname + '/ssl/cert.pem', 'utf8');
const ca = readFileSync(__dirname + '/ssl/chain.pem', 'utf8');
const serverOptions: https.ServerOptions = { key, cert, ca };

And finally, you create a https server using those certificates.

const server = https.createServer(serverOptions, app);
server.listen(httpsPort, () => log.debug("createWebServers", `server is listening on port ${httpsPort}`));

For security reasons it's probably not possible to listen directly on port 443. Instead, for instance use a port like 4201 and then use port forwarding.

If you use systemd to start/stop your service, then this port forwarding can be defined in your service configuration file. An easy solution:

[Unit]
Description=my.service
After=network.target

[Service]
Type=simple
TimeoutSec=0
User=ubuntu
PermissionsStartOnly=true
ExecStartPre=/sbin/iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 4201
ExecStart=/usr/local/bin/node /home/ubuntu/project/server.js
ExecStopPost=/sbin/iptables -t nat -D PREROUTING -p tcp --dport 443 -j REDIRECT --to-port 4201
Restart=on-failure

[Install]
WantedBy=multi-user.target

There are various ways to create and refresh your certificate files. So, I won't go into detail here about that. But most importantly, you don't need an amazon certificate to accomplish it. LetsEncrypt is free and easy and works fine.

Usually I also add a http server (without HTTPS) and apply a redirect. And then I also use port forwarding for that. So, I add a 2nd port forwarding rule in the service file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.