Django: How much it costs to host a small project on App Engine
The cost of Cloud SQL seems to dominate the hosting bill for Django on App Engine. A look into how to correctly configure the DB instance.
Since June 2020, I’ve been running a small Django app on GCP using App Engine
Standard, Cloud SQL (PostgreSQL) and Cloud Storage. Over the summer I
experimented with different configurations trying to bring down the monthly
cost. In this note I report on what I learned.
In my September 2020 bill, the total monthly cost is €28.36 ($33.23 in US
Dollars). This includes:
€26.41 (€24.74 + €1.67) for the CloudSQL database instance (93% of the total cost)
€1.78 for the App Engine Frontend instances
€0.17 for Cloud Storage
€0.16 for Google Maps API that my project happens to use, offset by a free
tier associated with this API
The final line of the invoice is an offset coming from the
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
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
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.
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).
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 is ~€30, but it seems theoretically possible to go as
low as ~€10 per month with the micro instance type. If anyone here tried that
and has experience to share, please leave a comment!