DEV Community

Legorie
Legorie

Posted on

Ultra cheap website on GCP

A few years ago, I wanted to test an idea with a YouTube aggregator site for a specific user group and following the lean methodology, I wanted to test the idea at a small RUN cost.
Google Cloud Platform (GCP) had launched a few years ago the Cloud Run service which could run a serverless docker container to respond to web requests; hence, I launched on a journey to launch my website using Cloud Run and the GCP ecosystem. In this article, we would see :

  1. The goal of the project
  2. The different technical components I ended up using - The technical architecture

(i) The goal of the project

  • Go through the different YouTube channels, grab the latest videos depending on some decision criteria and present them in a library format
  • Evidently, at a cheap cost of run

A website with an image carousal linking to the different selected videos, categorized into different sections.
Image carousal liking to the videos

(ii) The different technical components

The website is written in Python Django and the batch process to pull data from YouTube was written in plain Python.

Architecture diagram
Archi diagram

Here is an explanation of the overall architecture:
1) The Django application is hosted in GitHub and mirrored to the GCP Source repository

Code repo

2) Cloud Build is used to build the application into a docker image, push the image to the GCP Container registry. We can have multiple options to trigger the build, for example on a push to branch, a webhook event etc.. And for how to build the image, I used a Cloud Build configuration yaml file

steps:
  # build the container image
- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/${_DEV_REGION}/twsite', '.']
  # push the container image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
  args: ['push', 'gcr.io/${_DEV_REGION}/twsite']
  # Deploy container image to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
  args: ['beta', 'run', 'deploy', 'twsite', '--image', 'gcr.io/${_DEV_REGION}/twsite', '--region', 'us-central1', '--platform', 'managed']
images:
- gcr.io/${_DEV_REGION}/twsite
substitutions:
    _DEV_REGION: twsite-dev
Enter fullscreen mode Exit fullscreen mode

3) To make the image a runnable container, we are using the serverless - Cloud Run service. The service accepts web requests and provides enough Free tier to test our website. Pay attention to the auth requirement to invoke the website and the port requirements as per the application. Serverless applications have a delayed response time when invoked for the first time, called the 'cold start', it is important to keep this in mind for this solution. The service account used in the Cloud Run settings should have access to the Cloud Datastore as the application uses the implicit access to access the data - which gives us more control and audit in the GCP platform.

Cloud Run

4) The batch process is written in the Cloud Functions and is triggered once a day using the Cloud Scheduler. The Python code combs through a number of YouTube channels and pulls the most interesting videos to be saved in the Cloud Datastore. Many a time the timeout of the Cloud Functions was hit, and I had to break down the functions into smaller functions to fit inside the budgeted limits.

Cloud Functions

The selection of a NoSQL database as the Cloud Datastore helped in the evolution of the schema multiple times during the development phase.

5) Another key service which comes in handy to save the day or to give us a slap in the face is the logging service attached to each of the cloud services used. Many a time we face issues - regarding the necessary access to use a service, mismatch of a port number, a secret not initialized correctly - to name a few

Meme logging

After running the website for almost 6 months, I was not happy with the traction it built (or the lack of traction) and shut down the website. The maximum bill amount would have been less than $2 to which the Cloud Scheduler service was the major contributor and for the meak demand, the serverless components were well within the free tier.

The fun of building the website from the ground up, the design decisions, adapting the code & design for new requirements and tweaking the GCP services for the new needs are something I will cherish.

Hoping this article trickles your curiosity, Happy tinkering!

Top comments (0)