Running a HTTPS Python Server on EC2 in 5 Minutes
No Docker needed. Just a simplest setup you can get to launching and getting your dopamine hit.
I have come to grown very wary of all the extra layers of technologies (I’m looking at you, Docker) that had compounded onto launching a piece of software on the web. Many will frown upon you if you don’t containerize your app in 2021, but the truth is for most use cases, containers are just a waste of time and headspace compared to just doing it the old-school way.
Here I’m going to walk you through setting up a running HTTPS API server in Python using FastAPI (trust me, Flask is so 2008) and Caddy web server (this one is a total replacement to NGINX and gives you automatic HTTPS, saving you from the pain of setting up TLS certificate).
🤷 Tips: Hold it if you are tempted to Dockerize your app for your first Product Hunt launch or Hacker News show and tell. You’re distracted.
Setting Up
- Launch a t2.micro EC2 instance on AWS. I’ll be using Amazon Linux 2 in this example, but you can stick to Ubuntu if you like.
- Download a .pem keyfile from the instance in order to SSH into it.
- In the instance security group, edit the in-bound rule to open port 80 and 443 to public like this:
4. SSH into the instance with:
ssh -i my-key.pem ec2-user@<your-instance-public-dns-name>
💁♀️ Your EC2 instance public DNS name usually ends with
.compute.amazonaws.com
. Note thatec2-user
is currently a default user name for AWS Amazon Linux 2 instance.
5. Install Caddy web server with
yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy
This installation might run Caddy server process in the background right away. To be sure, run caddy stop
.
6. Optionally, you may need to grant low-numbered port access to non-root binary, which is caddy
in this case. This allows Caddy web server to…