Skip to content

Commit a83d469

Browse files
authored
Merge pull request #340 from gargnipungarg/apigw-fronting-md
API GW fronting Model deployment endpoint
2 parents 689938b + 958d5cf commit a83d469

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Introduction
2+
3+
This codes showcase how to use [Oracle Fn (Functions)](https://fnproject.io/) to execute Model deployment predict endpoint.
4+
5+
## Pre-Requisites
6+
7+
To be able to test the code described on this page, ensure you have access to the Oracle Data Science Model Deploy in your tenancy as well as Oracle Cloud Applications (aka Functions).
8+
9+
## Local Enviroment Setup
10+
11+
To be able to run this example, you have to install OCI SDK and configure your [API Auth Token](https://docs.oracle.com/en-us/iaas/Content/Registry/Tasks/registrygettingauthtoken.htm).
12+
13+
The OCI API Auth Token is used for the OCI CLI and Python SDK, as well as all other OCI SDK supported languages. Follow the guidance from the online documentation to configure it: <https://docs.oracle.com/en-us/iaas/Content/API/Concepts/apisigningkey.htm>
14+
15+
At the high level the instructions are:
16+
17+
- (1) `login` into your Oracle Cloud
18+
- (2) `select your account` from the top right dropdown menu
19+
- (3) generate a new `API Auth Key`
20+
- (4) download the private key and store it into your `$HOME/.oci` folder on your local machine
21+
- (5) copy the suggested configuration and store it into config file under your home directy `$HOME/.oci/config`
22+
- (6) change the `$HOME/.oci/config` with the suggested configuration in (5) to point to your private key
23+
- (7) test the SDK or CLI
24+
25+
For more detailed explanations, follow the instructions from our public documentation.
26+
27+
### Install Desktop Container Management
28+
29+
This example requires a desktop tool to build, run, launch and push the containers. We support:
30+
31+
- [Docker Desktop](<https://docs.docker.com/get-docker>)
32+
- [Rancher Desktop](<https://rancherdesktop.io/>)
33+
34+
### Install and Configure Oracle Functions
35+
36+
To be able to execute this example you have to install the configure Oracle Fn on your local environment.
37+
38+
- Install Oracle Fn <https://fnproject.io/tutorials/install/>
39+
- Create new context: ```fn create context <your-tenancy-name> --provider oracle```
40+
- Update the Fn context to point to your region, for example: ```fn update context api-url https://functions.<your-region>.oci.oraclecloud.com```
41+
- Update the Fn registry to point to your OCIR: ```fn update context registry <region-key>.ocir.io/<your-tenancy-namespace>/<your-repo-name-prefix>```
42+
- Update the Fn contex to point to your compartment: ```fn update context oracle.compartment-id <your-compartment-ocid>```
43+
44+
# Create OCI Function for invoking Model Deployment predict endpoint
45+
This project helps create a function that will be used as a backend for API Gateway. API Gateway endpoint can then be used as a proxy server infront of Model deployment endpoint. Refer [page](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionscreatingfirst.htm) to refer details about creating a sample function.
46+
For more examples, refer functions examples [repo](https://github.com/oracle-samples/oracle-functions-samples).
47+
48+
# Adding Function as API Gateway Backend
49+
Refer [page](https://docs.oracle.com/en-us/iaas/Content/APIGateway/Tasks/apigatewayusingfunctionsbackend.htm) to find details about how to use function as API Gateway.
50+
51+
# Build using fn cli
52+
```bash
53+
fn -v deploy --app <app-name>
54+
```
55+
56+
# oci-cli based function invocation
57+
```bash
58+
oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"<predict-url>", "PAYLOAD": "<json-payload>"}'
59+
```
60+
61+
## Sample:
62+
```bash
63+
oci fn function invoke --function-id <function-ocid> --file "-" --body '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}'
64+
```
65+
66+
# fn cli based invocation
67+
```bash
68+
fn invoke <app-name> <function-name>
69+
```
70+
71+
## Sample:
72+
```bash
73+
echo -n '{"ENDPOINT":"https://modeldeployment.us-ashburn-1.oci.customer-oci.com/<md-ocid>/predict", "PAYLOAD": "{\"index\": \"1\"}"}' | fn invoke <app-name> <function-name>
74+
```
75+
76+
# More information
77+
The sample code in [func.py](./func.py) also shows how to get headers and request body. Required headers can also be passed to downstream call, if needed.
78+
Other ways of function invocation can be found [here](https://docs.oracle.com/en-us/iaas/Content/Functions/Tasks/functionsinvokingfunctions.htm)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import io
2+
import json
3+
import logging
4+
import oci
5+
import requests
6+
from fdk import response
7+
8+
9+
def handler(ctx, data: io.BytesIO = None):
10+
auth = oci.auth.signers.get_resource_principals_signer()
11+
logger = logging.getLogger()
12+
try:
13+
logger.info("Inside function")
14+
body = json.loads(data.getvalue())
15+
logger.info("Body : " + json.dumps(body))
16+
headers = ctx.Headers()
17+
logger.info("Headers: " + json.dumps(headers))
18+
endpoint = body.get("ENDPOINT")
19+
payload = body.get("PAYLOAD")
20+
resp = requests.post(endpoint, json=json.loads(payload), auth=auth)
21+
logger.info("response : " + resp.json())
22+
except (Exception, ValueError) as ex:
23+
logger.error("Failed to call endpoint with ex : {}".format(str(ex)))
24+
return response.Response(
25+
ctx, response_data=resp.json(),
26+
headers={"Content-Type": "application/json"}
27+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
schema_version: 20180708
2+
name: oci-model-deploy-api-gw
3+
version: 0.0.1
4+
runtime: python
5+
build_image: fnproject/python:3.9-dev
6+
run_image: fnproject/python:3.9
7+
entrypoint: /python/bin/fdk /function/func.py handler
8+
memory: 256
9+
timeout: 60
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fdk>=0.1.60
2+
oci>=2.2.18
3+
requests

0 commit comments

Comments
 (0)