Crea e implementa una función con Cloud Code para VS Code
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta página, se muestra cómo crear, descargar, implementar e invocar una función HTTP de 2ª gen. con Cloud Functions y Cloud Code.
Antes de comenzar
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.
Descargar una función existente en un lugar de trabajo nuevo
Para descargar una función en un lugar de trabajo nuevo, sigue estos pasos:
Haz clic en Cloud Code y, luego, expande la sección Cloud Functions.
Expande el fragmento de tu Google Cloud proyecto, haz clic con el botón derecho en la función
que deseas descargar y selecciona Descargar a un espacio de trabajo nuevo.
Cuando se te solicite, ingresa un nombre para la aplicación nueva y haz clic en Create New Application.
Esta acción descarga la función a tu máquina local y la abre en un lugar de trabajo nuevo en tu IDE.
Implementa una función
Sigue estos pasos para implementar una función:
Haz clic con el botón derecho en una función y selecciona Implementar función.
En el menú de Selección rápida, selecciona un Google Cloud proyecto en el que desees implementar
tu función.
Selecciona la región en la que se implementará la función.
Selecciona un entorno de ejecución.
La implementación de la función puede tardar unos minutos. Mientras se realiza la acción, aparece un indicador de progreso de progress_activity. Cuando se complete, aparecerá una marca de verificación check_circle. Si falla, aparecerá una marca de error error.
Si la implementación falla, consulta la pestaña Output para ver el mensaje de error.
Cuando haces clic en el vínculo, se te dirige a los registros de compilación en la consola de Google Cloud y se proporcionan más detalles sobre el error.
Una vez que se implementa la función, puedes realizar las siguientes acciones (opcional):
Para ver los archivos de solo lectura de tu función implementada, expande la función y, luego, expande la carpeta Código fuente. Aquí puedes ver launch.json en .vscode, el código implementado y package.json.
Para ver la función en la Google Cloud consola, haz clic con el botón derecho en la función y selecciona Abrir en Cloud Console y, si se te solicita, selecciona Abrir.
Para editar los metadatos de la función, haz clic con el botón derecho en la función y selecciona Editar metadatos en la consola de Cloud.
Para ver los registros de la función implementada, haz clic con el botón derecho en la función y selecciona Ver registros.
Invoca una función
Invocar una función te permite ejecutar la función implementada en la consola deGoogle Cloud desde la terminal integrada del IDE. Para invocar una función, sigue estos pasos:
Haz clic con el botón derecho en la función y selecciona Invoke function via HTTP.
En el menú de selección rápida Activar función a través de HTTP que aparece, selecciona una de las dos opciones:
Predeterminado: Se abre una terminal en el IDE y se ejecuta un comando para ejecutar la función en Google Cloud.
Importar argumentos desde un archivo JSON: Esta opción toma el contenido del archivo JSON seleccionado, lo convierte en una cadena y lo pasa con -d '{. Esto es útil cuando tienes una entrada para tus funciones y quieres guardarla, lo que te permite ejecutarla sin tener que volver a escribirla varias veces.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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)."]]