In this lab you will be performing an API call to the Twilio text messaging API. The goal of this will be creating the serverless function, invoking it, and then putting it behind an API Gateway.
In this lab, we will finish the architecture diagram from the README by invoking a function from an OCI event.
To begin with this lab, you can copy the text-func folder in this repo, or it will be created for you by running the first cli command below. In the OCI console or via the CLI, you will have to create an application for the function to be deployed to - see lab 301. The yaml file in the folder is fine as it is, or it can be created with the fn init command below.
fn init --runtime python text-func
cd text-func
After this, you will have to update func.py and requirements to your API credentials, copies of these scripts are at the very bottom of this repo for reference.
fn list app
fn -v deploy --app TwilioText
fn invoke TwilioText text-func
After invoking my function, I have received a serverlessly generated text message.
From the OCI documentation we can get the syntax necessary for the API's json file.
My JSON file is below:
{
"requestPolicies": {},
"routes": [
{
"path": "/hello",
"methods": ["GET"],
"backend": {
"type": "ORACLE_FUNCTIONS_BACKEND",
"functionId": "{YOUR FUNCTION'S OCID}"
},
"requestPolicies": {}
}
]
}
To get your function's ocid, navigate to developer services, your application, and then your function.
Then navigate to your gateway, create a new deployment, and upload your JSON file, I named the path prefix '/api' .
Now copy the endpoint from your deployment details page to Postman and add '/hello' to the end. Select the get operation and press send.
Congratulations, you have just received a text message by invoking a serverless function, protected by API Gateway, that performs an API call to the Twilio text messaging API.
Note: this code will give a 500 response and the response below, however the text message is still sent. In a future lab we will discuss how to improve code for functions.
{
"code": "FunctionInvokeExecutionFailed",
"message": "function failed"
}
If you did lab 100 or lab 200, this part may be self explanatory.
Begin by returning to your previously created rule.
Input your function's details.
Congratulations, now upon successful object creation you will receive an email, a text message, and a message will be added to your stream.
This app is an alternative method to call ATP instead of using ORDS like in my previous lab.
Signing requests with python, bash, and more for OCI API
You can use serverless with the OCI API with proper authentication with signing requests.
The new application development stack
Learn ci/cd for functions, video playlist
Using Key Management Services with functions
import io
import json
import requests
import fdk
from fdk import response
def apicall():
url = "https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json"
payload = {'Body': 'Hello there, from OCI.',
'To': '+12345',
'From': '+12345'}
files = [
]
headers = {
'account_sid': 'LK88ea8d8a087c7ad',
'Authorization': 'Basic 87vzlhuadfyklh8wODQ4OTkzOGY2Yzo1Zjg1ZWM5OW08754671487149876142NA=='
}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))
apicall()
fdk
requests