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:
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
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:
Launch the Cloud Shell Editor.
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.
If you aren't already connected to Google Cloud, in the Cloud Code status bar, click Connect to Google Cloud.
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.
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.
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:
In the Cloud Code status bar, click Cloud Code.
In the Quick Pick menu that appears, select New Application.
Select Cloud Functions application as the type of sample application.
From the list of sample Cloud Run functions apps, select Node.js: Hello World.
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:
Click Cloud Code and then expand the Cloud Functions explorer.
Click a function and then click
Deploy function.In the Quickpick menu, select a Google Cloud project to deploy your function to.
Select the region that the function will be deployed to.
Select Node.js 20.
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.
(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, andpackage.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 allows you to execute the deployed function in Google Cloud console from the Cloud Shell Editor's terminal. To invoke a function, follow these steps:
Right-click the function and select Invoke function via HTTP.
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:
Click Cloud Code and then expand the Cloud Functions explorer.
Expand your Google Cloud project snippet, right-click the function that you wish to download, and select Download to new workspace.
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:
In the Cloud Functions explorer, right-click the function name and then select Open in Cloud Console.
Click Delete and then click Delete.
To delete your project (and associated resources):
Go to the Projects page in the Google Cloud console:
Select the project that you created for this quickstart and then click Delete.
Type the project ID to confirm and then click Shut down.
This shuts down the project and schedules it for deletion.
What's next
- Learn more about managing functions in your Cloud Shell workspace or running locally with Functions Framework.
- Learn more about writing Cloud Run functions.
- Learn more about deploying Cloud Run functions.
- Learn more about calling Cloud Run functions.
- Learn more about monitoring Cloud Run functions.
- Learn more about running Cloud Run functions locally.
- Explore the more advanced Cloud Run functions tutorials.