⚙️Practica Cloud Functions
En la consola de comandos de GCP vamos a crear una carpeta con la funcion
mkdir gcf_hello_world
cd gcf_hello_world
vamos a crear un archivo JS
nano index.js
dentro vamos a meter el siguiente codigo
/**
* Background Cloud Function to be triggered by Pub/Sub.
* This function is exported by index.js, and executed when
* the trigger topic receives a message.
*
* @param {object} data The event payload.
* @param {object} context The event metadata.
*/
exports.helloWorld = (data, context) => {
const pubSubMessage = data;
const name = pubSubMessage.data
? Buffer.from(pubSubMessage.data, 'base64').toString() : "Hello World";
console.log(`My Cloud Function: ${name}`);
};
Sal de nano (Ctrl + x) y guarda (Y) el archivo.
Creamos el Bucket en Cloud Storage
gsutil mb -p [PROJECT_ID] gs://[BUCKET_NAME]
para el nombre de bucket puede leer esta documentacion
Para el [PROJECT_ID] puedes usar el siguiente comando
gcloud config list project
Ahora vamos a implementar el codigo
gcloud functions deploy helloWorld --stage-bucket [BUCKET_NAME] --trigger-topic hello_world --runtime nodejs8
verificamos el estado
gcloud functions describe helloWorld
La probamos
DATA=$(printf 'Hello World!'|base64) && gcloud functions call helloWorld --data '{"data":"'$DATA'"}'
ver los registros
gcloud functions logs read helloWorld
Última actualización
¿Te fue útil?