Is Serverless Still Relevant? (GCP Addition)
Tags: kubernetes, devops, cloud, programming
Published: 2024-05-29 15:30:02

Serverless was a big implementation for organizations when it came out. The entire goal (depending on who you spoke with) was that it would minimize and/or remove the need for underlying infrastructure. Developers thought they would be able to write code and just run it without having to do any configuration.

Unfortunately, that did not pan out, but a lot of good still came out of it.

In this blog post, you’ll learn what serverless is, what GCP’s version of serverless is, and how to configure it.

## What Is Serverless

Here’s a high-level explanation of Serverless:

*Write code that you want to execute based on a certain event, the code is set up on a trigger for when that event occurs, and the code runs.*

This sounds great in theory and a lot of engineers loved the idea that they could write code and run it, but it turns out that Serverless was better suited for event-drive workloads. Event-driven workloads can mean many things, but in this case, it’s code that gets kicked off based on a particular event that occurs in your environment. It could also be a script that runs.

For example, let’s say you have to query particular workloads every few seconds. The workloads could be retrieving real-time data for an implementation and you have to ensure that the frontend is constantly updated with the new data. Cloud Run can run a job that continuously executes code or a container to run the code to competition, and the completion would be to fetch the data.

The goal with Serverless is to run code and/or containers based on a particular event that takes place.

## What Is GCP Cloud Run

There are many Serverless providers, but some of them are taking an extra step forward to act as more of an orchestrator compared to a simple event-based trigger workload.

GCP is taking this leap.

Cloud Run gives you the ability to run both Jobs (code/container that executes until competition) and Services, which are long-running workloads. The interesting thing about Cloud Run Services is that from a containerization perspective, it can almost act like a serverless orchestrator, which can be compared to other platforms like EKS Fargate profiles or even GKE Autopilot.

<aside>
💡 GKE Autopilot is “Serverless Kubernetes”. You no longer have to manage Worker Nodes if you use GKE Autopilot. It’s a great implementation, but there are some things that you’re limited to with GKE Autopilot because the entirety of the infrastructure is abstracted away from you.

</aside>

Where Cloud Run shines is if you want to manage multiple containers separately (no sidecar container support) to run application stacks or jobs. It’s great if your application stack is fully decoupled, but it may cause a headache if you need a few parts of your application stack in one location. If you’re already running containers and/or have created a container image and ensured that your application stack works in a containerized environment, you should have no trouble.

## Configuring GCP Cloud Run With Code

Now that you know a bit about serverless, let’s learn how to configure GCP Cloud Run with a custom Container Image.

1. In the GCP console, search for Cloud Run.
2. Within the Cloud Run panel, click the blue **+ CREATE SERVICE** button.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2e91gexn12afn1f8hft.png)



3. The service will require a container image. You can use an example container image or the container image below.

<aside>
💡 I wrote the `adminturneddevops/golangwebapi` container image below. It’s a simple web based Go (golang) API that’s great for testing.

</aside>

```jsx
adminturneddevops/golangwebapi
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trj567d62ibvtnu6jq27.png)



4. Configure the region, authentication and CPU allocation for the container image. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s03vjgp06wapopn0j5lv.png)



5. Set autoscaling and ingress capabilities.

<aside>
💡 This is where a tool like Cloud Run shines. It’s combining serverless with the ability to implement resource optimization for the containers running.

</aside>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r822gq7bf9k36ca3nohf.png)


6. Under the **Containers, Volumes, Networking, and Security** section, you can customize your container with ports, container name, volumes, networking, security, and any other customization option that’s available including resource optimization for CPU and memory within the containers.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n2rp55d2ccvvc7n7mt97.png)



7. When finished, click the blue **CREATE** button.

<aside>
💡 A cool feature when using cloud-based platforms like GCP is the ability to use the integrated third-party tools and services that are available within the cloud. For example, within Cloud Run, you can connect to a SQL server running on GCP.

</aside>


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6ul39a2x04ruorxoquv.png)



8. After a few moments, you should see the container running.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k25n5xmm8h6drdqeehmr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oamad4atknw3xmke03oh.png)



## GCP Cloud Run on The Terminal

Aside from running Cloud Run in the portal, you can also use the `gcloud` CLI,  which is the terminal/programmatic method of interacting with GCP.

To deploy a container in Cloud Run via `gcloud`, you can use the `run` command.

Below is an example.

```jsx
gcloud run deploy gowebapi --image adminturneddevops/golangwebapi
```

After you run the command above, you’ll have a few prompts to answer.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tb6mscawcbpfy9jrhcdl.png)


You should now see the container getting deployed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkypi0vamtrzr5b9t6i5.png)



After the deployment is complete, you’ll see the container show up in the Cloud Run portal.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i6zhjpyod20ynrwfvaf1.png)