Primeros Pasos con Lambda

Esta guía muestra cómo crear una lambda JavaScript, listarla e invocarla mediante rutas administrativas y públicas.

Requisitos Previos

  • Una cuenta de HolaCloud con un token X-Glue-Authentication.
  • curl instalado en tu máquina.

Paso 1: Crear una Lambda

Crea una lambda simple hello-world:

curl -X POST "https://api.hola.cloud/api/v0/lambdas" \
  -H "X-Glue-Authentication: TU_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "hello-world",
    "language": "javascript",
    "method": "GET",
    "path": "/hello-world",
    "code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })"
  }'
POST /api/v0/lambdas HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_TOKEN
Content-Type: application/json

{
    "name": "hello-world",
    "language": "javascript",
    "method": "GET",
    "path": "/hello-world",
    "code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })"
  }
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/api/v0/lambdas", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_TOKEN")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['code' => 'export default (req) => ({ body: { message: "Hello, World!", method: req.method, path: req.path } })', 'language' => 'javascript', 'method' => 'GET', 'name' => 'hello-world', 'path' => '/hello-world'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/api/v0/lambdas',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_TOKEN',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "X-Glue-Authentication": "TU_TOKEN",
    "Content-Type": "application/json",
}

payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/api/v0/lambdas",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"};

const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "POST",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language": "javascript", "method": "GET", "name": "hello-world", "path": "/hello-world"};

const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "POST",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("code", "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })", "language", "javascript", "method", "GET", "name", "hello-world", "path", "/hello-world");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/api/v0/lambdas"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("X-Glue-Authentication", "TU_TOKEN")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Respuesta esperada:

 1{
 2  "id": "f3b2c1a0-1234-5678-9abc-def012345678",
 3  "created_timestamp": 1750507200,
 4  "owner": "user_123",
 5  "project_id": "project_456",
 6  "name": "hello-world",
 7  "language": "javascript",
 8  "code": "export default (req) => ({ body: { message: \"Hello, World!\", method: req.method, path: req.path } })",
 9  "method": "GET",
10  "path": "/hello-world"
11}

Guarda el id devuelto; es el lambda_id usado por los endpoints de ejecución, consulta, actualización y eliminación.

Paso 2: Listar Lambdas

Verifica que la lambda fue creada:

curl "https://api.hola.cloud/api/v0/lambdas" \
  -H "X-Glue-Authentication: TU_TOKEN"
GET /api/v0/lambdas HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_TOKEN
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/api/v0/lambdas", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_TOKEN")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/api/v0/lambdas',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_TOKEN',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

headers = {
    "X-Glue-Authentication": "TU_TOKEN",
}

response = requests.request(
    "GET",
    "https://api.hola.cloud/api/v0/lambdas",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/lambdas", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN"
  }
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/api/v0/lambdas"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .header("X-Glue-Authentication", "TU_TOKEN")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Paso 3: Ejecutar la Lambda

Invócala mediante la ruta administrativa autenticada:

curl -X GET "https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID" \
  -H "X-Glue-Authentication: TU_TOKEN"
GET /api/v0/run/TU_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
X-Glue-Authentication: TU_TOKEN
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID", nil)
	if err != nil {
		panic(err)
	}
	req.Header.Set("X-Glue-Authentication", "TU_TOKEN")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
    CURLOPT_HTTPHEADER => [
        'X-Glue-Authentication: TU_TOKEN',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

headers = {
    "X-Glue-Authentication": "TU_TOKEN",
}

response = requests.request(
    "GET",
    "https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID",
    headers=headers
)

print(response.text)
const response = await fetch("https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN"
  }
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID", {
  method: "GET",
  headers: {
    "X-Glue-Authentication": "TU_TOKEN"
  }
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/api/v0/run/TU_LAMBDA_ID"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .header("X-Glue-Authentication", "TU_TOKEN")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Respuesta esperada:

1{
2  "body": {
3    "message": "Hello, World!",
4    "method": "GET",
5    "path": "/hello-world"
6  }
7}

También puedes invocar la lambda mediante su ruta pública:

curl -X GET "https://api.hola.cloud/run/TU_LAMBDA_ID"
GET /run/TU_LAMBDA_ID HTTP/1.1
Host: api.hola.cloud
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/run/TU_LAMBDA_ID", nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/run/TU_LAMBDA_ID',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

response = requests.request(
    "GET",
    "https://api.hola.cloud/run/TU_LAMBDA_ID"
)

print(response.text)
const response = await fetch("https://api.hola.cloud/run/TU_LAMBDA_ID", {
  method: "GET"
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/run/TU_LAMBDA_ID", {
  method: "GET"
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/run/TU_LAMBDA_ID"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Paso 4: Revisar Invocaciones en Curso

Lista las invocaciones que se están ejecutando:

curl "https://api.hola.cloud/ongoing"
GET /ongoing HTTP/1.1
Host: api.hola.cloud
package main

import (
	"fmt"
	"io"
	"net/http"
)

func main() {
	req, err := http.NewRequest("GET", "https://api.hola.cloud/ongoing", nil)
	if err != nil {
		panic(err)
	}

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/ongoing',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'GET',
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

response = requests.request(
    "GET",
    "https://api.hola.cloud/ongoing"
)

print(response.text)
const response = await fetch("https://api.hola.cloud/ongoing", {
  method: "GET"
});

console.log(await response.text());
const response = await fetch("https://api.hola.cloud/ongoing", {
  method: "GET"
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/ongoing"))
            .method("GET", HttpRequest.BodyPublishers.noBody())
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Siguientes Pasos

Comentarios

Deja un comentario