diff --git a/README.md b/README.md index 1ed8d28..40e7de3 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,8 @@ Table of Contents: | **[Rust MNIST](functions/rust-mnist/README.md)**
A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] | | **[PostgreSQL Python](functions/postgre-sql-python/README.md)**
A Python function to perform a query on a PostgreSQL managed database. | python310 | [Serverless Framework] | | **[Terraform Python](functions/terraform-python-example/README.md)**
A Python function deployed with Terraform. | python310 | [Terraform] | -| **[Typescript with Node runtime](functions/typescript-with-node/README.md)**
A Typescript function using Node runtime. | node18 | [Serverless Framework] | +| **[Typescript with Node runtime](functions/typescript-with-node/README.md)**
A Typescript function using Node runtime. | node18 | [Serverless Framework] | +| **[Serverless Gateway Python Example](functions/serverless-gateway-python/README.md)**
A Python serverless API using Serverless Gateway. | python310 | [Python API Framework] | ### 📦 Containers @@ -61,6 +62,7 @@ Table of Contents: | Example | Services | Language | Deployment | |-------------------------------------------------------------------------------------------------------------------------------------------|-------------|----------|------------------------| | **[Kong API Gateway](projects/kong-api-gateway/README.md)**
Deploying a Kong Gateway on containers to provide routing to functions. | CaaS & FaaS | Python | [Serverless Framework] | +| **[Serverless Gateway](https://github.com/scaleway/serverless-gateway)**
Our serverless gateway for functions and containers. | API Gateway | Python | [Python API Framework] | | **[Monitoring Glaciers](projects/blogpost-glacier/README.md)**
A project to monitor glaciers and the impact of global warming. | S3 & RDB | Golang | [Serverless Framework] | [Serverless Framework]: https://github.com/scaleway/serverless-scaleway-functions diff --git a/functions/serverless-gateway-python/README.md b/functions/serverless-gateway-python/README.md new file mode 100644 index 0000000..b059b54 --- /dev/null +++ b/functions/serverless-gateway-python/README.md @@ -0,0 +1,48 @@ +# Using Serverless Gateway and Serverless APIs + +This example shows how to serve a Serverless API composed of many functions through our serverless gateway. + +## Requirements + +This example uses: +* [Python API Framework](https://github.com/scaleway/serverless-api-project) to deploy the functions. +* [Serverless Gateway](https://github.com/scaleway/serverless-gateway) to deploy a serverless gateway container. + +## Gateway set-up + +Deploy the serverless gateway as a container following the Serverless Gateway project instructions. + +Make sure to export your gateway base URL (`GATEWAY_HOST`) and an authentication token (`TOKEN`) + +## Running + +### Deploy your serverless API + +Deploy your functions and add them automatically as endpoints to your serverless gateway using: + +``` +pip install -r requirements.txt +scw-serverless deploy app.py --gateway-url https://${GATEWAY_HOST} --gateway-api-key ${TOKEN} +``` + +### Check your endpoint has been added + +You can use: +``` +./list_gateway_endpoints.sh +``` + +### Call your function via its route + +You can use: +``` +curl https://${GATEWAY_HOST}/ +``` + +### Delete your endpoint + +You can use: +``` +./scripts/delete_function_to_gateway.sh https:// / +``` + diff --git a/functions/serverless-gateway-python/app.py b/functions/serverless-gateway-python/app.py new file mode 100644 index 0000000..3099c76 --- /dev/null +++ b/functions/serverless-gateway-python/app.py @@ -0,0 +1,15 @@ +from scw_serverless import Serverless + +app = Serverless("serverless-api") + +@app.get(url="/func-a") +def func_a(_event, _context): + return "Hello from function A" + +@app.get(url="/func-b") +def func_b(_event, _context): + return "Hello from function B" + +@app.get(url="/func-c") +def func_c(_event, _context): + return "Hello from function C" \ No newline at end of file diff --git a/functions/serverless-gateway-python/requirements.txt b/functions/serverless-gateway-python/requirements.txt new file mode 100644 index 0000000..fdd6a7b --- /dev/null +++ b/functions/serverless-gateway-python/requirements.txt @@ -0,0 +1 @@ +scw-serverless==1.0.1 \ No newline at end of file diff --git a/functions/serverless-gateway-python/scripts/add_function_to_gateway.sh b/functions/serverless-gateway-python/scripts/add_function_to_gateway.sh new file mode 100644 index 0000000..2a9237a --- /dev/null +++ b/functions/serverless-gateway-python/scripts/add_function_to_gateway.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +curl -X POST https://${GATEWAY_URL}/scw \ + -H 'X-Auth-Token: ${GATEWAY_TOKEN}' \ + -H 'Content-Type: application/json' \ + -d '{"target":"$1","relative_url":"$2"}' \ No newline at end of file diff --git a/functions/serverless-gateway-python/scripts/delete_function_from_gateway.sh b/functions/serverless-gateway-python/scripts/delete_function_from_gateway.sh new file mode 100644 index 0000000..2a9237a --- /dev/null +++ b/functions/serverless-gateway-python/scripts/delete_function_from_gateway.sh @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +curl -X POST https://${GATEWAY_URL}/scw \ + -H 'X-Auth-Token: ${GATEWAY_TOKEN}' \ + -H 'Content-Type: application/json' \ + -d '{"target":"$1","relative_url":"$2"}' \ No newline at end of file diff --git a/functions/serverless-gateway-python/scripts/list_gateway_endpoints.sh b/functions/serverless-gateway-python/scripts/list_gateway_endpoints.sh new file mode 100644 index 0000000..9f95f05 --- /dev/null +++ b/functions/serverless-gateway-python/scripts/list_gateway_endpoints.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e + +curl https://${GATEWAY_URL}/scw -H 'X-Auth-Token: ${GATEWAY_TOKEN}' \ No newline at end of file