Create a 2nd gen Cloud Run function by using Cloud Code for Cloud Shell

Learn how to create, deploy, invoke, and download an HTTP Cloud Run function using Cloud Code in the Cloud Shell Editor.

Cloud Code is an extension that brings many Google Cloud services, like Google Kubernetes Engine and Cloud Run, directly into your IDE, so you can develop your applications without having to context switch. Cloud Code is available by default in the Cloud Shell Editor.

You can install the Cloud Code extension in VS Code, which also supports creating and deploying functions, or IntelliJ and other supported JetBrains IDEs, which don't support Cloud Run functions.


To follow step-by-step guidance for this task directly in the Cloud Shell Editor, click Guide me:

Guide me


Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

Connect to Google Cloud

In this section, you'll launch the Cloud Shell Editor, connect to Google Cloud, and select a Google Cloud project.

To connect to Google Cloud, follow these steps:

  1. Launch the Cloud Shell Editor.

  2. If prompted to authorize Cloud Code to use your credentials to make a Google Cloud API call, click Authorize if you agree to the terms.

  3. If you aren't already connected to Google Cloud, in the Cloud Code status bar, click Connect to Google Cloud.

    Connect to Google Cloud button in the status bar

  4. If you're prompted to authorize Cloud Code to use your credentials to make Google Cloud API calls, click Authorize if you agree to these terms.

  5. If you don't already have a Google Cloud project selected, in the Cloud Code status bar, click Select a Google Cloud Project.

    If you already have a Google Cloud project selected, you'll see the project name in the Cloud Code status bar and can proceed to create your function.

  6. In the Select a Google Cloud Project Quick Pick menu that appears, select your Google Cloud project.

    The project loads and becomes the active project in Cloud Shell Editor. You can switch this project at any time by clicking the active project name in the Cloud Code status bar and selecting a different project.

Create your function

Use the Cloud Shell Editor as your environment for creating and deploying your function. The editor comes preloaded with the tools needed for cloud development.

To create a new function using a sample, follow these steps:

  1. In the Cloud Code status bar, click Cloud Code.

    Cloud Code status bar button

  2. In the Quick Pick menu that appears, select New Application.

  3. Select Cloud Functions application as the type of sample application.

  4. From the list of sample Cloud Run functions apps, select Node.js: Hello World.

  5. Select a folder for your app location and then click Create New Application.

    Cloud Shell Editor loads your app in a new workspace. After it reloads, your app is accessible with the Explorer view.

    The launch.json file, located in the .vscode folder, contains the required configurations for your function:

    {
    "configurations": [
        {
        "name": "Deploy Cloud Function",
        "type": "cloudcode.cloudfunctions",
        "request": "launch",
        "functionName": "function-hello-world",
        "gen": "GEN_2",
        "entryPoint": "helloHttp",
        }
    ]
    }
    

Deploy your function

Now that you've created your function, you can deploy it in Cloud Shell by following these steps:

  1. Click Cloud Code and then expand the Cloud Functions explorer.

  2. Click a function and then click Deploy function.

  3. In the Quickpick menu, select a Google Cloud project to deploy your function to.

  4. Select the region that the function will be deployed to.

  5. Select Node.js 22.

    The function's deployment may take a few minutes. If it is still in progress, you'll see a progress icon. After it is complete, you'll either see the success icon if it succeeds, the error icon if it fails, or the warning icon if the deployment fails and reverts to a previously successful deployment.

    If the deployment fails, refer to the Output (View > Output) view for the error message. Clicking the link takes you to the build logs in Google Cloud console and provides more detail about the error.

  6. (Optional) Once the function is deployed, you can perform the following actions:

    • To view read-only files of your deployed function, expand the function and then expand the Source Code folder. Here you can view launch.json under .vscode, the deployed code, and package.json.

    • To view the function in Google Cloud console, right-click the function and select Open in Cloud Console, and if prompted, select Open.

    • To edit the metadata of your function, right-click the function and select Edit metadata in Cloud Console.

    • To view logs of the deployed function, right-click the function and select View logs.

Invoke your function

Invoking a function lets you execute the deployed function in Google Cloud console from the Cloud Shell Editor's terminal. To invoke a function, follow these steps:

  1. Right-click the function and select Invoke function via HTTP.

  2. In the Trigger function via HTTP Quickpick menu that appears, select Invoke with no arguments.

    A terminal opens in the IDE and runs a command to execute the function in Google Cloud.

Optional: Download an existing function to a new workspace

To download an existing function to a new workspace, follow these steps:

  1. Click Cloud Code and then expand the Cloud Functions explorer.

  2. Expand your Google Cloud project snippet, right-click the function that you want to download, and select Download to new workspace.

  3. When prompted, enter a name for the new application and then click Create New Application.

    This downloads the function to your local machine and opens it in a new workspace in Cloud Shell Editor.

Clean up

To delete only the function you created for this quickstart:

  1. In the Cloud Functions explorer, right-click the function name and then select Open in Cloud Console.

  2. Click Delete and then click Delete.

To delete your project (and associated resources):

  1. Go to the Projects page in the Google Cloud console:

    Go to the Projects page

  2. Select the project that you created for this quickstart and then click Delete.

  3. Type the project ID to confirm and then click Shut down.

    This shuts down the project and schedules it for deletion.

What's next