Si vous créez une fonction, consultez le guide de démarrage rapide de la console sur Cloud Run. Le contenu de cette page ne s'applique qu'aux anciennes fonctions existantes créées avec l'API Cloud Functions v1.
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Exécuter des fonctions à l'aide de l'émulateur Functions
L'émulateur Functions de Google Cloud CLI vous permet de gérer les instances locales de vos fonctions Cloud Run via la commande gcloud alpha functions local. Cela vous permet de déployer et de tester vos fonctions sur votre système local avant de les déployer dans l'environnementGoogle Cloud .
L'émulateur Functions utilise les packs de création Google Cloud pour empaqueter le code de votre fonction dans des images de conteneurs exécutables localement. Vous exécutez ensuite ces images localement avec Docker.
Conditions requises pour l'installation
Avant de continuer, assurez-vous que les éléments suivants sont installés:
La valeur --builder est définie par défaut sur le compilateur App Engine pour le langage de votre fonction. Par exemple, il est défini par défaut sur gcr.io/serverless-runtimes/google-22-full/builder/python pour Python.
Lorsque vous utilisez la commande gcloud alpha pour la première fois, la commande gcloud vous invite à installer l'ensemble de commandes gcloud alpha.
Appeler votre fonction locale
Pour appeler votre fonction locale sans données, utilisez la commande suivante:
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME
Remplacez LOCAL_DEPLOYMENT_NAME par le nom que vous souhaitez utiliser pour déployer votre fonction localement.
Pour inclure des données dans l'appel de votre fonction locale, choisissez l'onglet correspondant à votre type de fonction:
Fonction HTTP
Appelez votre fonction HTTP locale comme suit :
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME \
--data='{"message": "MESSAGE"}'
Remplacez :
LOCAL_DEPLOYMENT_NAME : nom dans lequel déployer votre fonction localement.
ENTRY_POINT : point d'entrée de votre fonction.
MESSAGE : chaîne de texte à transmettre en tant que corps de la requête HTTP
Fonction CloudEvent
Pour appeler votre fonction CloudEvent locale, vous devez fournir un objet JSON CloudEvent décrivant l'événement déclencheur :
gcloud alpha functions local call LOCAL_DEPLOYMENT_NAME \
--cloud-event="CLOUD_EVENT_JSON"
Remplacez :
LOCAL_DEPLOYMENT_NAME : nom dans lequel déployer votre fonction localement.
ENTRY_POINT : point d'entrée de votre fonction.
CLOUD_EVENT_JSON : chaîne encodée au format JSON en mode contenu structuré décrivant l'événement déclencheur. Pour obtenir plus d'informations et d'exemples, consultez la section CloudEvents : format d'événement JSON.
Sauf indication contraire, le contenu de cette page est régi par une licence Creative Commons Attribution 4.0, et les échantillons de code sont régis par une licence Apache 2.0. Pour en savoir plus, consultez les Règles du site Google Developers. Java est une marque déposée d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/09/10 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Difficile à comprendre","hardToUnderstand","thumb-down"],["Informations ou exemple de code incorrects","incorrectInformationOrSampleCode","thumb-down"],["Il n'y a pas l'information/les exemples dont j'ai besoin","missingTheInformationSamplesINeed","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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."]]