Skip to content

feat: python serverless api with serverless gateway #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Table of Contents:
| **[Rust MNIST](functions/rust-mnist/README.md)** <br/> A Rust function to recognize hand-written digits with a simple neural network. | rust165 | [Serverless Framework] |
| **[PostgreSQL Python](functions/postgre-sql-python/README.md)** <br/> A Python function to perform a query on a PostgreSQL managed database. | python310 | [Serverless Framework] |
| **[Terraform Python](functions/terraform-python-example/README.md)** <br/> A Python function deployed with Terraform. | python310 | [Terraform] |
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
| **[Typescript with Node runtime](functions/typescript-with-node/README.md)** <br/> A Typescript function using Node runtime. | node18 | [Serverless Framework] |
| **[Serverless Gateway Python Example](functions/serverless-gateway-python/README.md)** <br/> A Python serverless API using Serverless Gateway. | python310 | [Python API Framework] |

### 📦 Containers

Expand All @@ -61,6 +62,7 @@ Table of Contents:
| Example | Services | Language | Deployment |
|-------------------------------------------------------------------------------------------------------------------------------------------|-------------|----------|------------------------|
| **[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] |
| **[Serverless Gateway](https://github.com/scaleway/serverless-gateway)** <br/> Our serverless gateway for functions and containers. | API Gateway | Python | [Python API Framework] |
| **[Monitoring Glaciers](projects/blogpost-glacier/README.md)** <br/> 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
Expand Down
48 changes: 48 additions & 0 deletions functions/serverless-gateway-python/README.md
Original file line number Diff line number Diff line change
@@ -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}/<chosen_relative_path>
```

### Delete your endpoint

You can use:
```
./scripts/delete_function_to_gateway.sh https://<function_domain_name> /<chosen_relative_path>
```

15 changes: 15 additions & 0 deletions functions/serverless-gateway-python/app.py
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions functions/serverless-gateway-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scw-serverless==1.0.1
Original file line number Diff line number Diff line change
@@ -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"}'
Original file line number Diff line number Diff line change
@@ -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"}'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

curl https://${GATEWAY_URL}/scw -H 'X-Auth-Token: ${GATEWAY_TOKEN}'