Expert · Deployment and scaling · topic 49 of 54 · 30 min
Deploying to the cloud
Explanation
What "the cloud" is
The cloud is other people's computers in big buildings called data centres, rented over the internet. Instead of buying a server for lakhs of rupees, you rent one by the hour and can switch it off any time. Cloud companies also rent ready services: databases, file storage, queues and more, so you do not have to run them yourself.
| Model | You manage | Provider manages | Examples |
|---|---|---|---|
| IaaS (Infrastructure as a Service) | OS updates, runtime, app, scaling | Hardware, network, data centre | AWS EC2, Google Compute Engine, Azure VMs |
| PaaS (Platform as a Service) | Your code and settings | Servers, OS, runtime, often HTTPS and scaling | Render, Railway, Fly.io, Heroku, Google App Engine |
| Serverless functions | Small functions | Everything else; runs only when called | AWS Lambda, Google Cloud Run functions, Azure Functions, Vercel Functions |
The big clouds and their Indian regions
A region is a group of data centres in one area. Picking a region near your users makes the app faster, and some Indian rules (for example the RBI rule on payment data) require certain data to be stored in India.
| Cloud | Regions in India |
|---|---|
| AWS (Amazon Web Services) | Mumbai (ap-south-1), Hyderabad (ap-south-2) |
| Google Cloud | Mumbai (asia-south1), Delhi (asia-south2) |
| Microsoft Azure | Central India (Pune), South India (Chennai), West India (Mumbai) |
For a first project, simpler platforms like Render, Railway or Fly.io are often easier: connect your GitHub repo, set environment variables, and they build and run it with HTTPS. Use a managed database too (for example Amazon RDS, Google Cloud SQL, Neon or Supabase for Postgres): the provider handles backups, updates and failover, which are hard to do well yourself.
Domain, DNS and HTTPS
A domain is your name on the internet, like api.example.in, bought from a registrar for a yearly fee. DNS (Domain Name System) is the internet's phone book: it turns that name into the address of your server. You add records at your DNS provider: an A record points a name to an IPv4 address, a CNAME points a name to another name (common with platforms), and a TXT record holds text used to prove you own the domain.
dig +short api.example.in # which address does this name point to?
sudo certbot --nginx -d api.example.in # get a free Let's Encrypt certificate for nginx
curl -I https://api.example.in/healthz # check HTTPS works and see the response headersHTTPS encrypts traffic between users and your server so nobody on the Wi-Fi can read passwords or OTPs. It needs a certificate. Let's Encrypt gives free certificates that last only a short time (90 days, and planned to get shorter), so renewal must be automatic. Certbot sets up that renewal for you.
Environment variables in production
In production, settings like DATABASE_URL, REDIS_URL and payment keys come from environment variables set in the platform's dashboard or a secret manager, never from a file in git. Check at startup that every required variable is present, and stop with a clear message if one is missing. Failing fast at boot is far better than failing on the first real payment.
Zero-downtime deploys
- Start the new version next to the old one.
- Wait until its health check (for example GET /healthz) answers 200.
- Move traffic to the new version, a few servers at a time (a rolling deploy) or all at once to a ready copy (blue-green).
- Stop the old version only after the new one is healthy. If the new one fails, traffic never moves, and rollback is instant.
- Make database changes backward compatible: first add a new column, deploy code that uses it, and remove the old column in a later release.
Watch the lesson
Deploying to the cloud · 1/7
The cloud means renting computers and services by the hour instead of buying them.
Prefer a video by a teacher? Find free videos on “how to deploy node js api to cloud for beginners” on YouTube ↗
In real life: Launching a coaching institute's test series app
A developer in Kota builds an online test series API. For launch she picks a PaaS in the Mumbai region close to her students, a managed Postgres with daily backups, and a domain with a CNAME pointing to the platform, which issues the HTTPS certificate automatically. DATABASE_URL and the payment keys are set in the dashboard, and the app refuses to start if one is missing. Each deploy starts the new version, waits for /healthz, then switches traffic, so students taking a test at 9 pm never see an error. A budget alert at 3,000 rupees a month tells her early when usage grows.
Practice (0/3 done)
Fail fast at startup. Write missingEnv(env, required) that returns the names in required that are missing from env or set to an empty string (or only spaces), in the same order as required. In a real app you would pass process.env.
Estimate a monthly bill. Write estimateMonthlyCost(usage, prices). usage = { servers, hours, storageGb, egressGb }. prices = { serverPerHour, storagePerGb, egressPerGb, freeEgressGb }. compute = servers x hours x serverPerHour. storage = storageGb x storagePerGb. transfer = (egressGb minus freeEgressGb, but never below 0) x egressPerGb. Return { compute, storage, transfer, total }, each rounded to 2 decimal places with Math.round(x * 100) / 100. All prices are in rupees and made up for practice.
Write your own deploy checklist for a Node.js API going to production for the first time. It should be short enough to follow every time.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.