Questo semplice tutorial mostra come scrivere, eseguire il deployment e attivare una funzione Cloud Run basata su eventi con un trigger Pub/Sub.
Se non hai mai utilizzato Pub/Sub e vuoi saperne di più, consulta la documentazione, in particolare la gestione di argomenti e sottoscrizioni. Consulta la sezione Trigger Pub/Sub per una panoramica dell'utilizzo di argomenti e sottoscrizioni Pub/Sub nelle funzioni Cloud Run.
Se stai cercando esempi di codice per utilizzare Pub/Sub, visita il browser degli esempi.
Obiettivi
- Scrivere ed eseguire il deployment di una funzione Cloud Run basata sugli eventi.
- Attiva la funzione pubblicando un messaggio in un argomento Pub/Sub.
Costi
In questo documento vengono utilizzati i seguenti componenti fatturabili di Google Cloud:
- Cloud Run functions
- Pub/Sub
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.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Pub/Sub APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Se utilizzi un provider di identità (IdP) esterno, devi prima accedere a gcloud CLI con la tua identità federata.
-
Per inizializzare gcloud CLI, esegui questo comando:
gcloud init
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Pub/Sub APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Install the Google Cloud CLI.
-
Se utilizzi un provider di identità (IdP) esterno, devi prima accedere a gcloud CLI con la tua identità federata.
-
Per inizializzare gcloud CLI, esegui questo comando:
gcloud init
- Prepara l'ambiente di sviluppo.
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 il campione come file ZIP ed estrarlo.
Python
git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
In alternativa, puoi scaricare il campione come file ZIP ed estrarlo.
Vai
git clone https://github.com/GoogleCloudPlatform/golang-samples.git
In alternativa, puoi scaricare il campione come file ZIP ed estrarlo.
Java
git clone https://github.com/GoogleCloudPlatform/java-docs-samples.git
In alternativa, puoi scaricare il campione come file ZIP ed estrarlo.
Ruby
git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples.git
In alternativa, puoi scaricare il campione come file ZIP ed estrarlo.
Passa alla directory che contiene il codice campione di Cloud Run Functions per accedere a Pub/Sub:
Node.js
cd nodejs-docs-samples/functions/helloworld/
Python
cd python-docs-samples/functions/helloworld/
Vai
cd golang-samples/functions/helloworld/
Java
cd java-docs-samples/functions/helloworld/hello-pubsub/
Ruby
cd ruby-docs-samples/functions/helloworld/pubsub/
Dai un'occhiata al codice campione:
Node.js
Python
Go
Java
Ruby
Pubblica un messaggio nell'argomento Pub/Sub. In questo esempio, il messaggio è un nome che la funzione includerà in un saluto:
gcloud pubsub topics publish
YOUR_TOPIC_NAME
--messageYOUR_NAME
Sostituisci
YOUR_TOPIC_NAME
con il nome dell'argomento Pub/Sub eYOUR_NAME
con una stringa arbitraria.Controlla i log per assicurarti che le esecuzioni siano state completate:
gcloud functions logs read --limit 50
- 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.
Se hai già installato gcloud CLI, aggiornala eseguendo il seguente comando:
gcloud components update
Preparazione della richiesta
Deployment della funzione
Per il deployment della funzione con un trigger Pub/Sub, esegui questo comando
nella directory che contiene il codice campione (o, nel caso di Java, il
file pom.xml
):
Node.js
gcloud functions deploy helloPubSub \ --runtime nodejs20 \
--trigger-topic YOUR_TOPIC_NAME
Utilizza il flag --runtime
per specificare l'ID runtime di una versione di Node.js supportata per eseguire la funzione.
Python
gcloud functions deploy hello_pubsub \ --runtime python312 \
--trigger-topic YOUR_TOPIC_NAME
Utilizza il flag --runtime
per specificare l'ID runtime di una
versione di Python supportata per eseguire
la funzione.
Vai
gcloud functions deploy HelloPubSub \ --runtime go121 \
--trigger-topic YOUR_TOPIC_NAME
Utilizza il flag --runtime
per specificare l'ID runtime di una versione di Go supportata per eseguire la funzione.
Java
gcloud functions deploy java-pubsub-function \ --entry-point functions.HelloPubSub \ --runtime java17 \ --memory 512MB \
--trigger-topic YOUR_TOPIC_NAME
Utilizza il flag --runtime
per specificare l'ID runtime di una
versione Java supportata per eseguire
la funzione.
Ruby
gcloud functions deploy hello_pubsub --runtime ruby33 \
--trigger-topic YOUR_TOPIC_NAME
Utilizza il flag --runtime
per specificare l'ID runtime di una
versione di Ruby supportata per eseguire
la funzione.
dove YOUR_TOPIC_NAME
è il nome dell'argomento Pub/Sub a cui verrà sottoscritta la funzione.
Se YOUR_TOPIC_NAME
non esiste già, questo comando
lo crea per te. Puoi anche creare un argomento prima di eseguire il comando deploy
utilizzando la consoleGoogle Cloud o il seguente comando gcloud
:
gcloud pubsub topics create YOUR_TOPIC_NAME
Attivazione della funzione
Puoi anche pubblicare un messaggio in un argomento Pub/Sub dall'interno di una funzione.
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 creato per il tutorial.
Per eliminare il progetto:
Eliminazione della funzione
L'eliminazione delle funzioni Cloud Run non rimuove le risorse archiviate in Cloud Storage.
Per eliminare la funzione creata in questo tutorial, esegui questo comando:
Node.js
gcloud functions delete helloPubSub
Python
gcloud functions delete hello_pubsub
Vai
gcloud functions delete HelloPubSub
Java
gcloud functions delete java-pubsub-function
Ruby
gcloud functions delete hello_pubsub
Puoi anche eliminare le funzioni Cloud Run dalla consoleGoogle Cloud .