# Practica Cloud Functions

En la consola de comandos de GCP vamos a crear una carpeta con la funcion&#x20;

```bash
mkdir gcf_hello_world
```

```bash
cd gcf_hello_world
```

vamos a crear un archivo JS&#x20;

```bash
nano index.js
```

dentro vamos a meter el siguiente codigo&#x20;

```javascript
/**
* 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

```bash
gsutil mb -p [PROJECT_ID] gs://[BUCKET_NAME]
```

para el nombre de bucket puede leer esta documentacion&#x20;

{% embed url="<https://cloud.google.com/storage/docs/buckets#naming>" %}

Para el **\[PROJECT\_ID]** puedes usar el siguiente comando&#x20;

```bash
gcloud config list project
```

Ahora vamos a implementar el codigo&#x20;

```bash
gcloud functions deploy helloWorld --stage-bucket [BUCKET_NAME] --trigger-topic hello_world --runtime nodejs8
```

verificamos el estado&#x20;

```bash
gcloud functions describe helloWorld
```

La probamos&#x20;

```bash
DATA=$(printf 'Hello World!'|base64) && gcloud functions call helloWorld --data '{"data":"'$DATA'"}'
```

ver los registros&#x20;

```bash
gcloud functions logs read helloWorld
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gelvezz23.gitbook.io/my-blog/google-cloud/google-cloud-computing-fundations/practica-cloud-functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
