Serverless and Flogo - A Perfect Match (Part 2)

Part 2! 🚀 Explore how to enhance your serverless apps with Flogo and Go API. Unleash the power of Serverless Framework for seamless deployment. Elevate your functions with personalized responses. Dive into the tutorial for a hands-on journey!

Serverless and Flogo - A Perfect Match (Part 2)
Page content

I can hear you think “Part 2?! So there actually is a part 1?” 😱 The answer to that is, yes, there most definitely is a part 1 (but you can safely ignore that 😅). In that part I went over deploying Flogo apps that you built with the Flogo Web UI using the Serverless Framework. Now, with the Go API that we added to Flogo, you can mix triggers and activities from Flogo (and the awesome community) with your regular Go code and… deploy using the Serverless Framework

What you’ll need

There are two things you need to have installed to follow along. If you don’t have these yet, now would be a great time to get them (I’ll wait, I promise…)

Let’s get started!

The first step is to create a sample project based on the Flogo template using a simple command from your terminal or console. Just type (or copy/paste)

serverless create -u https://github.com/tibcosoftware/flogo/tree/master/serverless -p myservice

That command just generated a bunch of files for you

myservice           <-- A directory with the name of your service
├── hello           <-- A folder with the sources of your function
│   ├── function.go <-- A Hello World function
│   └── main.go     <-- The Lambda trigger code, created by Flogo
├── .gitignore      <-- Ignores things you don't want in git
├── Makefile        <-- A Makefile to build and deploy even faster
├── README.md       <-- A quickstart guide
└── serverless.yaml <-- The Serverless Framework template

The content of main.go comes directly from the Lambda trigger. The function.go file has three methods that make up the entire app

init

The init method makes sure that everything is ready to go. It sets the default loglevels, creates an app by calling shimApp() and ultimately starts the engine

shimApp

shimApp is used to build a new Flogo app and register the Lambda trigger with the engine. The shimapp is used by the shim, which triggers the engine every time an event comes into Lambda. It registers the method that it should call each time and in this case that method is RunActivities

RunActivities

RunActivities is where the magic happens. This is where you get the input from any event that might trigger your Lambda function in a map called evt (which is part of the inputs). The sample will simply log “Go Serverless v1.x! Your function executed successfully!” and return the same as a response. The trigger, in main.go, will take care of marshalling it into a proper response for the API Gateway

Build and Deploy

To build the executable that you want to deploy to Lambda, you can run make or make build. That command, in turn, executes two other commands:

  • go generate ./...: In order to run the activities and triggers that you have uses, the Flogo engine needs to have all the metadata available. This command generates the metadata so that it can be compiled into the executable
  • env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/*.go: This command creates the executable (which should run on Linux) from the sources in the hello folder and stores the result (a file called hello) in the bin folder.

To deploy the app run make deploy or, if you don’t want to use make, run sls deploy to get the same result. This command will deploy your function to AWS Lambda.

The output on your screen will have something like what is pasted below:

<snip>
Service Information
service: myservice
stage: dev
region: us-east-1
stack: myservice-dev
api keys:
  None
endpoints:
  GET - https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
  hello: myservice-dev-hello
<snip>

And test

Using cURL you can test your new function

curl --request GET --url https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello --header 'content-type: application/json'

The above command will call your function and return a result:

{"message": "Go Serverless v1.x! Your function executed successfully!"}

A little more personal

That was pretty easy, and pretty cool, but not really useful. The next step is all about updating the code to not only handle GET, but also POST operations and provide a more personalized response.

The first thing is to update the serverless.yml file with a new event handler. Around line 58 of the file you’ll find the events and the types of events that can trigger your app. It already has an entry for GET, copy and paste that and change the method to POST.

Creating the response, so after the switch statement is completed, will still be the same.

In case of a GET operation, the message should still be the same. You can update the GET part by pasting in message = "Go Serverless v1.x! Your function executed successfully!"

In case of a POST operation, the message will be a little different. In that case you want to reply with the name of the person that called your function.

Putting everything together, the new method will look like

Build and Deploy, again

To build and deploy the updates you can use one command, make deploy. This command will build the new executable and deploy your function to AWS Lambda. The output on your screen will have something like what is pasted below:

<snip>
Service Information
service: myservice
stage: dev
region: us-east-1
stack: myservice-dev
api keys:
  None
endpoints:
  GET - https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello
  POST - https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello
functions:
  hello: myservice-dev-hello
<snip>

Using cURL you can test that your function still works for a GET operation. The command curl --request GET --url https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello — header 'content-type: application/json' should still respond with {"message": "Go Serverless v1.x! Your function executed successfully!"}

When you POST a message, though

curl --request POST --url https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello — header 'content-type: application/json' --data '{"name": "Flynn"}'

the result should be different

{"message": "Flynn is going all in on Serverless v1.x!"}

What’s next

So, if you’re a Go developer and you want to build event-driven apps and deploy using the Serverless Framework… You can now truly do it using Flogo! The next step would be to try Flogo and if you’re trying out Flogo and have any questions, feel free to join our Gitter channel, create an issue on GitHub or even drop me a note on Twitter. I’d also love to get your feedback if you thought this was helpful (or not).