Example: Serverless
Learning objectives
- You know how to create a serverless application.
Here, we'll look into creating a serverless application. We'll use the same application as in the Kubernetes example in the part on cloud computing, and similarly use Google Cloud for deploying the application.
Note that the guidelines here assume that you've worked through the Kubernetes example, and take a peek at them for specifics.
Setting up
Create a new project on Google Cloud and call it dab-serverless-jokes
(you could name the project anything you want). Create an artifact registry to the project, and create a folder for docker images into the artifact registry. Similar to the Kubernetes example, we'll call the repository "docker-images" and choose europe-north1
as the region.
Creating the image
We'll use the same application as previously. The application is written in vanilla Deno and responds with jokes. The app.js
file is as follows.
const jokes = [
"What did baby corn say to mama corn? -- Where's pop corn?",
"Why are eggs bad at handling stress? -- They crack under pressure.",
];
const server = `Server ${Math.floor(10000 * Math.random())}`;
const handleRequest = async (request) => {
const joke = jokes[Math.floor(Math.random() * jokes.length)];
return new Response(`${server}: ${joke}`);
};
Deno.serve({ hostname: "0.0.0.0", port: 7777 }, handleRequest);
And the Dockerfile
is as follows.
FROM denoland/deno:alpine-1.42.2
EXPOSE 7777
WORKDIR /app
COPY . .
CMD [ "run", "--unstable", "--allow-net", "app.js" ]
When in the folder that contains the above files, we run the command docker build -t jokes-app .
to create the jokes-app
image (if you followed the Kubernetes example, this is already done).
docker build -t jokes-app .
...
=> => naming to docker.io/library/jokes-app
Pushing the image to the registry
We'll again authenticate to the artifact registry using the command gcloud auth configure-docker europe-north1-docker.pkg.dev
. This will add the credentials to the Docker config file, if they are not yet there.
gcloud auth configure-docker europe-north1-docker.pkg.dev
WARNING: Your config file at [/home/username/.docker/config.json] contains these credential helper entries:
{
"credHelpers": {
"europe-north1-docker.pkg.dev": "gcloud"
}
}
Adding credentials for: europe-north1-docker.pkg.dev
gcloud credential helpers already registered correctly.
Next, we tag the image by running the command docker tag jokes-app europe-north1-docker.pkg.dev/dab-serverless-jokes/docker-images/jokes-app:latest
-- the key difference to the Kubernetes example is that the project that we are working with is dab-serverless-jokes
.
docker tag jokes-app europe-north1-docker.pkg.dev/dab-serverless-jokes/docker-images/jokes-app:latest
Once the image is tagged, we push it to the registry.
docker push europe-north1-docker.pkg.dev/dab-serverless-jokes/docker-images/jokes-app:latest
The push refers to repository [europe-north1-docker.pkg.dev/dab-serverless-jokes/docker-images/jokes-app]
...
latest: ...
Now, the image jokes-app
should be in the list of images in the artifact registry.
Going serverless
Next, we'll go to Google Cloud Run and click the create service button. In the configuration, we choose "Deploy one revision from an existing container image", and search the jokes-app
image from the artifact registry. We select europe-north1
as the region, select the per request pricing, and limit the autoscaling to at most 5 instances (keeping the minimum number of instances at 0). We'll allow all traffic to the service from the internet, and allow unauthenticated invocations.
Further, as we've created our application so that it listens to requests on port 7777
, we need to adjust the container network settings. In the part on Container configuration, set the "Container port" as 7777
, and allocate 128 MiB memory and one vCPU to the container.
We could also adjust the application so that it reads an environment variable
PORT
and use that value.
We can keep the other options as they are by default, and then click the "Create" button. The application will take a while to start up. This creates a serverless application that is accessible over the internet. The URL to the service is available on the service details once the application has been created.
In the case of the material author, the app can be visited at https://jokes-app-rqafkhjgta-lz.a.run.app.
curl https://jokes-app-rqafkhjgta-lz.a.run.app/
Server 1044: What did baby corn say to mama corn? -- Where's pop corn?
Google Cloud Run has a free tier of up to two million invocations per month, so contrary to the Kubernetes example, the above URL will be up and running at least for while..
Setting up continuous deployment
As you may notice if you've followed the above example and opened the Google Cloud Run dashboard, there's an option for setting up continuous deployment. This allows linking the application to a GitHub repository, and automatically deploying the application when changes are pushed to the repository.
Note that although the above application is a pet example, the same principles could be used to build larger applications; we could also, for example, run parts of the application using Kubernetes, and parts of the application on a serverless platform, and rely on a cloud database (or few) to manage the application data.