Skip to main content
Deploying Flogo Apps to Lambda with the Serverless Framework (Part 2)
  1. Blog/

Deploying Flogo Apps to Lambda with the Serverless Framework (Part 2)

·4 mins·

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…)

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 myservice

That 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 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
#

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.

Related

How To Build Securely Chatting Microservices With Flogo And PubNub

·5 mins
Building microservices is awesome, having them talk to each other is even more awesome! But in today’s world, you can’t be too careful when it comes to sending sensitive data across the wire. Last week I was at PubNub for a Meetup where, together with Jordan Schuetz and Nicholas GreniΓ©, we spoke about cool things you can do with PubNub. One of them is using PubNub as a messaging layer to have your microservices, built with Flogo (duh), talk to each other in a secure way. In this post, I’ll go over the steps to build those microservices and hook them up using PubNub.

Adopting Serverless Computing with TIBCO & AWS

·4 mins
As the AI-fueled, edge-exposed, blockchain-driven, and streaming analytics-enabled use cases of the future move closer into view, new technologies are needed to make the vision real. Unique and complex workloads accompany the use cases of the future, but luckily, the enabling technologies to compute those workloads have already arrived. Join TIBCO and AWS for an exciting webinar to help you better understand what serverless architecture is all about, and the benefits of running your apps in a serverless environment. Before you give a listen, how about a quick introduction?