Questo semplice tutorial illustra la scrittura, il deployment e l'attivazione di una funzione Cloud Functions HTTP.
Obiettivi
- Scrivi, sottoponi a deployment e attiva una funzione Cloud Functions HTTP.
Costi
In questo documento utilizzi i seguenti componenti fatturabili di Google Cloud:
- Cloud Functions
- Cloud Build
- Artifact Registry
Per maggiori dettagli, vedi i prezzi di Cloud Functions.
Per generare una stima dei costi in base all'utilizzo previsto,
utilizza il Calcolatore prezzi.
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.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions, Cloud Build, and Artifact Registry APIs.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Preparare l'ambiente di sviluppo.
Node.js
Python
Go
Java
C#
Ruby
PHP
Se hai già installato gcloud CLI, aggiornalo eseguendo questo comando:
gcloud components update
Preparazione dell'applicazione
Clona il repository dell'app di esempio sulla tua macchina locale:
Node.js
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
Go
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
C#
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
PHP
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
In alternativa, puoi scaricare l'esempio come file ZIP ed estrarlo.
Passa alla directory che contiene il codice di esempio di Cloud Functions:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Go
cd golang-samples/functions/functionsv2/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/
PHP
cd php-docs-samples/functions/helloworld_get/
Dai un'occhiata al codice campione:
Node.js
Python
Go
Java
C#
Ruby
PHP
Deployment della funzione
Per eseguire il deployment della funzione con un trigger HTTP, esegui questo comando nella directory contenente il codice campione (o, nel caso di Java, il file pom.xml
):
Node.js
gcloud functions deploy nodejs-http-function \ --gen2 \ --runtime=nodejs20 \ --region=REGION
\ --source=. \ --entry-point=helloGET \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione di Node.js supportata per eseguire la funzione.
Python
gcloud functions deploy python-http-function \ --gen2 \ --runtime=python312 \ --region=REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione Python supportata per eseguire la funzione.
Go
gcloud functions deploy go-http-function \ --gen2 \ --runtime=go121 \ --region=REGION
\ --source=. \ --entry-point=HelloGet \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione Go supportata per eseguire la funzione.
Java
gcloud functions deploy java-http-function \ --gen2 \ --runtime=java17 \ --region=REGION
\ --source=. \ --entry-point=functions.HelloWorld \ --memory=512MB \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione Java supportata per eseguire la funzione.
C#
gcloud functions deploy csharp-http-function \ --gen2 \ --runtime=dotnet6 \ --region=REGION
\ --source=. \ --entry-point=HelloWorld.Function \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione di .NET supportata per eseguire la funzione.
Ruby
gcloud functions deploy ruby-http-function \ --gen2 \ --runtime=ruby32 \ --region=REGION
\ --source=. \ --entry-point=hello_get \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione Ruby supportata per l'esecuzione della funzione.
PHP
gcloud functions deploy php-http-function \ --gen2 \ --runtime=php82 \ --region=REGION
\ --source=. \ --entry-point=helloGet \
--trigger-http
Utilizza il flag --runtime
per specificare l'ID runtime di una versione PHP supportata per eseguire la funzione.
Facoltativamente, puoi utilizzare il flag --allow-unauthenticated
per raggiungere la funzione senza autenticazione.
Questa opzione è utile per i test, ma non è consigliabile utilizzare questa impostazione in produzione, a meno che tu non stia creando un'API o un sito web pubblici. Inoltre, potrebbe non funzionare per te, a seconda delle impostazioni dei criteri aziendali. Per maggiori dettagli su come richiamare una funzione che richiede l'autenticazione, consulta Autenticazione per le chiamate.
Attivazione della funzione
Identifica l'URL della funzione di cui hai eseguito il deployment.
Accedi all'URL dal browser. Dovresti vedere un messaggio
Hello, World!
.
Esegui la pulizia
Per evitare che al tuo Account Google Cloud vengano addebitati costi relativi alle risorse utilizzate in questo tutorial, elimina il progetto che contiene le risorse oppure mantieni il progetto ed elimina le singole risorse.
Elimina il progetto
Il modo più semplice per eliminare la fatturazione è eliminare il progetto che hai creato per il tutorial.
Per eliminare il progetto:
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
Eliminazione della Cloud Function
L'eliminazione di Cloud Functions non rimuove le risorse archiviate in Cloud Storage.
Per eliminare la Cloud Function creata in questo tutorial, esegui questo comando:
Node.js
gcloud functions delete nodejs-http-function --gen2 --region REGION
Python
gcloud functions delete python-http-function --gen2 --region REGION
Go
gcloud functions delete go-http-function --gen2 --region REGION
Java
gcloud functions delete java-http-function --gen2 --region REGION
C#
gcloud functions delete csharp-http-function --gen2 --region REGION
Ruby
gcloud functions delete ruby-http-function --gen2 --region REGION
PHP
gcloud functions delete php-http-function --gen2 --region REGION
Puoi anche eliminare Cloud Functions dalla console Google Cloud.