Extend your API with Serverless Functions and API Gateways
Start here to explore how you can extend your APIs with API Gateway and Serverless Functions. The advantage is that you will use the latest libraries and technologies, not to worry about provisioning and scaling to match your workload demand.
Serverless technology is here to stay.
Serverless is the future of the Cloud.
Learn more about API Gateways and why you need one on my previous article:
We will build a serverless function that accepts text files and respond with the gzip file. We will make your function accessible with a secure API Gateway Endpoint.
We will use Oracle Cloud Infrastructure (OCI), specifically Oracle Functions and OCI API Gateway. They are both within the Free Tier of Oracle Cloud.
Do you have an Oracle Cloud Account? If not, you can create one here:
Remember, Oracle Cloud does not charge you anything unless you explicitly request the upgrade to pay as you go. No surprises.
Network and Security
Let’s start with the foundations: network and security.
Policies
We need to create a Policy to enable API Gateway to use Oracle Functions.
Click Create Policy. Set a Name, and Description. Check Show manual editor and write the following policy and click Create:
allow any-user to use functions-family in tenancy
Networking
We need to create a Virtual Cloud Network (VCN) representing the network fabric where API Gateway is going to live.
Click Start VCN Wizard and create a VCN with Internet Connectivity. Select a VCN Name (mine is serverless
) and leave everything else as default. Click Next and Create.
When the creation is completed, you will have two Subnets (one public and one private).
Click on your Public subnet:
Click Default Security List for serverless. And then click Add Ingress Rule. Set the values as follows:
Source: 0.0.0.0/0
Destination Port Range: 443
Description: Allow ingress traffic on TCP/443
API Gateways will listen into port 443. The ingress rule we have just created will allow that traffic from the Internet into the public subnet.
Serverless Function
Imagine you have several serverless functions, and they split across several bounded contexts or apps. That is why you need to create an Application in Oracle Functions, to aggregate similar serverless functions. Let’s do that.
Click Create Application and set the Name, VCN and Private Subnet.
We will click Getting Started on the side menu and use the Cloud Shell guide to configure and get our Serverless function deployed. Stop when you reach step seven and make sure you change [OCIR-REPO]
in step 4 for a registry repository name you like, for example, tools
.
At this point, we will create our serverless function to receive incoming text files and respond with the text file gzipped. Finally, we will deploy the function.
Start by building the function in Cloud Shell with:
fn init --runtime python zipper
Make sure you change the directory of the new Function zipper
:
cd zipper
Edit the func.py
file with the following code:
import io
import logging
import gzipfrom fdk import responsedef handler(ctx, data: io.BytesIO = None):
logging.getLogger().info("gzipping body")
gz_data = gzip.compress(data.getvalue())
logging.getLogger().info("sending response")
return response.Response(
ctx,
response_data=gz_data,
headers={"Content-Type": "application/gzip"}
)
Deploy function with the following command:
fn deploy --app tools
API Gateway
Time to create an OCI API Gateway.
Click Create Gateway and set the following values:
Name: tools
Type: Public
Virtual Cloud Network: serverless
Subnet: Public Subnet-serverless
When the Gateway is Active, it is time to create a Deployment.
The Deployment is just the representation of an API runtime with all the routing to backend APIs.
Click Deployments on the side menu of your new tools
Gateway; click Create Deployment.
Select From Scratch
and set a Name (for example tools
).
Make sure the Path Prefix is /tools/v1
.
Click Next; then, we will create one Route pointing to our zipper serverless function.
Path: /zipper
Methods: POST
Type: Oracle Functions
Application: tools
Function Name: zipper
Invoke your Serverless Function
We will use cURL
to make an HTTPS request to your API Gateway that will redirect the request to your Serverless Function.
Copy the URL of the API Gateway Endpoint from the detail page of API Gateway Deployment:
In Cloud Shell, we will create a file.txt
with content Hello
with the following command:
echo "Hello" > file.txt
Then, we will execute the request (replace <APIGW_ENDPOINT>
with the value for your API Gateway Endpoint):
curl -XPOST -H "Content-Type: text/plain" -d @file.txt <APIGW_ENDPOINT>/tools/v1/zipper > response.txt.gz
The result is going to be a response.txt.gz
that we can unzip with the following command:
gzip -d response.txt.gz
Then we can check the content of the new file, it should say Hello
:
cat response.txt
Congratulations! Feel free to redirect existing requests to your existing API, and extend your API with Oracle Functions to forget about low-level IaaS provisioning. Scale-up and down as needed and pay only for what you use.
Want to learn more?
There are periodic free training sessions, around 1 hour long, delivered by experts and Oracle Cloud Advocates. These are instructor-led training. You can follow along with our team to solve your questions on the spot.
Join me on Oracle Cloud Infrastructure Discord Channel for any question.
And keep tuned for more articles about the amazing things you can build with Oracle Cloud.
I am Victor Martin, a Software Developer. I deploy on Oracle Cloud Infrastructure.
Feel free to get connected with me on LinkedIn.
I am also interested in Scuba diving and space engineering. Happy to help, everything is easier than rocket science!