Modern cloud applications are moving toward serverless architectures because they reduce infrastructure management and allow developers to focus on code.
Oracle Cloud Infrastructure (OCI)
provides a powerful serverless service called “Oracle Functions”. It allows
developers to run code without managing servers.
In this post, I’ll go through how
to create a simple serverless API using Oracle Functions and expose it through
an HTTP endpoint.
Oracle Functions provides several advantages,
- No server management
- Automatic scaling
- Pay only for execution time
- Easy integration with other OCI services
- User sends an HTTP request
- OCI API Gateway triggers a Function
- Function processes the request
- Response is returned
1.
First create a function using the 'Fn Project
CLI' supported by OCI.
fn init --runtime python myhello-function
cd myhello-function
2.
Open the func.py file and update it.
def handler(ctx, data: bytes=None):
name = "Test"
if data:
name = data.decode('utf-8')
return f"Hello {name} Greetings
from Oracle!"
This will read input and returns a message.
fn deploy --app myapp
After deployment the function will run inside OCI’s
serverless environment.
4.
You can test it using:
echo -n "Pubudu" | fn invoke myapp myhello-function
5.
You should see below output
Hello Pubudu Greetings from Oracle!
You can make this function public by creating an OCI API Gateway, connecting it to the deployed function and generating a public HTTP endpoint. This allows the function to be accessed as a serverless API.
Comments
Post a Comment