Create and deploy a function with Cloud Code for VS Code

Stay organized with collections Save and categorize content based on your preferences.

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

  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 Cloud project. Learn how to check if billing is enabled on a project.

  4. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.

    Enable the APIs

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

    Go to project selector

  6. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  7. Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.

    Enable the APIs

  8. Install Git. Git is required for copying samples to your machine.
  9. 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:

  1. Click Cloud Code icon Cloud Code and then expand the Cloud Functions section.

  2. Click + Create function and select a template.

  3. 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 Kubernetes cluster view 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",
        }
      ]
    }
    

Download an existing function to a new workspace

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

  1. Click Cloud Code icon Cloud Code and then expand the Cloud Functions section.

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

  3. 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:

  1. Right-click a function and select Deploy function.

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

  3. Select a region that the function will be deployed to.

  4. Select a runtime.

    The function's deployment may take a few minutes. If it is still in progress, you'll see a Progress icon progress icon. Once it is complete, you'll see the Success icon success icon if it succeeds, or the Error icon error icon if it fails.

    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.

    Error message output

  5. (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 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:

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

  2. 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, stringifies it, 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

Get Support

To send feedback, report issues on GitHub, or ask a question on Stack Overflow.