Serverless function learning environments across Amazon, Microsoft, and Google clouds

by Brian Fitzgerald

Introduction

If you want to dip your toe into serverless function programming, you will want to try it out in a simple web-based environment with all the needed syntax setup for you. That way, you can at least get to “Hello World!” without delay or error.

Across three cloud providers, Amazon, Microsoft, and Google, online edit availability varies across languages and operating systems. Here is a brief summary.

Amazon Web Services

AWS serverless functions, Lambda, are available in seven languages, C#, Go, Java, JavaScript, Powershell, Python, and Ruby. You can experiment with some simple coding by entering your choice of JavaScript, Python or Ruby code into the online code editor. If you want to use C#, Go, Java, or Powershell, you will have to develop and test your files outside Lambda, put them in a zip file, and upload the zip file. The Lambda console also accepts a jar file for upload. A Lambda java upload needs class and jar files, not java source files. Also, a jar file can contain bytecode compiled from other languages that run in a JRE, so, for example, you can write a Lambda in scala or clojure.

Saving code changes from the AWS Console is quick, usually under one second. Python code is saved without syntax checking. There is one quirk. Tabs in sources get copied to the clipboard as spaces. I refer to the Lambda Management console in Chrome on Windows.

You can export your function, and in that way, get your source files out after you have tested them.

A python Lambda function can return any data type that is JSON serializable, such as  dict, list, tuple, Boolean, scalars, None, and hierarchies of these, but not, for example, set, date, datetime, class, or object.

Azure

Azure Functions are offered in five languages: C#, Java, JavaScript, Powershell, and Python. Azure functions can be administered online in the Azure portal. Azure offers a choice of Windows or Linux for your function, but online edit is only available for the Windows Function Apps. Python runs on Linux only, which rules out online edit. Creating Java or Go functions is supported only by upload. Online edit, therefore, is available for C#, JavaScript, and Powershell.

An Azure function sits inside a FunctionApp. FunctionApp names must be unique across all Azure. You cannot name your Azure FunctionApp “spam” or “eggs”, and you cannot name your Azure FunctionApp “SpamAndEggs” unless I delete my Azure FunctionApp “SpamAndEggs”.

spamandeggs

FunctionApp creation can take more than 1 minute. When creation finishes, the function list displayed in the portal does not refresh when the function is ready, and you could miss the notification. Saving your code from the portal is almost instantaneous. Compile and run takes less than 1 second. You can zip and download your finished code by pressing Download app content.

Press tab in the online code results in saving space characters, which will be less of a problem, since you won’t be editing python source online.

Google

In Google Cloud Platform, you can create a Google Cloud Function. The language choices are Go, JavaScript, and Python, and you can enter all code using the online editor.

When you finish editing, you press “Deploy”, which can run for up to 1 minute.Syntax errors lead to failed deployment. While testing the code, you can view it read-only.  If you want to make a change, you have to go back to the edit screen. You may download your finished code as a zip file.

Google Cloud function return type is limited to string, tuple, Response instance, or WSGI callable.

Summary

Here is a summary of programming languages across cloud providers.

Language AWS Azure Google
C# upload only online edit not available
Go upload only not available online edit
Java upload only upload only not available
JavaScript online edit online edit online edit
Powershell upload only online edit not available
Python online edit upload only online edit
Ruby online edit not available not available

JavaScript is universally available for learning: You can quickly create a Hello World serverless function using an online editor on any cloud platform. On the other hand, if you are a hard-core java programmer, you are going to need to work out how to upload your code. You could upload code from your IDE, for example. If you want to learn C# or Powershell cloud programming, Azure is the place to be. If you want to explore Go, then go to Google.

 

Using operators >> and << for push and pop in a C++ stack

By Brian Fitzgerald

The C++ “>>” (extraction operator) and “<<” (insertion operator) are normally used with I/O streams (cin/cout), but you can overload these operators in your own classes and make them do other things. In this case, I implement a stack of integers. “>>” is used for push and “<<” is used for pop. Here is Stk.cpp, containing the Stk class routines:

#include "Stk.h"

// push i onto the stack

Stk& operator >>(Stk& st, int i) {
	st.ints = (int *) realloc(st.ints, (++st.len * sizeof(int)));
	st.ints[st.len - 1] = i;
	return st;
}

// pop i off the stack
Stk& operator <<(Stk& st, int &i) {
	if (st.len == 0) {
		throw "empty stack";
	}
	i = st.ints[--st.len];
	return st;
}

int Stk::size() {
	return len;
}

int Stk::peek() {
	return ints[len - 1];
}
bool Stk::isempty() {
	return len == 0;
}

A few things to notice:

  • The stack is implemented as ints, an array of int.
  • Push resizes the array with a call to realloc.
  • Operators >> and << are not members of class Stk
  • They are implemented as friends.
  • The friend functions have access to private variables ints and len.
  • In >> and <<, the type of the first argument and the return is &Stk.

Here is the header:

#include 

#ifndef STK_H_
#define STK_H_

class Stk {
public:
	int size();
	int peek();
	bool isempty();
	friend Stk& operator >>(Stk& st, int i);
	friend Stk& operator <<(Stk& st, int &i);
private:
	int *ints = NULL;
	int len = 0;
};

#endif /* STK_H_ */

Here is the main program:

#include 
#include "Stk.h"
using namespace std;

int main() {
	cout << "begin" << endl;

	Stk st;
	cout << "push 3: 13, 17, 19" << endl; 	st >> 13 >> 17 >> 19;

	cout << "stack size=" << st.size() << endl;
	cout << "pop the stack" << endl;
	while (!st.isempty()) {
		int n;
		st << n;
		cout << "popped n=" << n << endl;
	}
	cout << "stack size=" << st.size() << endl;

	cout << "push 3: 29, 31, 37" << endl; 	st >> 29 >> 31 >> 37;
	int i, j, k;
	cout << "pop 2" << endl;
	st << i << j;
	cout << "i=" << i << " j=" << j << endl;
	cout << "peek " << st.peek() << endl;
	cout << "stack size=" << st.size() << endl;
	cout << "push 2: 41, 47" << endl; 	st >> 41 >> 47;
	cout << "pop 1" << endl;
	st << k;
	cout << "popped k=" << k << endl;
	cout << "stack size=" << st.size() << endl;
	// int p, q, r;
	// st << p << q << r; // empty stack exception
	cout << "done" << endl;
	return 0;
}

Points to notice:

  • Expression st >> 13 >> 17 >> 19 is evaluated left to right.
  • st >> 13 is evaluated. The result is st, the stack with 13 pushed.
  • Internally, the partially evaluated expression is now st >> 17 >> 19.
  • st >> 17 is evaluated, and so on.

Output:

C:\Users\Brian Fitzgerald\eclipse-workspace\Stk\Debug>Stk.exe
begin
push 3: 13, 17, 19
stack size=3
pop the stack
popped n=19
popped n=17
popped n=13
stack size=0
push 3: 29, 31, 37
pop 2
i=37 j=31
peek 29
stack size=1
push 2: 41, 47
pop 1
popped k=47
stack size=2
done

This is my Saturday project. The purpose was to refresh some skills and to use C++ language features in an interesting way. I saw something similar in one client’s Sybase CT-Library variable binding code. If we’re going to migrate the application to some other database connectivity library, we need to understand how the existing code works, and from there, develop a a migration action plan.

I am a database administrator, not a developer. Any comments or suggestions from anyone in any field are welcome.