Serverless function — Quickstart Templates

You have probably have seen my other article that helps you get started writing your very first event-driven function using VMware Event Broker Appliance (VEBA) for your vCenter infrastructure. I highlight the steps taken to write my first serverless function and provide templates to help you get started quickly!

Writing your first Serverless Functionhere

I also wanted to build on top of my t-shirt sized templates and provide a template where users can focus on writing their core business logic, be able to test quicker and debug easily!

Small — provides a minimal template to get started from scratch complete with a readymade README.md file

MediumSmall + work with configurations and ready-made handle function; jump right into writing your business logic.

Large — Medium+ improvements :)

Here are some of the things that i wanted to do

Improvements

  • Move helper functions to another file; only have the necessary lines of code within the handler.py
  • Make the function debuggable without having anyone touch and rebuild the code
  • Make some settings be driven through environment variables
  • Improve unit testing for faster function development

With this in mind, i now have the below structure created and the template is available here for you to build on top of.

Let’s explore what this looks like..

Image for post
Image for post
FaaSResponse class

Our function had a few utility classes that helped sending the responses back to OpenFaaS, this was moved the util.py file to keep our handler.py clean.

Image for post
Image for post
stack.yml for OpenFaaS

Logging in functions (with the templates that we are using) to stdout and stderr are both combined by default, this can be changed by setting an environment variable combine_output to false as shown in the stack.yml file.

Image for post
Image for post
WARN if debug is enabled
Image for post
Image for post
Logger utility class

This allows us to write stderr debug messages while stdout gets sent back to OpenFaaS as the output. The function WARN users that debug is enabled and sensitive information could be logged into the console.

With this in place, a logger function was added to produce colorized output in the console for readability and enhanced debuggability.

Image for post
Image for post
test.py provided for local testing

The util.py is now imported into the handler.py and abstracted away for the function developer who can now focus only on building the core business logic for their usecase!

I’ve also moved all the test cases to outside of the package and located within the test.py and it’s been improved to run multiple test cases in one go. Make sure to edit this file to update with test cases that are relevant to what you are looking to accomplish.

These changes certainly helped me write, test and debug my functions faster. While OpenFaaS has an easy process to build, deploy and invoke your function with an OpenFaaS gateway in place, the idea behind these changes and providing a local test script is to speed up the development/testing process by a few hours.

See this template in action!

I’ve used this template and structure to build a function that can make a POST REST API call to any system (think cURL or Postman but for VEBA). This enables our users to deploy a readymade function and only have to provide the right configuration to have it work with various external systems. This function has been tested to work with Slack, Pagerduty and ServiceNow!

Let me know how these templates help you innovate for your SDDC with VEBA and we can look into making templates in other languages.

Happy Eventing!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store