It‘s been a year (almost!) since Google Cloud Platform (GCP) first announced this interesting serverless SaaS product, knows as ‘Cloud Run’. It was built with the idea to bring serverless computing to fully-managed container service.
Cloud Run allows you to create dynamic web and mobile applications that automatically scale to meet traffic demands with a pay‐per‐use billing model.
That means you no longer have to pay for the times the application is not used and need not bear any hefty infrastructure costs.
Cloud Run can be defined as the combination of two biggest trends of recent times i.e. Containerization and Serverless. Serverless means no infrastructure management, usage-based pricing, and easier auto-scaling while containerization stands for serving a project with stateless containers.
In its documentation, Google defined it in the best way possible-
Run stateless HTTP containers on a fully managed environment or in your own GKE cluster.
Cloud Run was built around an open API, Knative, which enables the portability of your workloads and lets them run anywhere inside a stateless container. It allows you to store the docker images of the container with a web server within a specified memory specification/CPU resources and concurrency.
It is responsible for creating endpoints for serving over HTTP requests, routing to the container, and monitor the running container to handle the volume requests.
Being fully serverless, Cloud Run portrays a different and better scenario of using web server infrastructure. Apart from that, Cloud Run also confers numerous other wonderful features, such as-
Cloud SDK is compatible with all of Linux, Windows, and macOS. It is a set of command-line tools used to get direct access to cloud storage, BigQuery, and other Google cloud services. Let’s check out its installation steps-
Cloud SDK is available in package format for installation on Debian and Ubuntu systems. This package contains all gcloud commands for deployment over the command-line interface.
One can manually install the Cloud SDK by following the instructions below-
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get install apt-transport-https ca-certificates gnupg
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add –
sudo apt-get update && sudo apt-get install google-cloud-sdk
gcloud init
If one wants to go back to a specific version of Cloud SDK, where VERSION has the 123.0.0 module, run:
sudo apt-get update && sudo apt-get install google-cloud-sdk=123.0.0-0
The gcloud init command is quite useful to perform various common SDK configuration tasks including authorization of the SDK tools, accessing Google Cloud platform, and configuring the default SDK settings.
To initialize the SDK, run the following at a command prompt:
gcloud init
To restrict the command from initiating a web browser, use instead-
gcloud init --console-only
To authorize without a web browser and in a non-interactive way, create a service account with appropriate scopes using google cloud console and gcloud auth enable-service-account with the corresponding JSON key file.
Sign in with your Google user account.
In the browser, log in to your Google user account.
If prompted, click ‘Allow’ to grant permission to access resources on the Google Cloud Platform.
At the command prompt, select a Cloud Platform project from the list, having Owner, Editor, or Viewer permissions.
In case of a single project, gcloud init will select it by default.
If you have the Google Compute Engine API enabled, gcloud init allows to choose a default Compute Engine zone.
It also ensures that you have completed the setup process successfully: Kudos, gcloud has now been configured!
Cloud SQL is a fully-managed database service that serves the configuration, maintenance, and administration of PostgreSQL and MySQL relational databases in the cloud.
To use this service in running state in an instance of Cloud Run to a Cloud SQL, one needs to have this service in the container and loaded into the container registry.
If you don’t have the container in gcloud run then follow the below-given commands to build and push the docker image-
!/usr/bin/env bash
docker build -t image-name .
docker build -t gcr.io/$(gcloud config get-value project)/simple-example:latest .
docker push gcr.io/$(gcloud config get-value project)/simple-example
gcloud beta run deploy --image gcr.io/$(gcloud config get-value project)/simple-example:latest
Once you are done with configuring docker container correctly, the only way to connect Cloud SQL to cloud run is to use UNIX socket with cloud run instance (follow the below format for connection string):
/cloudsql / INSTANCE_CONNECTION_NAME
These connections are serviced to be automatically encrypted without any additional configuration.
Warning: Linux-based operating systems have a maximum socket path length of 107 characters. If the total path length exceeds this length, one cannot connect to a socket from Cloud Run (fully managed).
Cloud Run (fully managed) does not support connecting to the Cloud SQL instance via TCP. The code must not attempt to access the instance using an IP address such as 127.0.0.1 or 172.17.0.1. For example:
let variable;
const createVariable = async () => {
variable = await mysql.createVariable({
user: process.env.DB_USER, // e.g. 'my-db-user'
password: process.env.DB_PASS, // e.g. 'my-db-password'
database: process.env.DB_NAME, // e.g. 'my-database'
// If connecting via unix domain socket, specify the path
socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
// If connecting via TCP, enter the IP and port instead
// host: 'localhost',
// port: 3306,
//...
});
};
createVariable();
That’s it folk!! I’ll wrap up here, hoping that this whole info about using cloud run services and connecting with google cloud database will be proved helpful for you, sooner or later. Stay tuned with us.