Serverless computing is a type of cloud computing where the user writes code, that gets run by the service provider, without having the user manage the infrastructure. Basically, serverless offers you a way to create applications without having to build, maintain, update, upgrade or provision any type of infrastructure; things that take a lot of time and money
It is a cloud computing execution model that:
Serverless computing allows developers to purchase backend services on a flexible ‘pay-as-you-go’ basis, meaning that developers only have to pay for the services they use. This is like switching from a cell phone data plan with a monthly fixed limit, to one that only charges for each byte of data that actually gets used.
The term ‘serverless’ is somewhat misleading, as there are still servers providing these backend services, but all of the server space and infrastructure concerns are handled by the vendor. Serverless means that the developers can do their work without having to worry about servers at all.
Examples of Serverless offerings:
As a developer, you have probably heard of Docker at some point in your professional life. And you're likely aware that it has become an important piece of tech for any application developer to know.
Dосker is а wаy tо соntаinerize аррliсаtiоns (рutting соde in bоxes thаt саn wоrk оn their оwn). It mаgiсаlly mаkes а virtuаl соmрuter, but guess whаt — they аren’t reаlly virtuаl соmрuters.
Соntаiners аre bоxes thаt hаve nо hоst oрerаting system, sо they аre indeрendent оf the deviсe they run оn.
Think оf it like this — there’s а bee thаt оnly likes tо live in its оwn hоneyсоmb, аnd will nоt be аble tо wоrk if it lives sоmewhere else. Yоu just trар the bee in а bоx thаt lооks аnd feels exасtly like it’s hоneyсоmb. Thаt’s соntаinerizаtiоn.
FYI, You don’t need to be a container expert to be productive with Cloud Run, but if you are, Cloud Run won’t be in your way.
Containerization is the packaging of software code with just the operating system (OS) libraries and dependencies required to run the code to create a single lightweight executable, called a container, that runs consistently on any infrastructure. More portable and resource-efficient than virtual machines (VMs), containers have become the de facto compute units of modern cloud-native applications. Containerization allows developers to create and deploy applications faster and more securely.
With traditional methods, code is developed in a specific computing environment which, when transferred to a new location, often results in bugs and errors. For example, when a developer transfers code from a Linux to a Windows operating system. Containerization eliminates this problem by bundling the application code together with the related configuration files, libraries, and dependencies required for it to run. This single package of software or “container” is abstracted away from the host operating system, and hence, it stands alone and becomes portable—able to run across any platform or cloud, free of issues.
Docker is an open-source containerization platform. It enables developers to package applications into containers - standardized executable components combining application source code with the operating system (OS) libraries and dependencies required to run that code in any environment. Containers simplify the delivery of distributed applications and have become increasingly popular as organizations shift to cloud-native development and hybrid multi-cloud environments.
Developers can create containers without Docker, but the platform makes it easier, simpler, and safer to build, deploy and manage containers. Docker is essentially a toolkit that enables developers to build, deploy, run, update, and stop containers using simple commands and work-saving automation through a single API.
A docker image in general is a software package which wraps your application executable, dependencies, configurations and application runtime into a secure and immutable unit.
You can build your image based on the instructions provided in a Dockerfile - a text file that contains instructions on how the Docker image will be built.
When the image is built you can store it locally or in some container image repository like hub.docker.com or Google Container Registry.
When you want to run the image, you start a container which needs your image. The container is the runtime instance of your image.
So at the end of the day, we can sum things up as:
Dockerfile
is a recipe for creating Docker images
.Docker image
gets built by running a Docker command (which uses a text file called a Dockerfile
).Docker container
is a running instance of a Docker image.Cloud Run is a compute platform by Google Cloud Platform to run your stateless HTTP containers without worrying about provisioning machines, clusters or autoscaling. With Cloud Run, you go from a "container image" to a fully managed web application running on a domain name with TLS certificate that auto-scales with requests in literally two commands. You only pay while a request is handled. It allows you to hand over a container image with a web server inside, and specify some combination of memory/CPU resources and allowed concurrency.
Note that Cloud Run is serverless: it abstracts away all infrastructure management, so you can focus on what matters most — building great applications.
Cloud Run then takes care of creating an HTTP endpoint, receiving requests and routing them to containers, and making sure enough containers are running to handle the volume of requests. While your containers are handling requests, you are billed in 100 ms increments.
Typical Use case
All workloads working in stateless mode, with a processing time less than 15 minutes and can be triggered by one or several HTTP end-points, like CRUD REST API. Languages, libraries and binaries don’t matter, only a container is required. Website backend, micro-batching, ML inference,… are possible use cases
Cloud run combines the benefits of containers and serverless pluus way more.
Other Serverless offerings like Cloud function, AWS Lambda or Azure Functions provides limited choice for languages and runtime. Cloud Run provides flexibility to build great applications in your favorite language, with your favorite dependencies and tools, and deploy them in seconds.
On Google cloud run you can even run Fortran or Pascal if you want. 🍄 Users managed to run web servers written in x86 assembly, or 22-year old Python 1.3 on Cloud Run.
Cloud Run comes with a generous free tier and is pay per use, which means you only pay while a request is being handled on your container instance. If it is idle with no traffic, then you don’t pay anything.
Once above the free tier, Cloud Run charges you only for the exact resources that you use.
For a given container instance, you are only charged when:
During that time, Cloud Run bills you only for the allocated CPU and memory, rounded up to the nearest 100 milliseconds. Cloud Run also charges for network egress and the number of requests.
As shared by Sebastien Morand, Team Lead Solution Architect at Veolia, and Cloud Run developer, this allows you to run any stateless container with a very granular pricing model:
"Cloud Run removes the barriers of managed platforms by giving us the freedom to run our custom workloads at a lower cost on a fast, scalable and fully managed infrastructure."
Read more about Cloud Run pricing here.
Cloud Run automatically scales the number of container instances you need to handle all incoming requests or events. However, contrary to other Functions-as-a-Service (FaaS) solutions like Cloud Functions, these instances can receive more than one request or event at the same time.
The maximum number of requests that can be processed simultaneously to a given container instance is called concurrency. By default, Cloud Run services have a maximum concurrency of 80.
Using a concurrency higher than 1 has a few benefits:
Read more about the concept of concurrency here.
Firebase Hosting pairs great with Cloud Run: put a custom domain on any Cloud Run service in any region, cache requests and serve static files in a global CDN for free.
When you develop for Cloud Run, you have to build a container. This container have the ability to be deployed anywhere: on premise, on Kubernetes/GKE, on Cloud Run (serverless and GKE), on a VM, … anywhere. Plus it’s going to make it very, very simple for people who are running containerized, stateless web servers today to get serverless benefits.
It is also good to know that Cloud Run is based on Knative, an open source project. If you ever wanted to leave the managed Google Cloud environment, we could easily deploy the same app and get similar features with Knative on anywhere Kubernetes runs.
Cloud Run Provides a simple command line and user interface. It quickly deploys and manages your service.
It’s also dead simple to test locally because inside your container is a fully-featured web server doing all the work.
Totally integrated with the Google Cloud Stackdriver suite, if you want you can configure the logs to the Stackdriver format, to easily make filters for log severity.
There is no infrastructure to manage. If you have deployed your application, Cloud Run will manage all the services.
Slight overview of how cloud Run works.
When a container is not handling any requests, it is considered idle. On a traditional server, you might not think twice about this. But on Cloud Run, this is an important state:
When a container handles a request after being idle, Cloud Run will unthrottle the container’s CPU instantly. Your application — and your user — won’t notice any lag.
Cloud Run can keep idle containers around longer than you might expect, too, in order to handle traffic spikes and reduce cold starts. Don’t count on it, though. Idle containers can be shut down at any time.
If your container is idle, Cloud Run can decide to stop it. By default, a container just disappears when it is shut down.
With Cloud Run, you go from a "container image" to a fully managed web application running on a domain name with TLS certificate that auto-scales with requests in two commands.
A container image is a self-contained package with your application and everything it needs to run. If you run a container image, that’s called a container. Cloud Run expects your container to listen for incoming requests on port 8080, running an HTTP server. The port number 8080 is a default you can override when you deploy the container.
When you deploy a container image to Cloud Run for the first time, it creates a service for you. A service automatically gets a unique HTTPS endpoint (more about that later).
If requests come in, Cloud Run starts your container to handle them. It adds more instances of your container if needed to handle all incoming requests, automatically scaling up and down.
Create a GCP project and link billing.
Install the gcloud CLI.
Enable GCP project services
You can enable them via the CLI:
gcloud services enable containerregistry.googleapis.com cloudbuild.googleapis.com run.googleapis.com
Part 1: Add a requirements.txt
file, (most python projects already have this) then add all of your dependencies plus gunicorn
(the webserver we will be using).
Example:
requirements.txt
Django==4.0.4
gunicorn==20.1.0
Part 2: Edit the ALLOWED_HOSTS
in your Django settings to allow all hosts (not suitable for a production environment and should be changed later to your preferred host(s)):
settings.py
# settings.py
ALLOWED_HOSTS = ['*']
In the root project directory, create a file with the name Dockerfile with no file extension. In the Dockerfile
created above, add the code below.
Dockerfile
# pull the official base image
FROM python:3.10
# set work directory
WORKDIR /app
# Copy local code to the container image.
COPY . .
# Install dependencies.
RUN pip install --no-cache-dir -r requirements.txt
# Run the web service on container startup. Here we use the gunicorn webserver, with one worker process and 8 threads.
CMD exec gunicorn --bind 0.0.0.0:$PORT --workers 1 --threads 8 mysite.wsgi:application
Remember to replace $GC_PROJECT
with your GCP project.
Part 1: Build Image (using Cloud Build):
Build the image and save it to Google Container Registry. eu.gcr.io/$GC_PROJECT/my-dj-app
is the image to build. The image must be in the format registry/project/imageName.
gcloud builds submit --tag eu.gcr.io/$GC_PROJECT/my-dj-app
Part 2: Deploy to Cloud Run
Deploy to Image Cloud Run. Remember to copy the Service URL
in the output to be used in the next step (Step 4).
gcloud run deploy my-service \
--image eu.gcr.io/$GC_PROJECT/my-dj-app \
--project $GC_PROJECT \
--region "europe-west1" \
--allow-unauthenticated
Flags:
--image
: The image to deploy. It's the same image we built in Part 1:.--project
: Your Google Cloud project.--region
: The physical region where the service will be deployed.--allow-unauthenticated
: Ensures you can access the service's URL without passing an authentication header.You can also Build + Run Container Locally (Optional):
Do this just to test if it works. To build the Docker image from the Dockerfile we created above, execute the commands below:
# Build image
docker build -t my-dj-app .
# Run container
docker run -e PORT=8080 -p 8080:8080 -d --name my-container my-dj-app
# Make request
curl localhost:8080
# Check logs
docker logs my-container
# Stop container
docker stop my-container
You can also Build Locally + Deploy to Cloud Run (Optional):
Do this just to test if it works. Replace $GC_PROJECT
with your GCP project.
# Build Docker Image
docker build -t eu.gcr.io/$GC_PROJECT/my-dj-app .
# Push Docker Image to Google Container Registry
docker push eu.gcr.io/$GC_PROJECT/my-dj-app
# Deploy to Image from GCR onto Cloud Run
gcloud run deploy my-service \
--image eu.gcr.io/$GC_PROJECT/my-dj-app \
--project $GC_PROJECT \
--region "europe-west1" \
--platform managed \
--allow-unauthenticated
Visit the Service URL
you got in the above step to view your service. Here’s mine:
Done!
Thank you for reaching the end of the guide!
I hope you learned something.