Leon Stigter

@retgits

Serverless and Flogo: A Perfect Match

Flynn loves Lambda!

I get to work with serverless microservices on a daily basis, those are services I use myself and ones I help our customers build to take advantage of the benefits that serverless brings you. With many services needing to be deployed and continuous updates, I found myself doing the same thing over and over. It is that specific task that frustrates me most; it simply wasn’t as seamless as I thought it could be.

In this article, I’ll walk you through how I cut the development time and make deployments easily repeatable like a walk in the park — thanks to the combination of the Serverless Framework and a tool called Project Flogo.

What is Flogo?

I’m guessing that you know about the Serverless Framework, but you might not know what Flogo is. Well, Flogo is an Ultralight Edge Microservices Framework! Now that we got the tagline out of the way, let’s unwrap that statement a bit. Flogo is written in Go and now that you can run Go on Lambda, you can easily package up your service and run it on Lambda. Flogo provides you with a visual environment to design your service which, overly simplified, means you put a bunch of activities (like sending a message to Amazon SNS) in a row and execute them when an event occurs.

Together with the Serverless Framework, you can configure which events should trigger it, where to deploy it and what kind of resources it is allowed to use without going into the AWS console. The thing I’m personally very excited about is how easy configuration management is and how easy you can move your service to a new stage.

Prerequisites

In this tutorial, I’ll walk you through creating your app, as well as deploying it using Serverless.

You’ll need to have:

  1. The Serverless Framework and Go installed
  2. An AWS account

if you don’t have that done yet, the links will guide you through the steps.

Installing the Project Flogo CLI

To build the Flogo app, we’ll make use of the Flogo CLI.

Install it like so:

go get -u github.com/TIBCOSoftware/flogo-cli/...

To simplify dependency management, we’re using the Go dep tool. (Note that dep strongly recommends using the binary releases that are available on the releases page.)

Creating your app

Because of the way dep works, you’ll need to execute the commands from within your ${GOPATH}/src directory.

Let’s create a directory called serverlesslambdaapp:

cd $GOPATH/src
mkdir serverlesslambdaapp

And a flogo.json file in that directory:

A Flogo app, ready for Lambda

With that done, you’ll need just one command to turn it into a Flogo app that you can use later on to build the executable from:

flogo create -f flogo.json lambda

The above command will create a directory called lambda, find all the dependencies the flow has, and download them. It might take a few seconds for this command to complete.

Now, we can create an executable out of that project. To run in AWS Lambda, we’ll need to embed the flogo.json in the application to make sure there are no external file dependencies. (You can still make use of environment variables, but we’ll cover that in a different tutorial.)

The trigger for Lambda, which contains the event information, makes use of a Makefile to build and zip the app. So let’s run:

cd lambda
flogo build -e -shim my_lambda_trigger

After the flogo build command there are two important files in the ./src/lambda. One file is called handler.zip; this is a zipped executable that you can upload to Lambda. The other is simply called handler, and is the unzipped version.

While you could absolutely use the command line tools that AWS provides to deploy your app, or even upload it manually, it’s much easier to automate that part — especially as your app becomes more complex. This is why I love the Serverless Framework

Deploying Apps with Serverless

The team at Serverless did an amazing job making deployments and packaging really simple. From here you only need a few steps.

The first thing is to create a new Serverless service in the same folder as your flogo.json file (if you’ve followed along with the commands, you should still be there :-)):

# Let’s create a serverless service with the same name as the app
serverless create -u https://github.com/retgits/flogo-serverless -p serverlesslambdaapp

The next step is to copy the handler over to the newly-created Serverless folder:

cp src/lambda/handler serverlesslambdaapp/handler

This would be an ideal time to update your serverless.yml file with any bucket names, IAM roles, environment variables or anything that you want to configure, because the only thing left is to deploy!

# To package up your function before deploying run
cd serverlesslambdaapp
serverless package
# To deploy, which also does the packaging, your function run
cd serverlesslambdaapp
serverless deploy

*Note: this unfortunately only works under Linux or macOS systems, or when using the Windows Subsystem for Linux (WSL). This is because Windows developers may have trouble producing a zip file that marks the binary as executable on Linux. See here for more info).

Testing 1… 2… 3…

Let’s test the app to make sure that it really deploys to Lambda and runs correctly.

After you log into AWS and select ‘Lambda’, you’ll be presented with all the functions you’ve deployed so far. One of them should be called something like serverlesslambdaapp-dev-hello.

Click on that, and you’ll see the overview of your function, including a large button that says ‘Test’. Click ‘Test’ to configure a new test event (any input will do), and click ‘Test’ again to run it.

If all went well, and why shouldn’t it, your log will show a line like 2018–03–07 00:18:34.735 INFO [activity-tibco-log] — Hello World from Serverless and Flogo

testing…

Want to try yourself?

Excited to try even more things with Flogo? Check out our docs or the website

More by Leon Stigter

Topics of interest

More Related Stories