# Cloud Run Functions: Qwik Start - GSP1089

## Overview

[Cloud Run functions](https://cloud.google.com/functions) is Google Cloud's Functions-as-a-Service offering. It comes with an advanced feature set and is powered by [Cloud Run](https://cloud.google.com/run) and [Eventarc](https://cloud.google.com/eventarc/docs), giving you more advanced control over performance and scalability, and more control around the functions runtime and triggers from over 90+ event sources.

In this lab, you create Cloud Run functions that respond to HTTP calls, and get triggered by Cloud Storage events and Cloud Audit Logs. You also deploy multiple revisions of a Cloud Run function and explore new settings.

### What's new?

This new version of Cloud Run functions provides an enhanced FaaS experience powered by Cloud Run, Cloud Build, Artifact Registry, and Eventarc.

#### Enhanced Infrastructure

* **Longer request processing:** Run your Cloud Run functions longer than the 5 minute default, making it easier to run longer request workloads such as processing large streams of data from Cloud Storage or BigQuery. For HTTP functions, this is up to 60 minutes. For event-driven functions, this is currently up to 10 minutes.
    
* **Larger instances:** Take advantage of up to 16GB of RAM and 4 vCPUs on Cloud Run functions allowing larger in-memory, compute-intensive and more parallel workloads.
    
* **Concurrency:** Process up to 1000 concurrent requests with a single function instance, minimizing cold starts and improving latency when scaling.
    
* **Minimum instances:** Provide for pre-warmed instances to cut your cold starts and make sure the bootstrap time of your application does not impact the application performance.
    
* **Traffic splitting:** Support multiple versions of your functions, split traffic between different versions and roll your function back to a prior version.
    

#### Broader Event coverage and CloudEvents support

* **Eventarc Integration:** Cloud Run functions now includes native support for Eventarc, which brings over [125+ event sources](https://cloud.google.com/eventarc/docs/reference/supported-events) using Cloud Audit logs (BigQuery, Cloud SQL, Cloud Storage...), and of course Cloud Run functions still supports events from custom sources by publishing to Cloud Pub/Sub directly.
    
* **CloudEvent format:** All event-driven functions adhere to industry standard CloudEvents ([cloudevents.io](http://cloudevents.io/)), regardless of the source, to ensure a consistent developer experience. The payloads are sent via a structured CloudEvent with a [cloudevent.data payload](https://cloud.google.com/eventarc/docs/cloudevents) and implement the CloudEvent standard.
    

### Objectives

In this lab, you learn how to perform the following tasks:

* Write a function that responds to HTTP calls.
    
* Write a function that responds to Cloud Storage events.
    
* Write a function that responds to Cloud Audit Logs.
    
* Deploy multiple revisions of a Cloud Run function.
    
* Get rid of cold starts with minimum instances.
    
* Set up concurrency.
    

## Setup and requirements

### Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click **Start Lab**, shows how long Google Cloud resources are made available to you.

This hands-on lab lets you do the lab activities in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

* Access to a standard internet browser (Chrome browser recommended).
    

**Note:** Use an Incognito (recommended) or private browser window to run this lab. This prevents conflicts between your personal account and the student account, which may cause extra charges incurred to your personal account.

* Time to complete the lab—remember, once you start, you cannot pause a lab.
    

**Note:** Use only the student account for this lab. If you use a different Google Cloud account, you may incur charges to that account.

### How to start your lab and sign in to the Google Cloud console

1. Click the **Start Lab** button. If you need to pay for the lab, a dialog opens for you to select your payment method. On the left is the Lab Details pane with the following:
    
    * The Open Google Cloud console button
        
    * Time remaining
        
    * The temporary credentials that you must use for this lab
        
    * Other information, if needed, to step through this lab
        
2. Click **Open Google Cloud console** (or right-click and select **Open Link in Incognito Window** if you are running the Chrome browser).
    
    The lab spins up resources, and then opens another tab that shows the Sign in page.
    
    ***Tip:*** Arrange the tabs in separate windows, side-by-side.
    
    **Note:** If you see the **Choose an account** dialog, click **Use Another Account**.
    
3. If necessary, copy the **Username** below and paste it into the **Sign in** dialog.
    
    ```apache
    student-00-8ec5d106237b@qwiklabs.net
    ```
    
    You can also find the Username in the Lab Details pane.
    
4. Click **Next**.
    
5. Copy the **Password** below and paste it into the **Welcome** dialog.
    
    ```apache
    9Kezl0i7D2Ym
    ```
    
    You can also find the Password in the Lab Details pane.
    
6. Click **Next**.
    
    **Important:** You must use the credentials the lab provides you. Do not use your Google Cloud account credentials.
    
    **Note:** Using your own Google Cloud account for this lab may incur extra charges.
    
7. Click through the subsequent pages:
    
    * Accept the terms and conditions.
        
    * Do not add recovery options or two-factor authentication (because this is a temporary account).
        
    * Do not sign up for free trials.
        

After a few moments, the Google Cloud console opens in this tab.

**Note:** To access Google Cloud products and services, click the **Navigation menu** or type the service or product name in the **Search** field.

![Navigation menu icon and Search field](https://cdn.qwiklabs.com/9Fk8NYFp3quE9mF%2FilWF6%2FlXY9OUBi3UWtb2Ne4uXNU%3D align="left")

### Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

1. Click **Activate Cloud Shell** at the top of the Google Cloud console.
    
2. Click through the following windows:
    
    * Continue through the Cloud Shell information window.
        
    * Authorize Cloud Shell to use your credentials to make Google Cloud API calls.
        

When you are connected, you are already authenticated, and the project is set to your **Project\_ID**, `qwiklabs-gcp-02-6e1c9205e771`. The output contains a line that declares the **Project\_ID** for this session:

```apache
Your Cloud Platform project in this session is set to qwiklabs-gcp-02-6e1c9205e771
```

`gcloud` is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

3. (Optional) You can list the active account name with this command:
    

```apache
gcloud auth list
```

4. Click **Authorize**.
    

**Output:**

```apache
ACTIVE: *
ACCOUNT: student-00-8ec5d106237b@qwiklabs.net

To set the active account, run:
    $ gcloud config set account `ACCOUNT`
```

5. (Optional) You can list the project ID with this command:
    

```apache
gcloud config list project
```

**Output:**

```apache
[core]
project = qwiklabs-gcp-02-6e1c9205e771
```

**Note:** For full documentation of `gcloud`, in Google Cloud, refer to [the gcloud CLI overview guide](https://cloud.google.com/sdk/gcloud).

## Task 1. Enable the APIs

Before you create the Cloud Run functions, you need to enable the relevant APIs.

* Execute the following command to enable all the necessary services.
    

```apache
gcloud services enable \
  artifactregistry.googleapis.com \
  cloudfunctions.googleapis.com \
  cloudbuild.googleapis.com \
  eventarc.googleapis.com \
  run.googleapis.com \
  logging.googleapis.com \
  pubsub.googleapis.com
```

### Enable Gemini Code Assist in the Cloud Shell IDE

You can use Gemini Code Assist in an integrated development environment (IDE) such as Cloud Shell to receive guidance on code or solve problems with your code. Before you can start using Gemini Code Assist, however, you need to enable it.

1. In Cloud Shell, enable the **Gemini for Google Cloud** API with the following command:
    

```apache
gcloud services enable cloudaicompanion.googleapis.com
```

2. Click **Open Editor** on the Cloud Shell toolbar.
    

**Note:** To open the Cloud Shell Editor, click **Open Editor** on the Cloud Shell toolbar. You can switch between Cloud Shell and the code Editor by clicking **Open Editor** or **Open Terminal**, as required.

3. In the Cloud Shell Editor, navigate to **Cloud Code &gt; Help and Feedback &gt; Change Settings**.
    
4. In the **Settings**, search for **Gemini Code Assist**.
    
5. Locate and ensure that the checkbox is selected for **Geminicodeassist: Enable**, and close the **Settings**.
    
6. Click **Cloud Code - No Project** in the status bar at the bottom of the screen.
    
7. Authorize the plugin as instructed. If a project is not automatically selected, click **Select a Google Cloud Project**, and choose `qwiklabs-gcp-02-6e1c9205e771`.
    
8. Verify that your Google Cloud project (`qwiklabs-gcp-02-6e1c9205e771`) displays in the Cloud Code status message in the status bar.
    

## Task 2. Create an HTTP function

For the first function, you create an **authenticated** Node.js function that responds to HTTP requests, and use a 10 minute timeout to showcase how a function can have more time to respond to HTTP requests.

### Create

1. In the Cloud Shell terminal, run the following command to create the folder and files for the app and navigate to the folder:
    

```apache
mkdir ~/hello-http && cd $_
touch index.js && touch package.json
```

2. Click the **Open Editor** button on the toolbar of Cloud Shell.
    
3. Add the following code to the `hello-http/index.js` file that simply responds to HTTP requests:
    

```go
const functions = require('@google-cloud/functions-framework');

functions.http('helloWorld', (req, res) => {
  res.status(200).send('HTTP with Node.js in GCF 2nd gen!');
});
```

4. Add the following content to the `hello-http/package.json` file to specify the dependencies.
    

```json
{
  "name": "nodejs-functions-gen2-codelab",
  "version": "0.0.1",
  "main": "index.js",
  "dependencies": {
    "@google-cloud/functions-framework": "^2.0.0"
  }
}
```

5. In the file Explorer, open the `index.js` file. This action enables Gemini Code Assist, as indicated by the presence of the icon in the upper-right corner of the editor.
    
6. Click the **Gemini Code Assist: Smart Actions** icon and select **Explain this**.
    
7. Gemini Code Assist opens a chat pane with the prefilled prompt of `Explain this`. In the inline text box of the Code Assist chat, replace the prefilled prompt with the following, and click **Send**:
    

```apache
You are an expert Cloud Developer at Cymbal AI. A new team member is unfamiliar with this Cloud Function implementation. Explain the "index.js" and "package.json" files within the "hello-http" folder in detail, breaking down their key components used in the application code.

For the suggested improvements, don't update these files.
```

Detailed explanations for the `index.js` and `package.json` code appears in the **Gemini Code Assist** chat.

### Deploy

1. In the Cloud Shell terminal, run the following command to deploy the function and enter `y` for the **unauthenticated invocations** pop-up:
    

```apache
gcloud functions deploy nodejs-http-function \
  --gen2 \
  --runtime nodejs22 \
  --entry-point helloWorld \
  --source . \
  --region us-central1 \
  --trigger-http \
  --timeout 600s \
  --max-instances 1
```

**Note:** If you get a `permissions error`, please wait a few minutes and try the deployment again. It takes a few minutes for the APIs to be enabled.

Although not strictly necessary for this step, there is a timeout of 600 seconds. This allows the function to have a longer timeout to respond to HTTP requests.

2. Once the function is deployed, in the search bar enter **Cloud Run functions**, and click on the result; you are redirected to the **Cloud Run** console. Verify the function was deployed successfully.
    

![verify function was deployed](https://cdn.qwiklabs.com/6sZ4ea9KieO27cTdCXUjDYQ3eaEiSP9RBOVpTjOAz1E%3D align="left")

### Test

* Test the function with the following command:
    

```apache
gcloud functions call nodejs-http-function \
  --gen2 --region us-central1
```

You should see the following message as a response:

```apache
HTTP with Node.js in GCF 2nd gen!
```

Click **Check my progress** to verify the objective.

Create an HTTP function

## Task 3. Create a Cloud Storage function

In this section, you create a Node.js function that responds to events from a Cloud Storage bucket.

### Set up

* To use Cloud Storage functions, first run the following code to grant the `pubsub.publisher` IAM role to the Cloud Storage service account:
    

```apache
PROJECT_NUMBER=$(gcloud projects list --filter="project_id:qwiklabs-gcp-02-6e1c9205e771" --format='value(project_number)')
SERVICE_ACCOUNT=$(gsutil kms serviceaccount -p $PROJECT_NUMBER)

gcloud projects add-iam-policy-binding qwiklabs-gcp-02-6e1c9205e771 \
  --member serviceAccount:$SERVICE_ACCOUNT \
  --role roles/pubsub.publisher
```

### Create

1. Run the following command to create the folder and files for the app, and navigate to the folder:
    

```apache
mkdir ~/hello-storage && cd $_
touch index.js && touch package.json
```

2. Click the **Open Editor** button on the toolbar of Cloud Shell.
    
3. Add the following code to the `hello-storage/index.js` file that simply responds to Cloud Storage events:
    

```apache
const functions = require('@google-cloud/functions-framework');

functions.cloudEvent('helloStorage', (cloudevent) => {
  console.log('Cloud Storage event with Node.js in GCF 2nd gen!');
  console.log(cloudevent);
});
```

4. Add the following content to the `hello-storage/package.json` file to specify the dependencies:
    

```json
{
  "name": "nodejs-functions-gen2-codelab",
  "version": "0.0.1",
  "main": "index.js",
  "dependencies": {
    "@google-cloud/functions-framework": "^2.0.0"
  }
}
```

5. In the file Explorer, open the `index.js` file within the "hello-storage" folder. This action enables Gemini Code Assist, as indicated by the presence of the icon in the upper-right corner of the editor.
    
6. Click the **Gemini Code Assist: Smart Actions** icon and select **Explain this**.
    
7. Gemini Code Assist opens a chat pane with the prefilled prompt of `Explain this`. In the inline text box of the Code Assist chat, replace the prefilled prompt with the following, and click **Send**:
    

```apache
You are an expert Cloud Developer at Cymbal AI. A new team member is unfamiliar with this Cloud Function implementation. Explain the "index.js" and "package.json" files within the "hello-storage" folder in detail, breaking down their key components used in the application code.

For the suggested improvements, don't update these files.
```

Detailed explanations for the `index.js` and `package.json` code appears in the **Gemini Code Assist** chat.

### Deploy

1. First, run the following command to create a Cloud Storage bucket to use for creating events:
    

```apache
BUCKET="gs://gcf-gen2-storage-qwiklabs-gcp-02-6e1c9205e771"
gsutil mb -l us-central1 $BUCKET
```

2. Run the following command to deploy the function:
    

```apache
gcloud functions deploy nodejs-storage-function \
  --gen2 \
  --runtime nodejs22 \
  --entry-point helloStorage \
  --source . \
  --region us-central1 \
  --trigger-bucket $BUCKET \
  --trigger-location us-central1 \
  --max-instances 1
```

**Note:** As before, if you get a `permissions error`, please wait a few minutes and try the deployment again. It can take a few minutes for the APIs to be enabled.

3. Once the function is deployed, verify that you can see it under the **Cloud Run** console.
    

![two functions](https://cdn.qwiklabs.com/JL6%2FM88FSKXcP4uAkBzK%2B0BF9cC99qY5VWLZA2BiN5w%3D align="left")

### Test

1. Run the following command that uploads a file to the bucket to test the function:
    

```apache
echo "Hello World" > random.txt
gsutil cp random.txt $BUCKET/random.txt
```

2. Run the following command. You should see the received CloudEvent in the logs:
    

```apache
gcloud functions logs read nodejs-storage-function \
  --region us-central1 --gen2 --limit=100 --format "value(log)"
```

**Note:** It may take a minute to generate logs.

You should see output similar to the following.

**Output:**

```apache
}
  traceparent: '00-c74cb472d1e78f7225b6f617a31d9c08-96f0380bb62be2c1-01'
  },
    etag: 'CKOx1L3wofoCEAE='
    crc32c: 'R1jUOQ==',
    mediaLink: 'https://storage.googleapis.com/download/storage/v1/b/gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb/o/random.txt?generation=1663625646643363&alt=media',
    md5Hash: '5Z/5eUEET4XfUpfhwwLSYA==',
    size: '12',
    timeStorageClassUpdated: '2022-09-19T22:14:06.657Z',
    storageClass: 'STANDARD',
    updated: '2022-09-19T22:14:06.657Z',
    timeCreated: '2022-09-19T22:14:06.657Z',
    contentType: 'text/plain',
    metageneration: '1',
    generation: '1663625646643363',
    bucket: 'gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb',
    name: 'random.txt',
    selfLink: 'https://www.googleapis.com/storage/v1/b/gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb/o/random.txt',
    id: 'gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb/random.txt/1663625646643363',
    kind: 'storage#object',
  data: {
  bucket: 'gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb',
  time: '2022-09-19T22:14:06.657124Z',
  subject: 'objects/random.txt',
  type: 'google.cloud.storage.object.v1.finalized',
  specversion: '1.0',
  source: '//storage.googleapis.com/projects/_/buckets/gcf-gen2-storage-qwiklabs-gcp-00-101b4b9ca9bb',
  id: '5693030851428996',
{
Cloud Storage event with Node.js in GCF 2nd gen!
```

Click **Check my progress** to verify the objective.

Create a Cloud Storage function

## Task 4. Create a Cloud Audit Logs function

In this section, you create a Node.js function that receives a [Cloud Audit Log](https://cloud.google.com/logging/docs/audit) event when a Compute Engine VM instance is created. In response, it adds a label to the newly created VM, specifying the creator of the VM.

### Determine newly created Compute Engine VMs

Compute Engine emits 2 Audit Logs when a VM is created.

The first one is emitted at the beginning of the VM creation and looks like this:

![audit log 1](https://cdn.qwiklabs.com/qHSG8p8wtqFWTgj3TLrB15M59avOo0XZ8HEVjSc2j5U%3D align="left")

The second one is emitted after the VM creation and looks like this:

![audit log 2](https://cdn.qwiklabs.com/VM8dvwhzbz4ZnShGiS%2FRAdhic8y4SXJMLyb9In5Gze4%3D align="left")

Notice the operation field with the `first: true` and `last: true` values. The second Audit Log contains all the information you need to label an instance, therefore you use the `last: true` flag to detect it in Cloud Run functions.

### Set up

To use Cloud Audit Log functions, you must enable Audit Logs for Eventarc. You also need to use a service account with the `eventarc.eventReceiver` role.

1. From the Navigation menu, go to **IAM & Admin** &gt; **Audit Logs**.
    

**Note:** you can ignore the missing `resourcemanager.folders.getIamPolicy` permission warning.

2. Find the **Compute Engine API** and click the checkbox next to it. If you are unable to find the API, search for it on the next page.
    
3. On the info pane on the right, select the checkboxes for the **Admin Read**, **Data Read**, and **Data Write** log types, then click **Save**.
    

![check admin read, data read, data write](https://cdn.qwiklabs.com/pSeTB3u5RH5TaSsE6gExLGeiKvMhma30OZsFmPov%2BuQ%3D align="left")

4. Grant the default Compute Engine service account the `eventarc.eventReceiver` IAM role:
    

```apache
gcloud projects add-iam-policy-binding qwiklabs-gcp-02-6e1c9205e771 \
  --member serviceAccount:$PROJECT_NUMBER-compute@developer.gserviceaccount.com \
  --role roles/eventarc.eventReceiver
```

### Get the code

1. Run the following code to clone the repo that contains the application:
    

```apache
cd ~
git clone https://github.com/GoogleCloudPlatform/eventarc-samples.git
```

2. Click the **Open Editor** button on the toolbar of Cloud Shell.
    
3. In the file Explorer, navigate to **eventarc-samples** &gt; **gce-vm-labeler** &gt; **gcf** &gt; **nodejs** &gt; **index.js**.
    
4. Open the `index.js` file. This action enables Gemini Code Assist, as indicated by the presence of the icon in the upper-right corner of the editor.
    
5. Click the **Gemini Code Assist: Smart Actions** icon and select **Explain this**.
    
6. Gemini Code Assist opens a chat pane with the prefilled prompt of `Explain this`. In the inline text box of the Code Assist chat, replace the prefilled prompt with the following, and click **Send**:
    

```apache
You are an expert Cloud Developer at Cymbal AI. A new team member is unfamiliar with this Cloud Function implementation. Explain the "index.js" file in detail, breaking down its key components used in the application code.

For the suggested improvements, don't update the file.
```

The [`index.js`](https://github.com/GoogleCloudPlatform/eventarc-samples/blob/main/gce-vm-labeler/gcf/nodejs/index.js) file contains the application code that receives the Audit Log wrapped into a CloudEvent. It then extracts the Compute Engine VM instance details and sets a label on the VM instance. Feel free to study [`index.js`](https://github.com/GoogleCloudPlatform/eventarc-samples/blob/main/gce-vm-labeler/gcf/nodejs/index.js) in more detail on your own.

Detailed explanations for the `index.js` code appears in the **Gemini Code Assist** chat.

### Deploy

1. In the Cloud Shell terminal, run the following command to navigate to the app directory:
    

```apache
cd ~/eventarc-samples/gce-vm-labeler/gcf/nodejs
```

2. Run the following command to deploy the function with `gcloud` as before. Notice how the function is filtering on Audit Logs for Compute Engine insertions with the `--trigger-event-filters` flag:
    

```apache
gcloud functions deploy gce-vm-labeler \
  --gen2 \
  --runtime nodejs22 \
  --entry-point labelVmCreation \
  --source . \
  --region us-central1 \
  --trigger-event-filters="type=google.cloud.audit.log.v1.written,serviceName=compute.googleapis.com,methodName=beta.compute.instances.insert" \
  --trigger-location us-central1 \
  --max-instances 1
```

**Note**: Although your Audit Log function trigger is created immediately, it can take up to 10 minutes for triggers to be fully functional.

Click **Check my progress** to verify the objective.

Create a Cloud Audit Logs function

### Test

To test your Audit Log function, you need to create a Compute Engine VM in the Cloud console. (You can also create VMs with `gcloud` but it does not seem to generate Audit Logs.)

1. From the Navigation menu, go to **Compute Engine &gt; VM instances**.
    
2. Click **Create Instance**, set the instance **Name** as **instance-1** and the **Zone** as `us-central1-c`.
    
3. Leave all of the other fields as their defaults and click **Create**.
    

Once the VM creation completes, you should see the added `creator` label on the VM in the Cloud console in the **Basic information** section.

![creator label](https://cdn.qwiklabs.com/%2FiLfq8g1pAkZKTawwktH9kVMFTfAtjZeD94FbdsIsw4%3D align="left")

4. Verify using the following command:
    

```apache
gcloud compute instances describe instance-1 --zone us-central1-c
```

You should see the label in the output like in the following example.

**Output example:**

```apache
...
labelFingerprint: ULU6pAy2C7s=
labels:
  creator: student-02-19b599a0f901
...
```

Click **Check my progress** to verify the objective.

Create a VM instance

5. Run the following command to delete the VM. Type `Y`, when prompted, to confirm.
    

```apache
gcloud compute instances delete instance-1 --zone us-central1-c
```

## Task 5. Deploy different revisions

Cloud Run functions supports multiple revisions of your functions, splitting traffic between different revisions, and rolling your function back to a prior version. In this section, you deploy two revisions of your Cloud Run function.

### Create

1. Run the following command to create the folder and files for the app and navigate to the folder:
    

```apache
mkdir ~/hello-world-colored && cd $_
touch main.py
touch requirements.txt
```

2. Add the following code to the `hello-world-colored/main.py` file with a Python function that reads a color environment variable and responds with `Hello World` in that background color:
    

```go
import os

color = os.environ.get('COLOR')

def hello_world(request):
    return f'<body style="background-color:{color}"><h1>Hello World!</h1></body>'
```

### Deploy

1. Run the following command to deploy the first revision of the function with an orange background:
    

```apache
COLOR=orange
gcloud functions deploy hello-world-colored \
  --gen2 \
  --runtime python39 \
  --entry-point hello_world \
  --source . \
  --region us-central1 \
  --trigger-http \
  --allow-unauthenticated \
  --update-env-vars COLOR=$COLOR \
  --max-instances 1
```

At this point, if you test the function by viewing the HTTP trigger (the URI output of the previously executed deployment command) in your browser, you should see `Hello World` with an orange background:

![orange hello world](https://cdn.qwiklabs.com/3WNBe97ARMDXFdVHomB5BADvbszuE7KXA8rSacHUOLk%3D align="left")

2. Navigate to the **Cloud Run functions** page, you are redirected to the **Cloud Run** console; click the **hello-world-colored** function.
    
3. Click **Edit & Deploy New Revision**.
    
4. Leave everything as their defaults, scroll down, select the **Variables & Secrets** tab. In the **Environment Variables** section, update the **COLOR** environment variable to `yellow`.
    

![function revision](https://cdn.qwiklabs.com/3MsPocnJEaIfrGUNPVz%2Bqf4eAJnZHd6HPAn1BBsMvS0%3D align="left")

5. Click **Deploy**.
    

Since this is the latest revision, if you test the function, you should see `Hello World` with a yellow background:

![yellow hello world](https://cdn.qwiklabs.com/xoe%2BB97W092RvG9fdH%2B4EPUu4V%2BJFqm39DW38IAH32k%3D align="left")

Click **Check my progress** to verify the objective.

Deploy different revisions

## Task 6. Set up minimum instances

In Cloud Run functions, one can specify a minimum number of function instances to be kept warm and ready to serve requests. This is useful in limiting the number of cold starts. In this section, you deploy a function with slow initialization. You observe the cold start problem. Then, you deploy the function with the minimum instance value set to 1 to get rid of the cold start.

### Create

1. Run the following command to create the folder and files for the app and navigate to the folder:
    

```apache
mkdir ~/min-instances && cd $_
touch main.go
touch go.mod
```

2. Add the following code to the `min-instances/main.go` file. This Go service has an `init` function that sleeps for 10 seconds to simulate a long initialization. It also has a `HelloWorld` function that responds to HTTP calls:
    

```go
package p

import (
        "fmt"
        "net/http"
        "time"
)

func init() {
        time.Sleep(10 * time.Second)
}

func HelloWorld(w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "Slow HTTP Go in GCF 2nd gen!")
}
```

3. Add the following code to the `min-instances/go.mod` file. This specifies the module path and Go language version:
    

```apache
module example.com/mod

go 1.23
```

### Deploy

1. Run the following command to deploy the first revision of the function with the default minimum instance value of zero:
    

```apache
gcloud functions deploy slow-function \
  --gen2 \
  --runtime go123 \
  --entry-point HelloWorld \
  --source . \
  --region us-central1 \
  --trigger-http \
  --allow-unauthenticated \
  --max-instances 4
```

2. Test the function with this command:
    

```apache
gcloud functions call slow-function \
  --gen2 --region us-central1
```

You should observe a 10 second delay (cold start) on the first call and then see the message. Subsequent calls should return immediately.

```apache
Slow HTTP Go in GCF 2nd gen!
```

### Set minimum instances

To get rid of the cold start on the first request, redeploy the function with the `--min-instances` flag set to 1 as follows:

1. Navigate to the **Cloud Run** page in the console and click the **slow-function** service.
    
2. Click **Edit & Deploy New Revision**.
    
3. Under the **Revision scaling** section, set **Minimum number of instances** to `1` and **Maximum number of instances** to `4`.
    
4. Leave the rest of the fields as their defaults and click **Deploy**.
    

### Test

* Run the following command to test the function again:
    

```apache
gcloud functions call slow-function \
  --gen2 --region us-central1
```

You should not see the 10 second delay anymore in the first request. The cold start problem for the first invocation (after a long time without) is gone, thanks to minimum instances! Refer to the [using minimum instances](https://cloud.google.com/run/docs/configuring/min-instances) documentation for more information.

Click **Check my progress** to verify the objective.

Set up minimum instances

## Task 7. Create a function with concurrency

In Cloud Run functions, a function instance handles one concurrent request by default but you can specify the number of concurrent requests that can be processed simultaneously by an instance. This can also be useful in preventing cold starts because a new function instance does not need to be created for every parallel request.

In this section, you use the function with slow initialization from the previous step. You send it 10 requests and observe the cold start problem again as new function instances need to be created to handle the requests.

To fix the cold-start problem, you deploy another function with a concurrency value of 100. You observe that the 10 requests now do not cause the cold start problem and a single function instance can handle all of the requests.

### Test without concurrency

1. Use the following Install command to install `hey` in the Cloud Shell:
    

```apache
sudo apt install hey
```

2. Run the following command to get the URL of the function and save it as an environment variable:
    

```apache
SLOW_URL=$(gcloud functions describe slow-function --region us-central1 --gen2 --format="value(serviceConfig.uri)")
```

3. Use an open source benchmarking tool called [`hey`](https://github.com/rakyll/hey) to send 10 concurrent requests to the slow function.
    

```apache
hey -n 10 -c 10 $SLOW_URL
```

You should see in the output of `hey` that some requests are taking long.

**Output:**

```apache
Summary:
  Total:        10.9053 secs
  Slowest:      10.9048 secs
  Fastest:      0.4439 secs
  Average:      9.7930 secs
  Requests/sec: 0.9170

  Total data:   310 bytes
  Size/request: 31 bytes

Response time histogram:
  0.444 [1]     |■■■■
  1.490 [0]     |
  2.536 [0]     |
  3.582 [0]     |
  4.628 [0]     |
  5.674 [0]     |
  6.720 [0]     |
  7.767 [0]     |
  8.813 [0]     |
  9.859 [0]     |
  10.905 [9]    |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
```

This is because more function instances are being created to handle the requests. If you check the active instances count for the function, you should also see that more than one instance was created as some point and these are causing the cold start problem.

![active instances graph](https://cdn.qwiklabs.com/NJLjn6N4cjhCCEUbCkKBAYFxQP0wCMjE7L5Yr1VYqfI%3D align="left")

4. Run the following command to delete the function. Type `Y`, when prompted, to confirm.
    

```apache
gcloud run services delete slow-function --region us-central1
```

### Deploy

* Run the following command to deploy a new function identical to the previous function. Once deployed, you increase its concurrency:
    

```apache
gcloud functions deploy slow-concurrent-function \
  --gen2 \
  --runtime go123 \
  --entry-point HelloWorld \
  --source . \
  --region us-central1 \
  --trigger-http \
  --allow-unauthenticated \
  --min-instances 1 \
  --max-instances 4
```

### Set concurrency

Now you set the concurrency of the underlying Cloud Run service for the function to `100` (it can be a maximum of 1000). This ensures that at least 100 requests can be handled by a single function instance.

1. From the Navigation menu, go to **Cloud Run**.
    
2. Click the **slow-concurrent-function** service.
    
3. Click **Edit & Deploy New Revision**.
    
4. Under the **Resources** section, set the **CPU** to `1`.
    
5. Under **Requests**, set the **Maximum concurrent requests per instance** to `100`.
    
6. Under **Revision scaling**, set the **Maximum number of instances** to `4`.
    
7. Leave the rest of the fields as their defaults and click **Deploy**.
    

### Test with concurrency

1. Once your function has deployed, run the following command to get the URL of the new function and save it as an environment variable:
    

```apache
SLOW_CONCURRENT_URL=$(gcloud functions describe slow-concurrent-function --region us-central1 --gen2 --format="value(serviceConfig.uri)")
```

2. Now use [`hey`](https://github.com/rakyll/hey) to send 10 concurrent requests:
    

```apache
hey -n 10 -c 10 $SLOW_CONCURRENT_URL
```

You should see in the output of `hey` that all requests are processed quickly.

**Output:**

```apache
Summary:
  Total:        0.0652 secs
  Slowest:      0.0651 secs
  Fastest:      0.0619 secs
  Average:      0.0636 secs
  Requests/sec: 153.3751

  Total data:   280 bytes
  Size/request: 28 bytes

Response time histogram:
  0.062 [1]     |■■■■■■■■■■■■■■■■■■■■
  0.062 [1]     |■■■■■■■■■■■■■■■■■■■■
  0.063 [0]     |
  0.063 [0]     |
  0.063 [0]     |
  0.063 [1]     |■■■■■■■■■■■■■■■■■■■■
  0.064 [2]     |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.064 [2]     |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.064 [2]     |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.065 [0]     |
  0.065 [1]     |■■■■■■■■■■■■■■■■■■■■
```

A single function instance was able to handle all the requests and the cold start problem is gone, thanks to the increased concurrency! Refer to the [concurrency](https://cloud.google.com/run/docs/configuring/concurrency) documentation for more information.

Click **Check my progress** to verify the objective.

Create a function with concurrency

---

## Solution of Lab

### New solution

```apache
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP1089/lab.sh
source lab.sh
```

Open: https://console.cloud.google.com/run/

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770966357872/747b2269-44eb-4649-aea5-3a6de5246292.png align="center")

---

### Old solution

%[https://youtu.be/dgalGdwxL2U] 

```apache
curl -LO raw.githubusercontent.com/ePlus-DEV/storage/refs/heads/main/labs/GSP1089/old-lab.sh
source old-lab.sh
```

**Script Alternative**

```apache
curl -LO raw.githubusercontent.com/prateekrajput08/Arcade-Google-Cloud-Labs/refs/heads/main/Cloud%20Run%20Functions%3A%20Qwik%20Start/TechCode.sh
sudo chmod +x TechCode.sh 
./TechCode.sh
```

Open: https://console.cloud.google.com/run/

**Task 5:**

```apache
yellow
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770956412401/fcdcb7a7-b945-44c5-91e8-4de6530a28db.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770957110623/d9f52d88-a7c8-477a-87f7-fe80b627c484.png align="center")

**Task 6:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770714662421/71b2ef68-1a55-4113-999e-5223725bb65b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770714670619/28813ceb-6502-4eff-914b-d30548995fc3.png align="center")

**Task 7:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770955084899/3a8d337a-252f-45f3-846e-f6ba8f121c10.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1770955095850/b2b1bff6-9e87-48f0-b68c-6234a539a82d.png align="center")

---

### Manual

%[https://youtu.be/Yf3LgNaV9gg]
