Crea ed esegui il deployment di una funzione con Cloud Code for VS Code
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Questa pagina mostra come creare, scaricare, eseguire il deployment e richiamare una funzione HTTP di 2ª generazione. utilizzando Cloud Functions con Cloud Code.
Prima di iniziare
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.
Scaricare una funzione esistente in una nuova area di lavoro
Per scaricare una funzione in un nuovo spazio di lavoro:
Fai clic su Cloud Code ed espandi la sezione
Cloud Functions.
Espandi lo snippet del progetto Google Cloud , fai clic con il tasto destro del mouse sulla funzione che vuoi scaricare e seleziona Scarica in un nuovo workspace.
Quando richiesto, inserisci un nome per la nuova applicazione e fai clic su
Crea nuova applicazione.
In questo modo la funzione viene scaricata sulla macchina locale e viene aperta in una nuova area di lavoro nell'IDE.
Esegui il deployment di una funzione
Per eseguire il deployment di una funzione:
Fai clic con il tasto destro del mouse su una funzione e seleziona Esegui il deployment della funzione.
Nel menu Scelta rapida, seleziona un progetto Google Cloud in cui eseguire il deployment della funzione.
Seleziona una regione in cui verrà eseguito il deployment della funzione.
Seleziona un runtime.
Il deployment della funzione potrebbe richiedere alcuni minuti. Durante l'avanzamento,
viene visualizzato un indicatore di avanzamento
progress_activity. Al termine, viene visualizzato un
segno di spunta check_circle. Se non riesce, viene visualizzato un
errore.
Se il deployment non riesce, fai riferimento alla scheda Output per il messaggio di errore.
Facendo clic sul link vengono visualizzati i log di build nella console Google Cloud e vengono forniti ulteriori dettagli sull'errore.
(Facoltativo) Una volta eseguito il deployment della funzione, puoi eseguire queste azioni:
Per visualizzare i file di sola lettura della funzione di cui hai eseguito il deployment, espandi la funzione, quindi espandi la cartella Codice sorgente. Qui puoi visualizzare
launch.json in .vscode, il codice di cui è stato eseguito il deployment e package.json.
Per visualizzare la funzione nella console Google Cloud , fai clic con il tasto destro del mouse sulla funzione e seleziona Apri nella console Cloud e, se richiesto, seleziona Apri.
Per modificare i metadati della funzione, fai clic con il tasto destro del mouse sulla funzione e seleziona Modifica metadati nella console Cloud.
Per visualizzare i log della funzione di cui è stato eseguito il deployment, fai clic con il tasto destro del mouse sulla funzione e seleziona Visualizza log.
Richiamare una funzione
Richiamare una funzione consente di eseguire la funzione di cui è stato eseguito il deployment nella consoleGoogle Cloud dal terminale integrato dell'IDE. Per richiamare una funzione,
segui questi passaggi:
Fai clic con il tasto destro del mouse sulla funzione e seleziona Richiama funzione tramite HTTP.
Nel menu di scelta rapida Attiva funzione tramite HTTP che viene visualizzato, seleziona una delle due opzioni:
Predefinito: un terminale si apre nell'IDE ed esegue un comando per eseguire la funzione in Google Cloud.
Importa argomenti dal file JSON: questa opzione prende il contenuto del file JSON selezionato, lo trasforma in una stringa e lo invia con -d '{. Questa opzione è utile quando hai un input per le tue funzioni e
vuoi salvarlo, in modo da poterlo eseguire senza doverlo digitare ripetutamente.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-09-04 UTC."],[[["\u003cp\u003eThis page guides users on how to manage 2nd gen HTTP functions using Cloud Functions with Cloud Code, covering creation, downloading, deployment, and invocation.\u003c/p\u003e\n"],["\u003cp\u003eNew functions can be created from templates within Cloud Code, allowing users to define function names and create new applications within specified pathways.\u003c/p\u003e\n"],["\u003cp\u003eExisting functions can be downloaded from Google Cloud to a new workspace on a local machine, allowing users to then edit locally.\u003c/p\u003e\n"],["\u003cp\u003eFunctions are deployed to a user-specified Google Cloud project, region, and runtime, with progress indicators and options to view source code, logs, or edit metadata.\u003c/p\u003e\n"],["\u003cp\u003eDeployed functions can be invoked directly from the IDE via HTTP, either with default settings or by importing arguments from a JSON file.\u003c/p\u003e\n"]]],[],null,["# Create and deploy a function with Cloud Code for VS Code\n\nThis page shows you how to create, download, deploy, and invoke a 2nd gen HTTP\nfunction using [Cloud Functions](https://cloud.google.com/functions/) with\nCloud Code.\n\nBefore you begin\n----------------\n\n- Sign in to your Google Cloud account. If you're new to Google Cloud, [create an account](https://console.cloud.google.com/freetrial) to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.\n\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudfunctions,cloudbuild.googleapis.com,artifactregistry.googleapis.com,run.googleapis.com,logging.googleapis.com,pubsub.googleapis.com&redirect=https://cloud.google.com/functions/docs/create-deploy-nodejs)\n\n- In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n-\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n-\n\n\n Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, Logging, and Pub/Sub APIs.\n\n\n [Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=cloudfunctions,cloudbuild.googleapis.com,artifactregistry.googleapis.com,run.googleapis.com,logging.googleapis.com,pubsub.googleapis.com&redirect=https://cloud.google.com/functions/docs/create-deploy-nodejs)\n\n1. Install [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git). Git is required for copying samples to your machine.\n2. Install the [Cloud Code plugin](/code/docs/vscode/install#installing) if you haven't already.\n\nCreate a function\n-----------------\n\nIf you've created a function before, you can [download an existing function to a\nnew workspace](#download_an_existing_function_to_a_new_workspace) and edit it\nlocally.\n\nTo create a new function using a sample, follow these steps:\n\n1. Click **Cloud Code** and then expand the\n **Cloud Functions** section.\n\n2. Click add\n **Create function** and select a template.\n\n3. Navigate to the pathway that you'd like to create your new function in, enter\n a name for the function, and select **Create New Application**.\n\n If the folder of your application doesn't appear automatically in the\n **Explorer** , click\n refresh\n **Refresh**.\n\n The `launch.json` file contains the required configurations for your\n function: \n\n {\n \"configurations\": [\n {\n \"name\": \"Deploy Cloud Function\",\n \"type\": \"cloudcode.cloudfunctions\",\n \"request\": \"launch\",\n \"functionName\": \"function-hello-world\",\n \"gen\": \"GEN_2\",\n \"entryPoint\": \"helloHttp\",\n }\n ]\n }\n\nTo learn more about creating multiple functions in a single workspace, see\n[Manage functions in VS Code workspace](/code/docs/vscode/manage-functions-workspace).\n\nDownload an existing function to a new workspace\n------------------------------------------------\n\nTo download a function to a new workspace, follow these steps:\n\n1. Click **Cloud Code** and then expand the\n **Cloud Functions** section.\n\n2. Expand your Google Cloud project snippet, right-click the function\n that you wish to download, and select **Download to new workspace**.\n\n3. When prompted, enter a name for the new application and click\n **Create New Application**.\n\n This downloads the function to your local machine and opens it in a new\n workspace in your IDE.\n\nDeploy a function\n-----------------\n\nTo deploy a function, follow these steps:\n\n1. Right-click a function and select **Deploy function**.\n\n2. In the Quickpick menu, select a Google Cloud project to deploy your\n function to.\n\n3. Select a region that the function will be deployed to.\n\n4. Select a runtime.\n\n The function's deployment may take a few minutes. While in progress,\n a progress_activity\n progress indicator appears. After it is complete, a\n check_circle checkmark\n appears. If it fails, an\n error error mark appears.\n\n If the deployment fails, refer to the **Output** tab for the error message.\n Clicking the link takes you to the build logs in Google Cloud console and\n provides more detail about the error.\n\n5. Optional: Once the function is deployed, you can perform the following\n actions:\n\n - To view read-only files of your deployed function, expand the function\n and then expand the **Source Code** folder. Here you can view\n `launch.json` under `.vscode`, the deployed code, and `package.json`.\n\n - To view the function in Google Cloud console, right-click the\n function and select **Open in Cloud Console** , and if prompted, select\n **Open**.\n\n - To edit the metadata of your function, right-click the function and\n select **Edit metadata in Cloud Console**.\n\n - To view logs of the deployed function, right-click the function and\n select **View logs**.\n\nInvoke a function\n-----------------\n\nInvoking a function allows you to execute the deployed function in\nGoogle Cloud console from the IDE's built-in terminal. To invoke a function,\nfollow these steps:\n\n1. Right-click the function and select **Invoke function via HTTP**.\n\n2. In the **Trigger function via HTTP** Quickpick that appears, select one of\n the two options:\n\n - **Default**: A terminal opens in the IDE and runs a command to execute\n the function in Google Cloud.\n\n - **Import arguments from JSON file** : This option takes the content of\n the selected JSON file, turns it into a string, and passes it with\n `-d '{`. This is useful for when you have an input to your functions and\n want to save it, allowing you to execute it without having to retype it repeatedly.\n\nWhat's next\n-----------\n\n- See the [Work with Cloud Functions](/code/docs/vscode/functions-overview) overview to learn more about [managing your workspace](/code/docs/vscode/manage-functions-workspace) or [setting up a local development environment](/code/docs/vscode/setup-local-functions-dev-environment).\n\nGet Support\n-----------\n\nTo send feedback, report issues on [GitHub](https://github.com/GoogleCloudPlatform/cloud-code-vscode/issues), or ask a question on [Stack Overflow](https://stackoverflow.com/questions/ask?tags=cloud-code-vscode)."]]