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 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 community) with your regular Go code and deploy using the Serverless Framework.
What you’ll need #
Two things need to be installed before we start. If you don’t have these yet, now would be a great time to get them (I’ll wait, I promiseβ¦)
- The Serverless Framework
- An AWS account
Let’s get started!
Create a sample project based on the Flogo template:
serverless create -u https://github.com/tibcosoftware/flogo/tree/master/serverless -p myserviceThat generates the following structure:
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 templateThe content of main.go comes directly from the Lambda trigger. The function.go file has three methods that make up the entire app:
init #
Sets up the defaults β log levels, creates the app by calling shimApp(), and starts the engine.
shimApp #
Builds a new Flogo app and registers the Lambda trigger with the engine. The shim triggers the engine every time an event comes into Lambda and calls RunActivities each time.
RunActivities #
This is where the actual work happens. You get the input from whatever event triggered your Lambda function in a map called evt (part of the inputs). The sample logs “Go Serverless v1.x! Your function executed successfully!” and returns the same as a response. The trigger in main.go handles marshalling it into a proper API Gateway response.
Build and Deploy #
To build the executable for Lambda, run make or make build. That runs two commands under the hood:
go generate ./...: Generates the metadata that the Flogo engine needs for all the activities and triggers you’re using, so it can be compiled into the executable.env GOOS=linux go build -ldflags="-s -w" -o bin/hello hello/*.go: Creates a Linux executable from the sources in the hello folder and puts it in the bin folder.
To deploy, run make deploy or sls deploy. This pushes your function to AWS Lambda.
The output will look something like:
<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 #
Test it with cURL:
curl --request GET --url https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello --header 'content-type: application/json'You should get back:
{"message": "Go Serverless v1.x! Your function executed successfully!"}A little more personal #
That works, but it’s not very useful yet. Let’s update the code to handle both GET and POST and return a more personalized response.
First, update serverless.yml with a new event handler. Around line 58 you’ll find the events section. It already has a GET entry β copy it and change the method to POST.
The response creation after the switch statement stays the same:
For GET, keep the original message: message = "Go Serverless v1.x! Your function executed successfully!"
For POST, reply with the caller’s name:
The complete updated method:
Build and Deploy, again #
Run make deploy to build and deploy the update. The output now shows both endpoints:
<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>The GET still works as before. 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!"}
But when you POST:
curl --request POST --url https://xxx.execute-api.us-east-1.amazonaws.com/dev/hello β header 'content-type: application/json' --data '{"name": "Flynn"}'you get a personalized response:
{"message": "Flynn is going all in on Serverless v1.x!"}What’s next #
If you’re a Go developer who wants to build event-driven apps and deploy with the Serverless Framework, Flogo is worth a look. If you have questions, join the Gitter channel, create an issue on GitHub, or drop me a note on Twitter.