What is Google Cloud Run?
Published: 2023-02-01 15:30:17

**Cloud Run** is a managed compute platform that lets you run containers directly on top of Google's scalable infrastructure.

1. Deploy and Scale applications fast and securely in a fully managed environment.
2. Simple & automated.
3. No intra management.
4. Developer velocity.
5.Deploy code in any language.

You can deploy code written in **any programming language** on Cloud Run if you can build a container image from it. 
In fact, building container images is optional. If you're using Go, Node.js, Python, Java, .NET Core, or Ruby, you can use the source-based deployment option that builds the container for you, using the best practices for the language you're using.

**There are two ways to run this code**
On Cloud Run, your code can either run continuously as a service or as a job. Both services and jobs run in the same environment and can use the same integrations with other services on Google Cloud.

- **Cloud Run services**. Used to run code that responds to web requests, or events.


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

A Cloud Run service provides you with the infrastructure required to run a reliable HTTPS endpoint. Your responsibility is to make sure your code listens on a TCP port and handles HTTP requests.

Standard service features include:

- Unique HTTPS endpoint for every service

- Fast request-based auto scaling

- Built-in traffic management

- Private and public services

Cloud Run services running on Google Cloud Platform (GCP) are built on top of a managed compute platform that allows you to run stateless containers that are invocable via HTTP requests. Cloud Run is serverless, so you don't need to manage any servers, and you only pay for the compute resources you use. Cloud Run is also highly scalable and can automatically scale up and down in response to incoming requests.

Cloud Run is designed to be easy to use and allows developers to quickly build and deploy cloud-native apps without having to worry about managing the underlying infrastructure. Cloud Run is also integrated with other GCP services such as Cloud Storage, Cloud Functions and Cloud Pub/Sub, allowing developers to easily build and deploy applications that use these services. Cloud Run is also integrated with Kubernetes and can be used to deploy and manage containerized applications in a Kubernetes cluster.


**When to use Cloud Run services**
Cloud Run services are most useful for situations where you need to quickly scale up or down, without having to manage the underlying infrastructure. This could be for a web application, a containerized API, or a microservice. Cloud Run services are also useful for applications that need to be able to scale up or down based on demand. They are also useful for applications that need to run in a secure and managed environment.

Cloud Run services are great for code that handles requests or events. Example use cases include:

**- Websites and web applications**
Build your web app using your favorite stack, access your SQL database, and render dynamic HTML pages.

**- APIs and microservices**
You can build a REST API, or a GraphQL API or private microservices that communicate over HTTP or gRPC.

**- Streaming data processing**
Cloud Run services can receive messages from Pub/Sub push subscriptions and events from Eventarc.



- **Cloud Run jobs**. Used to run code that performs work (a job) and quits when the work is done.

If your code performs work and then stops (a script is a good example), you can use a Cloud Run job to run your code. You can execute a job from the command line using the gcloud CLI, schedule a recurring job, or run it as part of a workflow.

Array jobs are a faster way to run jobs
A job can start one container instance to run your code — that's a common way to run a script or a tool. However, you can also start many identical, independent container instances in parallel, that is, an array job.

Array jobs are a faster way to process jobs that can be split into multiple independent tasks, as shown here:

Array jobs are a faster way to run parallelizable jobs

For example, if you are reading 1,000 images from Cloud Storage to resize and crop them, processing them consecutively will be slower than processing them all at the same time with many container instances.

**When to use Cloud Run jobs**
Cloud Run jobs are well-suited to run code that performs work (a job) and quits when the work is done. Here are a few examples:

**Script or tool**
Run a script to perform database migrations or other operational tasks.

**Array job**
Perform highly parallelized processing of all files in a Cloud Storage bucket.

**Scheduled job**
Create and send invoices at regular intervals, or save the results of a database query as XML and upload the file every few hours.


## **Cloud Run jobs workflow**

The Cloud Run jobs workflow consists of two simple steps:

1. **Create the job** The job encapsulates all the configuration needed to execute the job, such as the container image.
2. **Execute the job**. Use either the Cloud Console or the gcloud CLI and execute the job from the command line.

A job can start *one* container instance to run your code, or it can start many identical, independent container instances in parallel by specifying the number of tasks to run. For example, if you are reading 1,000 images from Cloud Storage to resize and crop them, processing them consecutively will be slower than processing them all at the same time with many container instances.

## **Create the job**

To [create the job](https://cloud.google.com/run/docs/create-jobs), you can use either the Cloud Console or the gcloud CLI using the following command:

```
gcloud beta run jobs create JOB_NAME --image IMAGE_URL --region europe-west9 OPTIONS

```

Replace:

- **`JOB_NAME`** with the name of the job you want to create.
- **`IMAGE_URL`** with a reference to the container image.
- Optionally, replace **`OPTIONS`** with any of the available flags. For a complete list of flags, run **`gcloud beta run jobs create --help`**.

Example flags include:

- **`-tasks`** for the number of tasks to run.
- **`–max retries`** for the number of times a failed task is retried.
- **`-parallelism`** for the maximum number of tasks that can run in parallel.
- **`-execute-now`** to execute the job immediately after it is created.
- **`-async`** to exit the job immediately after creating a new execution.

You can also use the usual Cloud Run features to secure your Cloud Run job and connect it to the rest of your Google Cloud Platform (GCP) environment.

## **Execute the job**

To execute the job, you can use either the Cloud Console or the gcloud CLI using the following command:

- In the Cloud Console, click on the job name, and click **Execute** near the top of the page.
- In the gcloud CLI, use the following command:

```
gcloud beta run jobs execute JOB_NAME --region europe-west9 EXECUTION_OPTIONS

```

Replace **`JOB_NAME`** with the name of the job. Optionally, replace **`EXECUTION_OPTIONS`** to specify:

- **Immediate** job execution after you create the job.

```
gcloud beta run jobs create JOB_NAME --region europe-west9 --execute-now

```

- If you want to **wait** until the execution is complete before exiting.

```
gcloud beta run jobs create JOB_NAME --region europe-west9 --wait

```

- If you want to **exit immediately** after creating a new execution.