Crea ed esegui il deployment di una funzione Cloud Run HTTP utilizzando Ruby (1ª gen.)
Questa guida illustra la procedura per scrivere una funzione Cloud Run utilizzando il runtime Ruby. Esistono due tipi di funzioni Cloud Run:
- Una funzione HTTP, che richiami da richieste HTTP standard.
- Una funzione basata su eventi, che utilizzi per gestire gli eventi della tua infrastruttura cloud, ad esempio i messaggi in un argomento Pub/Sub o le modifiche in un bucket Cloud Storage.
L'esempio mostra come creare una semplice funzione HTTP.
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. -
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 role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Functions and Cloud Build APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles. - Installa e inizializza gcloud CLI.
- Aggiorna e installa i componenti di
gcloud:gcloud components update
- Prepara l'ambiente di sviluppo.
Crea una directory sul sistema locale per il codice della funzione:
Linux o Mac OS X
mkdir ~/helloworld cd ~/helloworldWindows
mkdir %HOMEPATH%\helloworld cd %HOMEPATH%\helloworldCrea un file
app.rbnella directoryhelloworldcon il seguente contenuto:Questa funzione di esempio prende un nome fornito nella richiesta HTTP e restituisce un saluto oppure "Hello World!" se non viene fornito alcun nome.
Al termine del deployment della funzione, annota la proprietà
httpsTrigger.urlo cercala utilizzando il seguente comando:gcloud functions describe hello_httpDovrebbe avere il seguente aspetto:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http
Accedi all'URL dal browser. Dovresti vedere il messaggio "Hello World!".
Prova a inserire un nome nella richiesta HTTP, ad esempio utilizzando il seguente URL:
https://GCP_REGION-PROJECT_ID.cloudfunctions.net/hello_http?name=NAME
Dovresti vedere il messaggio "Hello
NAME!".
Crea una funzione
Specifica le dipendenze
Le dipendenze in Ruby vengono gestite con bundler ed espresse in un file denominato Gemfile.
Quando esegui il deployment della funzione, Cloud Run Functions scarica e installa le dipendenze dichiarate in Gemfile e Gemfile.lock utilizzando bundler.
Il file Gemfile elenca i pacchetti richiesti dalla funzione, insieme a eventuali vincoli di versione facoltativi. Per una funzione Cloud Run, uno di questi pacchetti deve essere il gem functions_framework.
Per questo esercizio, crea un file denominato Gemfile nella stessa directory del file app.rb che contiene il codice della funzione, con il contenuto seguente:
source "https://rubygems.org" gem "functions_framework", "~> 0.7"
Esegui questo comando per installare il gem functions_framework e altre dipendenze:
bundle install
Crea build ed esegui test in locale
Prima di eseguire il deployment della funzione, puoi compilarla e testarla localmente.
Esegui questo comando per utilizzare l'eseguibile functions-framework-ruby per avviare un server web locale che esegue la funzione hello_http:
bundle exec functions-framework-ruby --target hello_http
# ...starts the web server in the foreground
Se la funzione viene compilata correttamente, viene visualizzato l'URL che puoi visitare nel browser web per vedere la funzione in azione: http://localhost:8080/. Dovresti visualizzare un messaggio Hello World!.
In alternativa, puoi inviare richieste a questa funzione utilizzando curl da un'altra finestra del terminale:
curl localhost:8080
# Output: Hello World!
Consulta la sezione sul test delle funzioni nella documentazione del framework di Functions per Ruby.
Esegui il deployment della funzione
Per il deployment della funzione con un trigger HTTP, esegui questo comando nella directory helloworld:
gcloud functions deploy hello_http --no-gen2 --runtime ruby34 --trigger-http --allow-unauthenticated
Il flag --allow-unauthenticated consente di raggiungere la funzione senza autenticazione.
Per richiedere l'autenticazione, ometti il flag.
Esegui il test della funzione di cui hai eseguito il deployment
Visualizza i log
Puoi visualizzare i log di Cloud Run Functions nell'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, seguito dal nome della funzione:
gcloud functions logs read hello_http
L'output dovrebbe essere simile al seguente:
LEVEL NAME EXECUTION_ID TIME_UTC LOG D helloHttp rvb9j0axfclb 2019-09-18 22:06:25.983 Function execution started D helloHttp rvb9j0axfclb 2019-09-18 22:06:26.001 Function execution took 19 ms, finished with status code: 200
Visualizza i log nella dashboard Logging
Puoi visualizzare i log per Cloud Run Functions anche dalla consoleGoogle Cloud .