Since June 2020, I’ve been running a small Django app on GCP using App Engine Standard, Cloud SQL (PostgreSQL) and Cloud Storage. After experimenting with various configurations I found a way to make it run for as low as 11 Euro / 13 US Dollars per month.
Cost breakdown
In my November 2021 bill, the total monthly cost is €11.69 ($13.14 in US Dollars). This includes:
- €9.58 for the CloudSQL database instance (98% of the total cost)
- €0.16 for Cloud Storage
- €0.00 for the App Engine Frontend instances – all of my usage is covered by the free tier
- (the rest is taxes)
Note: if you’re new to Google Cloud, make sure to check out GCP free trial credits – making the payable cost €0.00 as long as the free credits last.
Understanding CloudSQL pricing
As we can see, the cost of CloudSQL is dominating the bill, so let’s take a deeper look on what affects this charge. The official docs on CloudSQL pricing mention two types of billing:
- instance pricing, applicable to instances with shared CPUs, where we pay for the instance and get a fixed amount of RAM and storage included. For example, we can opt for db-g1-small, and the only cost we’ll pay is the instance cost (for Frankfurt it at the moment of this writing it’s ~€26 per month)
- CPU and memory pricing, applicable to instances with dedicated CPUs, where we pay separately for CPU and RAM and storage of the instance, and we have a lot of flexibility in how much resources to allocate for it
The table below compares different instance pricing configurations. Note that these are not meant for production use and are not covered by Cloud SQL SLA – nevertheless, they’re what’s cheapest. For a small personal project, I wanted to go for one of the two smallest configurations: micro or small.
As my instance of PostgreSQL seems to be using ~450 MBs of memory under a normal load (see below), I didn’t feel comfortable with the little safety margin I’d have on the micro instance type (which comes with 600 MBs of RAM), and opted for small – this increases the cost by a factor of 3, meaning that a more adventurous administrator may be able to run their Django project on GCP for less than €10 per month.
However, we need to first know how to actually select the instance type we want, and this turned out not obvious.
Configuring CloudSQL
When creating the CloudSQL instance in the cloud console, we’re presented with the folowing configuration form:
At the first look, this seems to represent the cheapest possible configuration. But is it the micro instance then? Or the small instance perhaps? It’s actually neither, if we create the instance this way we will be billed via the CPU and memory pricing described above, not the instance pricing.
Upon some experimentation, we may notice that the CPU slider is not actually in the leftmost possible position, even though it may look like it – we can slide it further to the left:
This looks more like it! We even get the expected warning that those instances are only intended for testing.
However, I was still unsure if this configuration represents precisely the
instance pricing options referenced in the docs, as the words “instance
pricing”, “micro” and “small” don’t appear on the screen. If you’re like me and
want to be sure, I found that after creating the instance we can use a gcloud
command to explicitly switch the Cloud SQL instance to the desired instance
type. Note that this results in brief downtime while the instance is being
reconfigured (~5 minutes in my experience).
gcloud sql instances patch <INSTANCE_NAME> --tier=db-f1-micro
App Engine autoscaling settings
To make sure I’m not paying for idle instances (which App Engine autoscaling may
create to better handle potential spikes in traffic), I set the cap on the
maximum number of idle instances to 1 in my app.yaml
:
instance_class: F1
automatic_scaling:
max_idle_instances: 1
And to make sure I don’t incur high unexpected costs if my small personal project unexpectedly experiences very high traffic, I also add a cap on the max number of instances overall:
instance_class: F1
automatic_scaling:
max_idle_instances: 1
max_instances: 3
Conclusion
Cloud SQL pricing dominates the cost of running a Django app on GCP. Take care to configure the SQL instance as desired. With the configuration described above, the monthly cost for my app is ~€13. Let me know in the comments if you find a way to go lower than that!