Why This Matters
You can build the most elegant, scalable system in the world -- but if it costs $50,000 per month to run when your revenue is $10,000, the system has failed. Cost modeling is the practice of estimating what your infrastructure will cost before you build it, and continuously optimizing those costs as your system grows.
Cloud providers charge for compute, storage, network transfer, and managed services. Every architecture decision has a price tag: choosing a larger database instance, adding more servers, enabling a CDN, or using a managed service instead of self-hosting. Engineers who understand cost modeling make better design decisions. They know when a $20/month managed service saves $2,000/month in engineering time, and when a "free" open-source tool costs more in operational overhead than a paid alternative. Cost optimization is not about being cheap -- it is about spending wisely.
Define Terms
Visual Model
The full process at a glance. Click Start tour to walk through each step.
Cost modeling is iterative: estimate, calculate, optimize, and monitor continuously.
Code Example
// Cloud cost calculator
class CostCalculator {
constructor() {
// Simplified pricing (per month)
this.pricing = {
compute: { small: 20, medium: 80, large: 160 }, // per instance
storage: 0.023, // per GB
transfer: 0.09, // per GB outbound
database: { small: 50, medium: 200, large: 800 }, // managed DB
};
}
estimateMonthlyCost(config) {
const compute = this.pricing.compute[config.instanceSize]
* config.instanceCount;
const storage = this.pricing.storage
* config.storageGB;
const transfer = this.pricing.transfer
* config.transferGB;
const database = this.pricing.database[config.dbSize];
const total = compute + storage + transfer + database;
return {
compute: Math.round(compute * 100) / 100,
storage: Math.round(storage * 100) / 100,
transfer: Math.round(transfer * 100) / 100,
database: Math.round(database * 100) / 100,
total: Math.round(total * 100) / 100,
};
}
compareConfigs(configs) {
return configs.map((cfg, i) => ({
name: cfg.name,
...this.estimateMonthlyCost(cfg),
}));
}
}
const calc = new CostCalculator();
// Compare two architectures
const results = calc.compareConfigs([
{
name: "Small startup",
instanceSize: "small", instanceCount: 1,
storageGB: 50, transferGB: 100, dbSize: "small",
},
{
name: "Growing platform",
instanceSize: "medium", instanceCount: 4,
storageGB: 500, transferGB: 1000, dbSize: "medium",
},
]);
console.log(results);Interactive Experiment
Try these exercises:
- Use the calculator to estimate costs for your own project. How much would 3 medium instances with 1 TB of storage cost?
- Add "reserved instance" pricing at 60% of on-demand. How much does a 1-year commitment save?
- Calculate the cost of logging: if each request generates 1 KB of logs and you get 1 million requests per day, how many GB of log storage per month?
- Compare the cost of running your own database server versus using a managed service. Include the cost of an engineer spending 4 hours per week on database maintenance.
Quick Quiz
Coding Challenge
Write a function `optimizeCost` that takes a `currentConfig` object with `instanceSize` ('small'=20/mo, 'medium'=80/mo, 'large'=160/mo), `instanceCount`, and `avgCpuPercent`. If avgCpuPercent is below 30 and the instance can be downsized (medium->small or large->medium), recommend downsizing. If avgCpuPercent is above 80, recommend upsizing (small->medium or medium->large). Otherwise, recommend keeping the current size. Return an object with `recommendation` (string: 'downsize', 'upsize', or 'keep'), `currentMonthlyCost`, and `projectedMonthlyCost`.
Real-World Usage
Cost modeling drives real business decisions:
- AWS Cost Explorer: Built-in AWS tool that visualizes spending patterns, forecasts future costs, and identifies savings opportunities like unused reserved capacity.
- Infracost: An open-source tool that estimates cloud costs from Terraform code, showing cost changes in pull requests before infrastructure is deployed.
- Spot instances (AWS) / Preemptible VMs (GCP): Use spare cloud capacity at up to 90% discount for fault-tolerant workloads like batch processing, CI/CD, and data pipelines.
- Serverless pricing: AWS Lambda charges per request and per millisecond of compute. For low-traffic APIs, serverless can be 10-100x cheaper than always-on servers.
- FinOps teams: Many companies now have dedicated FinOps (Financial Operations) teams that optimize cloud spending across the organization.