This page shows you how to create, download, deploy, and invoke a 2nd gen HTTP function using Cloud Functions with Cloud Code.
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.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.
-
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.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.
- Install Git. Git is required for copying samples to your machine.
- Install the Cloud Code plugin if you haven't already.
Create a function
If you've created a function before, you can download an existing function to a new workspace and edit it locally.
To create a new function using a sample, follow these steps:
Click Cloud Code and then expand the Cloud Functions section.
Click add Create function and select a template.
Navigate to the pathway that you'd like to create your new function in, enter a name for the function, and select Create New Application.
If the folder of your application doesn't appear automatically in the Explorer, click refresh Refresh.
The
launch.json
file 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", } ] }
To learn more about creating multiple functions in a single workspace, see Manage functions in VS Code workspace.
Download an existing function to a new workspace
To download a function to a new workspace, follow these steps:
Click Cloud Code and then expand the Cloud Functions section.
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 click Create New Application.
This downloads the function to your local machine and opens it in a new workspace in your IDE.
Deploy a function
To deploy a function, follow these steps:
Right-click a function and select Deploy function.
In the Quickpick menu, select a Google Cloud project to deploy your function to.
Select a region that the function will be deployed to.
Select a runtime.
The function's deployment may take a few minutes. While in progress, a progress_activity progress indicator appears. After it is complete, a check_circle checkmark appears. If it fails, an error error mark appears.
If the deployment fails, refer to the Output tab 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 a function
Invoking a function allows you to execute the deployed function in Google Cloud console from the IDE's built-in 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 that appears, select one of the two options:
Default: A terminal opens in the IDE and runs a command to execute the function in Google Cloud.
Import arguments from JSON file: This option takes the content of the selected JSON file, turns it into a string, and passes it with
-d '{
. This is useful for when you have an input to your functions and want to save it, allowing you to execute it without having to retype it repeatedly.
What's next
- See the Work with Cloud Functions overview to learn more about managing your workspace or setting up a local development environment.