Skip to content

Commit 08266d3

Browse files
authored
feat: python serverless api with a self-hosted serverless gateway (#31)
Example of Python Serverless API deployment using Serverless API Framework and a self-hosted Serverless Gateway
1 parent 638a763 commit 08266d3

File tree

7 files changed

+85
-1
lines changed

7 files changed

+85
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Table of Contents:
4646
| **[Rust MNIST](functions/rust-mnist/README.md)** <br/> A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] |
4747
| **[PostgreSQL Python](functions/postgre-sql-python/README.md)** <br/> A Python function to perform a query on a PostgreSQL managed database. | python310 | [Serverless Framework] |
4848
| **[Terraform Python](functions/terraform-python-example/README.md)** <br/> A Python function deployed with Terraform. | python310 | [Terraform] |
49-
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
49+
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
50+
| **[Serverless Gateway Python Example](functions/serverless-gateway-python/README.md)** <br/> A Python serverless API using Serverless Gateway. | python310 | [Python API Framework] |
5051

5152
### 📦 Containers
5253

@@ -61,6 +62,7 @@ Table of Contents:
6162
| Example | Services | Language | Deployment |
6263
|-------------------------------------------------------------------------------------------------------------------------------------------|-------------|----------|------------------------|
6364
| **[Kong API Gateway](projects/kong-api-gateway/README.md)** <br/> Deploying a Kong Gateway on containers to provide routing to functions. | CaaS & FaaS | Python | [Serverless Framework] |
65+
| **[Serverless Gateway](https://github.com/scaleway/serverless-gateway)** <br/> Our serverless gateway for functions and containers. | API Gateway | Python | [Python API Framework] |
6466
| **[Monitoring Glaciers](projects/blogpost-glacier/README.md)** <br/> A project to monitor glaciers and the impact of global warming. | S3 & RDB | Golang | [Serverless Framework] |
6567

6668
[Serverless Framework]: https://github.com/scaleway/serverless-scaleway-functions
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Using Serverless Gateway and Serverless APIs
2+
3+
This example shows how to serve a Serverless API composed of many functions through our serverless gateway.
4+
5+
## Requirements
6+
7+
This example uses:
8+
* [Python API Framework](https://github.com/scaleway/serverless-api-project) to deploy the functions.
9+
* [Serverless Gateway](https://github.com/scaleway/serverless-gateway) to deploy a serverless gateway container.
10+
11+
## Gateway set-up
12+
13+
Deploy the serverless gateway as a container following the Serverless Gateway project instructions.
14+
15+
Make sure to export your gateway base URL (`GATEWAY_HOST`) and an authentication token (`TOKEN`)
16+
17+
## Running
18+
19+
### Deploy your serverless API
20+
21+
Deploy your functions and add them automatically as endpoints to your serverless gateway using:
22+
23+
```
24+
pip install -r requirements.txt
25+
scw-serverless deploy app.py --gateway-url https://${GATEWAY_HOST} --gateway-api-key ${TOKEN}
26+
```
27+
28+
### Check your endpoint has been added
29+
30+
You can use:
31+
```
32+
./list_gateway_endpoints.sh
33+
```
34+
35+
### Call your function via its route
36+
37+
You can use:
38+
```
39+
curl https://${GATEWAY_HOST}/<chosen_relative_path>
40+
```
41+
42+
### Delete your endpoint
43+
44+
You can use:
45+
```
46+
./scripts/delete_function_to_gateway.sh https://<function_domain_name> /<chosen_relative_path>
47+
```
48+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from scw_serverless import Serverless
2+
3+
app = Serverless("serverless-api")
4+
5+
@app.get(url="/func-a")
6+
def func_a(_event, _context):
7+
return "Hello from function A"
8+
9+
@app.get(url="/func-b")
10+
def func_b(_event, _context):
11+
return "Hello from function B"
12+
13+
@app.get(url="/func-c")
14+
def func_c(_event, _context):
15+
return "Hello from function C"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scw-serverless==1.0.1
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
curl -X POST https://${GATEWAY_URL}/scw \
5+
-H 'X-Auth-Token: ${GATEWAY_TOKEN}' \
6+
-H 'Content-Type: application/json' \
7+
-d '{"target":"$1","relative_url":"$2"}'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -e
3+
4+
curl -X POST https://${GATEWAY_URL}/scw \
5+
-H 'X-Auth-Token: ${GATEWAY_TOKEN}' \
6+
-H 'Content-Type: application/json' \
7+
-d '{"target":"$1","relative_url":"$2"}'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
set -e
3+
4+
curl https://${GATEWAY_URL}/scw -H 'X-Auth-Token: ${GATEWAY_TOKEN}'

0 commit comments

Comments
 (0)