Cette page explique comment créer et déployer une fonction Cloud à l'aide de l'outil de ligne de commande gcloud
.
Avant de commencer
- Connectez-vous à votre compte Google.
Si vous n'en possédez pas déjà un, vous devez en créer un.
-
Dans Google Cloud Console, sur la page de sélection du projet, sélectionnez ou créez un projet Google Cloud.
-
Assurez-vous que la facturation est activée pour votre projet Cloud. Découvrez comment vérifier que la facturation est activée pour votre projet.
- Activer les API Cloud Functions and Cloud Build.
- Installez et initialisez le SDK Cloud.
- Mettez à jour les composants
gcloud
:gcloud components update
- Préparez votre environnement de développement.
Vous avez besoin d'une invite de commande ? Vous pouvez utiliser Google Cloud Shell. Google Cloud Shell est un environnement de ligne de commande qui inclut le SDK Google Cloud. Vous n'avez donc pas besoin de l'installer. Le SDK Google Cloud est également préinstallé sur les machines virtuelles Google Compute Engine.
Obtenir l'exemple de code
Clonez l'exemple de dépôt sur votre ordinateur local :
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
Vous pouvez également télécharger l'exemple en tant que fichier ZIP et l'extraire.
Accédez au répertoire qui contient l'exemple de code de Cloud Functions :
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/helloworld/
C#
cd dotnet-docs-samples/functions/helloworld/HelloWorld/
Ruby
cd ruby-docs-samples/functions/helloworld/get/
Consultez l'exemple de code :
Node.js
Python
Go
Java
C#
Ruby
Déployer une fonction
Pour déployer la fonction avec un déclencheur HTTP, exécutez la commande suivante dans le répertoire contenant votre fonction :
Node.js
gcloud functions deploy helloGET \ --runtime nodejs10 --trigger-http --allow-unauthenticatedVous pouvez attribuer les valeurs suivantes à l'option
--runtime
, afin de spécifier votre version préférée de Node.js : nodejs10
nodejs12
Python
gcloud functions deploy hello_get \ --runtime python37 --trigger-http --allow-unauthenticatedVous pouvez attribuer les valeurs suivantes à l'option
--runtime
, afin de spécifier votre version préférée de Python :python37
python38
Go
gcloud functions deploy HelloGet \ --runtime go111 --trigger-http --allow-unauthenticatedVous pouvez attribuer les valeurs suivantes à l'option
--runtime
, afin de spécifier votre version préférée de Go : go111
go113
Java
gcloud functions deploy java-helloworld \ --entry-point functions.HelloWorld \ --runtime java11 \ --memory 512MB --trigger-http --allow-unauthenticated
C#
gcloud functions deploy csharp-helloworld \ --entry-point HelloWorld.Function \ --runtime dotnet3 --trigger-http --allow-unauthenticated
Ruby
gcloud functions deploy hello_get --runtime ruby26 --trigger-http --allow-unauthenticatedVous pouvez attribuer les valeurs suivantes à l'option
--runtime
, afin de spécifier votre version préférée de Ruby : ruby26
ruby27
L'option --allow-unauthenticated
vous permet d'accéder à la fonction sans authentification.
Pour exiger une authentification, omettez cette option.
Tester la fonction
Une fois le déploiement de la fonction terminé, notez la propriété
url
dehttpsTrigger
ou recherchez-la à l'aide de la commande suivante :Node.js
gcloud functions describe helloGET
Python
gcloud functions describe hello_get
Go
gcloud functions describe HelloGet
Java
gcloud functions describe java-helloworld
C#
gcloud functions describe csharp-helloworld
Ruby
Elle doit se présenter comme ceci :
Node.js
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGET
Python
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_get
Go
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloGet
Java
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/java-helloworld
C#
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/csharp-helloworld
Ruby
gcloud functions describe hello_get
Accédez à cette URL dans votre navigateur. Le message suivant devrait s'afficher :
Hello World!
.
Supprimer la fonction
Pour supprimer la fonction, exécutez la commande suivante :
Pour supprimer la fonction, exécutez la commande suivante :
Node.js
gcloud functions delete helloGET --trigger-http --allow-unauthenticated
Python
gcloud functions delete hello_get --trigger-http --allow-unauthenticated
Go
gcloud functions delete HelloGet --trigger-http --allow-unauthenticated
Java
gcloud functions delete java-helloworld --trigger-http --allow-unauthenticated
C#
gcloud functions delete csharp-helloworld --trigger-http --allow-unauthenticated
Ruby
gcloud functions delete hello_get --trigger-http --allow-unauthenticated
Node.js
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/helloGET
Python
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_get
Go
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/HelloGet
Java
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/java-helloworld
C#
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/csharp-helloworld
Ruby
gcloud functions describe hello_get
Étape suivante
- En savoir plus sur l'écriture des fonctions Cloud Functions
- En savoir plus sur le déploiement des fonctions Cloud Functions
- En savoir plus sur l'appel des fonctions Cloud Functions
- Apprenez à surveiller des fonctions Cloud Functions.
- Apprenez à exécuter des fonctions Cloud Functions localement.
- Explorez les tutoriels sur les fonctions Cloud Functions plus avancées.