Se stai creando una nuova funzione, consulta la guida rapida della console su Cloud Run. I contenuti di questa pagina si applicano solo alle funzioni legacy esistenti create con l'API Cloud Functions v1.
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Esegui funzioni utilizzando l'emulatore di Functions
L'emulatore di Functions di Google Cloud CLI consente di gestire
le istanze locali delle funzioni Cloud Run tramite il comando gcloud alpha functions
local. In questo modo puoi
eseguire il deployment e testare le funzioni sul sistema locale prima di eseguirne il deployment nell'ambiente
Google Cloud .
Functions Emulator utilizza i buildpack di Google Cloud per pacchettizzare il codice della funzione in immagini container eseguibili localmente. Poi esegui queste immagini
localmente con Docker.
Prerequisiti per l'installazione
Prima di procedere, assicurati di aver installato quanto segue:
Il valore di --builder è impostato per impostazione predefinita sul builder di App Engine per la lingua della tua funzione. Ad esempio, per impostazione predefinita è
gcr.io/serverless-runtimes/google-22-full/builder/python per Python.
Quando utilizzi per la prima volta il comando gcloud alpha, il comando gcloud ti chiederà
di installare il
set di comandi gcloud alpha.
Chiama la tua funzione locale
Per chiamare la funzione locale senza dati, utilizza il comando seguente:
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME
Sostituisci LOCAL_DEPLOYMENT_NAME con il nome con cui vuoi eseguire il deployment locale della funzione.
Per includere i dati nella chiamata alla funzione locale, scegli la scheda che
corrisponde al tipo di funzione:
Funzione HTTP
Chiama la tua funzione HTTP locale nel seguente modo:
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME \
--data='{"message": "MESSAGE"}'
Sostituisci:
LOCAL_DEPLOYMENT_NAME: il nome con cui eseguire il deployment locale della funzione.
ENTRY_POINT: il punto di ingresso della funzione.
MESSAGE: una stringa di testo da passare come corpo della richiesta HTTP.
Funzione CloudEvent
Per chiamare la funzione CloudEvent locale, devi fornire un oggetto JSON CloudEvent che descriva l'evento trigger:
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME \
--cloud-event="CLOUD_EVENT_JSON"
Sostituisci:
LOCAL_DEPLOYMENT_NAME: il nome con cui eseguire il deployment locale della funzione.
ENTRY_POINT: il punto di ingresso della funzione.
CLOUD_EVENT_JSON: una stringa con codifica JSON in modalità contenuti strutturati che descrive l'evento di attivazione. Per ulteriori dettagli ed esempi, vedi
CloudEvents - JSON event format.
[[["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-10 UTC."],[[["\u003cp\u003eThe Functions Emulator allows you to manage, deploy, and test Cloud Run functions locally before deploying them to Google Cloud using the \u003ccode\u003egcloud alpha functions local\u003c/code\u003e command.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites for using the Functions Emulator include the Google Cloud CLI, Buildpacks pack tool, and Docker, as it containerizes function code using Google Cloud's buildpacks.\u003c/p\u003e\n"],["\u003cp\u003eYou can deploy functions locally with various runtimes, including Node.js, Python, Go, Java, C#, Ruby, and PHP, using the \u003ccode\u003egcloud alpha functions local deploy\u003c/code\u003e command, and optionally configure the deployment with flags like \u003ccode\u003e--port\u003c/code\u003e and \u003ccode\u003e--builder\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eLocal functions can be called using \u003ccode\u003egcloud alpha functions local call\u003c/code\u003e which sends an HTTP POST request, or with specified data for HTTP and CloudEvent functions.\u003c/p\u003e\n"],["\u003cp\u003eYou can delete local function deployments using the \u003ccode\u003egcloud alpha functions local delete\u003c/code\u003e command, which undeploys the function but doesn't remove the function code.\u003c/p\u003e\n"]]],[],null,["# Run functions using the Functions Emulator\n==========================================\n\n|\n| **Preview**\n|\n|\n| This product or feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA products and features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nThe Google Cloud CLI *Functions Emulator* enables you to you manage\nlocal instances of your Cloud Run functions through the [gcloud alpha functions\nlocal](/sdk/gcloud/reference/alpha/functions/local) command. This lets you\ndeploy and test your functions on your local system before deploying them to the\nGoogle Cloud environment.\n\nThe Functions Emulator uses [Google Cloud's buildpacks](https://cloud.google.com/docs/buildpacks/overview) to package your function\ncode into locally-runnable container images. You then run these images\nlocally with [Docker](https://docs.docker.com/get-docker/).\n| **Note:** The `gcloud alpha functions local` command is available as an alpha pre-release, and might be unstable or change without notice. If you prefer to run your functions locally without containerization, use the [Functions Framework](/functions/1stgendocs/running/function-frameworks).\n\nInstallation prerequisites\n--------------------------\n\nBefore proceeding, make sure you have the following installed:\n\n- [Google Cloud CLI](/sdk/docs/install)\n- [Buildpacks pack tool](https://buildpacks.io/docs/tools/pack/)\n- [Docker](https://www.docker.com/get-started/)\n\nDeploy your function locally\n----------------------------\n\nTo deploy your function locally with the Functions Emulator, use the\n`gcloud functions` command: \n\n### Node.js\n\n```bash\n gcloud alpha functions local deploy LOCAL_DEPLOYMENT_NAME \\\n --entry-point=ENTRY_POINT \\\n --no-gen2 \\\n --runtime=nodejs20\n```\n\n### Python\n\n```bash\n gcloud alpha functions local deploy LOCAL_DEPLOYMENT_NAME \\\n --entry-point=ENTRY_POINT \\\n --runtime=python312\n```\n\n### Go\n\n```bash\n gcloud alpha functions local deploy LOCAL_DEPLOYMENT_NAME \\\n --entry-point=ENTRY_POINT \\\n --no-gen2 \\\n --runtime=go121\n```\n\n### Java\n\n```bash\n gcloud alpha functions local deploy LOCAL_DEPLOYMENT_NAME \\\n --entry-point=ENTRY_POINT \\\n --no-gen2 \\\n --runtime=java17 \n```\n| **Note:** Java functions use `[PACKAGE_NAME].[CLASS_NAME]` as an entry-point value.\n\n### Ruby\n\n```bash\n gcloud alpha functions local deploy LOCAL_DEPLOYMENT_NAME \\\n --entry-point=ENTRY_POINT \\\n --runtime=ruby33\n```\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e: The name under which you are locally deploying your function.\n- \u003cvar translate=\"no\"\u003eENTRY_POINT\u003c/var\u003e: The entry point of your function.\n\nYou can further configure your deployment command with the following optional\nflags:\n\nThe `--builder` value defaults to the App Engine builder for your\nfunction's language. For example, it defaults to\n`gcr.io/serverless-runtimes/google-22-full/builder/python` for Python.\n\nWhen you first use the `gcloud alpha` command, the `gcloud` command will prompt\nyou to install the\n[gcloud alpha](https://cloud.google.com/sdk/gcloud#release_levels) command set.\n\nCall your local function\n------------------------\n\nTo call your local function without data, use the following command: \n\n gcloud alpha functions local call \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e\n\nReplace \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e with the name you want to locally\ndeploy your function under.\n| **Note:** `gcloud alpha functions local call` calls your function with an HTTP POST request. To use a different HTTP method, use a dedicated HTTP request tool such as `curl`. See [calling local functions](/functions/1stgendocs/running/calling) for instructions on how to interact with your locally-running function using `curl`.\n\nTo include data in the call to your local function, choose the tab that\nmatches your function type: \n\n### HTTP function\n\nCall your local HTTP function as follows: \n\n gcloud alpha functions local call \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e \\\n --data='{\"message\": \"\u003cvar translate=\"no\"\u003eMESSAGE\u003c/var\u003e\"}'\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e: The name to locally deploy your function under.\n- \u003cvar translate=\"no\"\u003eENTRY_POINT\u003c/var\u003e: The entry point of your function.\n- \u003cvar translate=\"no\"\u003eMESSAGE\u003c/var\u003e: A text string to pass in as the body of the HTTP request.\n\n### CloudEvent function\n\nTo call your local CloudEvent function, you must supply a `CloudEvent` JSON\nobject describing the trigger event: \n\n gcloud alpha functions local call \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e \\\n --cloud-event=\"\u003cvar translate=\"no\"\u003eCLOUD_EVENT_JSON\u003c/var\u003e\"\n\nReplace:\n\n- \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e: The name to locally deploy your function under.\n- \u003cvar translate=\"no\"\u003eENTRY_POINT\u003c/var\u003e: The entry point of your function.\n- \u003cvar translate=\"no\"\u003eCLOUD_EVENT_JSON\u003c/var\u003e: A JSON-encoded string in structured content mode describing the triggering event. For more details and examples, see [CloudEvents - JSON event format](/eventarc/docs/workflows/cloudevents).\n\nHere is an example command line: \n\n gcloud alpha functions local call my-function --cloud-event='{\n \"specversion\" : \"1.0\",\n \"type\" : \"com.github.pull.create\",\n \"source\" : \"https://github.com/cloudevents/spec/pull\",\n \"subject\" : \"123\",\n \"id\" : \"ce\",\n \"time\" : \"2021-01-27T18:30:00Z\", \"data\" : \"{\\n \\\"subscription\\\": \\\"projects\\/test-project\\/subscriptions\\/my-subscription\\\",\\n \\\"message\\\": {\\n \\\"attributes\\\": {\\n \\\"attr1\\\":\\\"attr1-value\\\"\\n },\\n \\\"data\\\": \\\"d29ybGQ=\\\",\\n \\\"messageId\\\": \\\"message-id\\\",\\n \\\"publishTime\\\":\\\"2021-02-05T04:06:14.109Z\\\",\\n \\\"orderingKey\\\": \\\"ordering-key\\\"\\n }\\n}\"\n }'\n\nSee [CloudEvent specs](https://github.com/cloudevents/spec/blob/main/cloudevents/formats/json-format.md) for more information about how the JSON format is defined for CloudEvents.\n\nDelete your local function deployment\n-------------------------------------\n\nDelete your local function deployment with the following command: \n\n gcloud alpha functions local delete \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e\n\nReplace \u003cvar translate=\"no\"\u003eLOCAL_DEPLOYMENT_NAME\u003c/var\u003e with the name to locally deploy your\nfunction under.\n\nThis command undeploys your function but doesn't delete the function code.\n\nNext steps\n----------\n\n- Learn how to [Deploy a Cloud Run function](/functions/1stgendocs/deploy) on Google Cloud."]]