POST POST

OCT
12
2021

Running Serverless Offline with a Self-Signed SSL Certificate

ORIGINALLY POSTED TO: https://blog.simontimms.com/2021/10/12/serverless-offline-https

If you find yourself in need of running serverless locally using serverless offline and you want an SSL certificate then fear not, it's not all that difficult. First you'll need an SSL certificate. For our purposes you we're going to use a self-signed certificate. This will cause browsers to complain but for local testing it isn't typically a big problem.

Generate a Cert Using OpenSSL

You should install OpenSSL (or one of the more secure alternatives like LibreSSL) and then run

1
openssl req -newkey rsa:2048 -nodes -keyout key.pem -x509 -days 365 -out cert.pem

This will prompt you for a bunch of information about your organization. You can put anything you want in most of those fields but do pay attention to the Common Name field which needs to hold the value of localhost.

These are the answers I gave

1
2
3
4
5
6
7
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:TX
Locality Name (eg, city) []:Austin
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Inventive
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:

You should now have a cert.perm and a key.pem in your local directory. Copy these into a cert folder at the root of your serverless project.

Running with the Certificate

Now you need to tell serverless where to find your certificate. You can either run with the flag

1
2
3
4
--httpsProtocol cert
```

or update your `serverless.yml` to include the cert directory

custom: serverless-offline: httpsProtocol: "cert" ...


## Gotchas

If you're seeing a warning about an invalid certificate then check that you're accessing serverless via `localhost` and not `127.0.0.1` or `0.0.0.0`. SSL works with domain names so you need to use one, even if it is just `localhost`.

Simon Timms

Email Email
Web Web
Twitter Twitter
GitHub GitHub
RSS

Looking for someone else?

You can find the rest of the Western Devs Crew here.

© 2015 Western Devs. All Rights Reserved. Design by Karen Chudobiak, Graphic Designer