If you have your servers generate their own self-signed certificate as part of the deployment container commands, then each server will get an updated certificate every time you deploy and when a new server is started.
The best command I have found for this is the following, which creates certificates valid for 10 years:
sudo openssl req -x509 -newkey rsa:4096 -keyout /etc/pki/tls/certs/server.key -out /etc/pki/tls/certs/server.crt -days 3650 -nodes -subj "/CN=example.com"
Using this approach, as long as you deploy (including upgrading your EB container version) at least once a decade, your servers will stay up.
This drastically simplifies the setup for this as well. Now all you need to do is the following:
- Add a config file to your elastic beanstalk project which generates self-signed certificates and adds HTTPS settings to the web server.
- Have the web server security group accept port 443 connections from the load balancer security group.
- Set your load balancer to forward traffic from port 443 to port 443.
Below is an example of a full HTTPS elastic beanstalk config file for python. This is a slight modification of AWS's suggested config file for python. I've added the generate certificate command to the beginning of container commands and removed the two file statements for /etc/pki/tls/certs/server.crt and /etc/pki/tls/certs/server.key as they are now auto generated. AWS examples for other languages can be found here.
AWS Linux 2, Apache-based deployment
Put the following in .ebextensions/ssl.config:
container_commands:
01_create_certs:
command: |
sudo openssl req -x509 -newkey rsa:4096 -keyout /etc/pki/tls/certs/server.key -out /etc/pki/tls/certs/server.crt -days 3650 -nodes -subj "/CN=example.com"
02_restart_httpd:
command: |
# Condition on whether httpd is running for compatibility with EB worker environments
sudo systemctl status httpd && sudo systemctl restart httpd || echo "httpd not running"
03_wait_for_httpd_restart:
command: "sleep 3"
Put the following in .platform/httpd/conf.d/ssl.conf:
Listen 443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "/etc/pki/tls/certs/server.crt"
SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
# Limit requests to 100 MB
LimitRequestBody 104857600
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://localhost:8000/ retry=0
ProxyPassReverse / http://localhost:8000/
ProxyPreserveHost on
</VirtualHost>
AWS Linux 1, Apache-based deployment
Put the following in .ebextensions/ssl.config:
packages:
yum:
mod24_ssl : []
files:
/etc/httpd/conf.d/ssl.conf:
mode: "000644"
owner: root
group: root
content: |
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
Listen 443
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile "/etc/pki/tls/certs/server.crt"
SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app>
Require all granted
</Directory>
WSGIDaemonProcess wsgi-ssl processes=1 threads=15 display-name=%{GROUP} \
python-path=/opt/python/current/app \
python-home=/opt/python/run/venv \
home=/opt/python/current/app \
user=wsgi \
group=wsgi
WSGIProcessGroup wsgi-ssl
</VirtualHost>
container_commands:
01_create_certs:
command: |
sudo openssl req -x509 -newkey rsa:4096 -keyout /etc/pki/tls/certs/server.key -out /etc/pki/tls/certs/server.crt -days 3650 -nodes -subj "/CN=example.com"
02_kill_httpd:
command: "sudo restart supervisord"
03_wait_for_httpd_death:
command: "sleep 3"