Crea ed esegui il deployment di una funzione Cloud Run HTTP con Ruby
Questa guida illustra il processo di scrittura di una funzione Cloud Run utilizzando il runtime Ruby. Esistono due tipi di funzioni Cloud Run:
- Una funzione HTTP, che richiami dalle richieste HTTP standard.
- Una funzione basata su eventi, che utilizzi per gestire gli eventi dal tuo Cloud dell'infrastruttura, ad esempio i messaggi su un argomento Pub/Sub o le modifiche nel bucket Cloud Storage.
Per maggiori dettagli, consulta la sezione sulla scrittura di funzioni HTTP e sulla scrittura di funzioni basate sugli eventi.
Prima di iniziare
- Accedi al tuo account Google Cloud. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti gratuiti per l'esecuzione, il test e il deployment dei carichi di lavoro.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Assicurati che la fatturazione sia attivata per il tuo progetto Google Cloud.
-
Enable the Cloud Functions, Cloud Build, Artifact Registry, Cloud Run, and Cloud Logging APIs.
- Installa e inizializza Google Cloud SDK.
- Aggiorna e installa i componenti
gcloud
con il seguente comando.gcloud components update
- Prepara l'ambiente di sviluppo.
Crea la funzione
Crea una directory sul sistema locale per il codice della funzione:
Linux o Mac OS X
mkdir ~/helloworld cd ~/helloworld
Windows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworld
Crea un file
app.rb
nella directoryhelloworld
con seguenti contenuti:Questa funzione di esempio prende un nome fornito nella richiesta HTTP e restituisce un saluto oppure "Hello World!" se non viene fornito alcun nome.
Specifica delle dipendenze
Le dipendenze in Ruby sono gestite con bundler e expressed in un file denominato Gemfile
.
Quando esegui il deployment della funzione, Cloud Run Functions scarica e installa
delle dipendenze dichiarate in Gemfile
e Gemfile.lock
utilizzando bundler
.
Gemfile
elenca i pacchetti richiesti dalla tua funzione, insieme a eventuali
vincoli di versione facoltativi. Per una funzione Cloud Run, uno di questi
i pacchetti devono essere la gemma functions_framework
.
Per questo esercizio, crea un file denominato Gemfile
nella stessa directory del
File app.rb
contenente il codice della funzione, con i seguenti contenuti:
source "https://rubygems.org"
gem "functions_framework", "~> 0.7"
Esegui il seguente comando per installare il gem functions_framework
e altre dipendenze:
bundle install
Crea e testa la tua funzione in locale
Prima di eseguire il deployment della funzione, puoi crearla e testarla in locale.
Esegui questo comando per utilizzare l'eseguibile
functions-framework-ruby
per avvia un server web locale che esegue la funzionehello_http
:bundle exec functions-framework-ruby --target hello_http
Testa la funzione visitando
http://localhost:8080
in un browser o eseguendocurl localhost:8080
da un'altra finestra.Consulta Invio di richieste a funzioni locali per in modo più dettagliato.
Consulta Funzioni di test nella documentazione del framework di funzioni Ruby.
esegui il deployment della funzione
Per eseguire il deployment della funzione, esegui il seguente comando nella directory helloworld
:
gcloud functions deploy ruby-http-function \
--gen2 \
--runtime=ruby32 \
--region=REGION \
--entry-point=hello_http \
--source=. \
--trigger-http \
--allow-unauthenticated
Sostituisci REGION con il nome della regione Google Cloud in cui vuoi eseguire il deployment della funzione
(ad es. us-west1
).
Il flag facoltativo --allow-unauthenticated
consente di raggiungere la funzione
senza autenticazione.
Testa la funzione di cui hai eseguito il deployment
Dopo il deployment della funzione, prendi nota della proprietà
uri
dall'output del comandogcloud functions deploy
o recuperala con il seguente comando:gcloud functions describe ruby-http-function \ --region=REGION
Sostituisci REGION con il nome della regione Google Cloud in cui hai eseguito il deployment della funzione (ad es.
us-west1
).Accedi all'URL dal browser. La funzione restituisce il messaggio "Hello World!".
Visualizza i log della funzione
Puoi esaminare i log della funzione con l'interfaccia utente di Cloud Logging o tramite Google Cloud CLI.
Visualizza i log con lo strumento a riga di comando
Per visualizzare i log per la tua funzione con gcloud CLI, utilizza il comando
logs read
:
gcloud functions logs read \
--gen2 \
--limit=10 \
--region=REGION \
ruby-http-function
Sostituisci REGION con il nome della regione Google Cloud in cui hai eseguito il deployment della funzione
(ad es. us-west1
).
L'output è simile al seguente:
LEVEL: I
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.477
LOG: Default STARTUP TCP probe succeeded after 1 attempt for container "hello__http-1" on port 8080.
LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.451
LOG: I, [2023-06-01T00:09:41.451784 #1] INFO -- : FunctionsFramework: Serving function "hello_http" on port 8080...
LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.364
LOG: I, [2023-06-01T00:09:41.363923 #1] INFO -- : FunctionsFramework: Starting server...
LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.363
LOG: I, [2023-06-01T00:09:41.363855 #1] INFO -- : FunctionsFramework: Looking for function name "hello_http"...
LEVEL:
NAME: hello-http
TIME_UTC: 2023-06-01 00:09:41.354
LOG: I, [2023-06-01T00:09:41.354150 #1] INFO -- : FunctionsFramework: Loading functions from "./app.rb"...
Visualizzare i log con la dashboard di logging
Per visualizzare i log della tua funzione con la dashboard di logging, apri la pagina Panoramica delle funzioni Cloud Run e fai clic sul nome della funzione nell'elenco, quindi sulla scheda Log.