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 2 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Table of Contents:
| **[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] |
| **[Serverless Gateway Python Example](functions/serverless-gateway-python/README.md)** <br/> A Python serverless API using Serverless Gateway. | node18 | [Serverless Framework] |

### 📦 Containers

Expand All @@ -60,6 +61,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 | CaaS |
| **[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
55 changes: 55 additions & 0 deletions functions/serverless-gateway-python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 the a serverless gateway as a 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_URL`) and an authentication token (`$GATEWAY_TOKEN`)

## Running

### Deploy your serverless API

Deploy your functions using:

```
pip install -r requirements.txt
scw_serverless deploy app.py
```

### Add your functions to gateway endpoints

You can add your function to the gateway with a chosen relative path using:
```
sh scripts/add_function_to_gateway.sh http://<function_domain_name> /<chosen_relative_path>
```

### Verify that your function is added to the gateway:

You can use:
```
sh list_gateway_endpoints.sh
```

### Call your functions through gateway

You can use:
```
curl scripts/http://${GATEWAY_URL}/<chosen_relative_path>
```

### Delete your function through gateway

You can use:
```
sh scripts/delete_function_to_gateway.sh http://<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.func()
def func_a(_event, _context):
return "Hello from function A"

@app.func()
def func_b(_event, _context):
return "Hello from function B"

@app.func()
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~=0.0.4
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
curl -X POST http://${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 @@
curl -X POST http://${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 @@
curl http://${GATEWAY_URL}/scw -H 'X-Auth-Token: ${GATEWAY_TOKEN}'