Deploying to Rented Infrastructure
Why the API moves from a dev workspace to a cloud virtual machine, and the IaaS vocabulary that comes with it: instances, images, instance types, key pairs, SSH, public addresses, security groups, and a continuous-deployment job that ships code on every push.
Why Leave the Dev Workspace?
Until now the API has run inside the cloud development environment. That’s ideal for writing code and wrong for serving users:
- Dev workspaces sleep when idle and stop when you close them.
- Their URLs and resources are tied to a developer’s account.
- They’re sized and configured for editing, not for steady traffic.
Production code needs a machine that is always on, configured deliberately, and controlled by the team. The movie API moves to Amazon EC2 (Elastic Compute Cloud), AWS’s virtual server service — the IaaS tier from Module 1. You rent the virtual machine; everything from the operating system up is yours to manage and secure.
The IaaS Vocabulary
| Term | Meaning |
|---|---|
| Instance | One virtual machine running in the provider’s data center |
| Image (AMI) | The template the instance boots from — here, an Ubuntu Linux image |
| Instance type | The hardware size: CPU, memory, network. Small burstable types (like t2.micro) suit light workloads and free tiers |
| Region / Availability Zone | The geographic area and the isolated data center within it where the instance lives |
| Key pair | A public/private key pair for logging in. AWS keeps the public key on the instance; you download the private key once, as a .pem file |
| Public IPv4 address / DNS name | How the internet reaches the instance. By default these change when the instance is stopped and started |
| Security group | A virtual firewall attached to the instance |
SSH and key pairs
SSH (Secure Shell) is an encrypted protocol for logging in to a remote machine and running commands. Rather than a password, the movie deployment uses public-key authentication: the instance holds the public key, and whoever holds the matching private key can prove their identity. The .pem file is that private key — PEM (Privacy-Enhanced Mail) is just the text format it’s stored in. Possessing this file is equivalent to being able to log in, so it’s one of the most sensitive files in the whole project.
Security groups
A security group is a stateful firewall: you write inbound rules stating which ports accept traffic from which sources, and responses to allowed traffic are automatically permitted back out. Each rule has a protocol, a port, and a source written in CIDR notation — a specific address like 203.0.113.7/32, a range, or 0.0.0.0/0 for everyone.
The lab setup opens port 3000 (and port 80 for HTTP) to the whole internet so a phone anywhere can reach the API. That’s the trade-off to notice: every open port is attack surface, and SSH in particular should never be open to everyone.
Stop vs. terminate
Stopping an instance shuts it down but keeps its disk, so you can start it again (with a new public address unless you’ve reserved a static one, called an Elastic IP). Terminating deletes the instance and, by default, its disk — permanently. Idle instances still cost money, and forgotten ones are a classic source of surprise bills and unpatched servers.
Continuous Deployment with a Deploy Job
Module 4’s pipeline gains a third job. It declares needs: build_test, so it runs only if the tests pass — the essence of safe continuous deployment.
- 1 Tests pass
needs: build_test
- 2 Write .env
from repository secrets
- 3 Load SSH key
EC2_SSH_KEY secret
- 4 Copy code
scp to the instance
- 5 Remote commands
install · stop old · start new
The secrets involved — MONGODB_URI, EC2_SSH_KEY (the private key’s contents), and EC2_PUBLIC_DNS — live in GitHub’s encrypted secret store, never in the repository.
On the instance, the remote commands are worth understanding conceptually:
- Idempotent setup — install Node.js, npm, and tools only if missing, so the same script works on a fresh instance or an existing one.
- Stop the old version — kill whatever is listening on port 3000; two processes can’t bind the same port.
- Start the new version in the background —
nohupkeeps the server running after the SSH session ends, with output sent to a log file.
This is a minimal deployment, good for learning the moving parts. Production systems usually go further: a process manager (like systemd or PM2) restarts the app if it crashes, a load balancer spreads traffic across several instances, and an auto scaling group adds or removes instances with demand. A single EC2 instance on its own doesn’t scale automatically — those services do.
Common mistake: leaving the downloaded .pem key inside the project directory, where it’s one git add . away from being committed.
A committed private key gives anyone who sees the repository shell access to your server. Store keys outside the project, restrict their file permissions, keep them in the CI secret store for pipelines, and if one leaks, replace the key pair — deleting the commit isn’t enough. Also restrict port 22 to known addresses; better still, use the cloud provider’s browser-based or session-manager access so SSH needn’t be exposed at all.
1. What does a security group's inbound rule with source 0.0.0.0/0 on port 22 mean?
0.0.0.0/0 matches every IPv4 address. For SSH, restrict the source to trusted addresses.
2. Why does the deploy job declare needs: build_test?
The dependency gates deployment on passing tests — failing code never reaches the server.
3. After stopping and restarting an EC2 instance, the deploy job can no longer reach it. What's the most likely cause?
Without an Elastic IP, public addresses change on stop/start, so secrets that reference them must be updated.
4. Which statement about scaling is accurate?
An instance is one machine. Elasticity comes from adding services that distribute traffic and manage instance counts.
Up Next
The API is running on EC2 — yet the phone still can’t reliably reach it. The next lesson explains why: NAT, ports, HTTPS requirements on mobile platforms, and how a tunnel gets around them.