Archived | Build serverless API handlers
Create auto-scaling actions that respond to API calls
Serverless platforms, like IBM Cloud Functions powered by Apache OpenWhisk, provide a runtime that scales up or down automatically in response to demand. This results in overall lower cloud resource consumption and a better match between business value gained and the cost of the resources that are used. This code pattern shows you how you can use Cloud Functions to map HTTP REST API calls to business logic functions that create, read, update, and delete entities, and how each API method handler action scales (and is billed) independently in direct correlation to the volume of requests.
This project shows how serverless, event-driven architectures execute code in response to demand from HTTP REST API calls. No resources are consumed until the API endpoints are invoked. When they are called, resources are provisioned to exactly match the current load.
It shows four IBM Cloud Functions (based on Apache OpenWhisk) that write and read data in a MySQL database, demonstrating how actions can work with supporting data services and execute logic in response to HTTP requests.
One function, or action, is mapped to HTTP POST requests. It inserts the supplied cat name and color parameters into the database. A second action is mapped to PUT requests to update those fields for an existing cat. A third action is mapped to GET requests that return specific cat data. A fourth action deletes a given cat data.
The Node.js runtime on IBM Cloud provides built-in whitelist of NPM modules. This demo also highlights how additional Node.js dependencies such as the MySQL client can be packaged in a ZIP file with custom actions to provide high extensibility.
- The API client sends an HTTP POST request to the REST API.
- The API gateway receives the request and forwards it to an OpenWhisk action.
- The OpenWhisk action extracts the request body parameters, and uses the NPM MySQL client to create an SQL INSERT.
- The cat data is stored in the database.
- The flow repeats for the GET, PUT, and DELETE requests to read, update, and delete cat data.
Ready to put this code pattern to use? Complete details on how to get started running and using this application are in the README.