A quick foray into AWS Lamba and Golang

Gleicon Moraes
2 min readAug 10, 2016

--

AWS Lambda is the flagship product of the serverless architecture. Well integrated into the whole AWS ecosystem, it is a packaging and orchestrating system based on containers.

Natively it runs Javascript, Java and Python but through what is called a "shim" it can run programs in other languages, as long as the runtime is compatible with Amazon Linux. You can ship binaries such as phantomjs with your code and exec them from your code. A function is called through manual request or events. Events can be from S3, Kinesis and AWS API Gateway. The synergy with AWS API gateway in good and you can come up with a set of simple api just by clicking around.

I had the opportunity of working with Lambda last year to automate AWS WAF rule creation from Cloudfront log analysis. The console interface is not that bad and there are plenty of tools that implement a sane development cycle with versioning and artifacts packaging for it.

One of these tools is called apex, written in Golang and supporting many languages and a configuration system based on json files. Apex is elegant and clean, providing a terminal based interface to manage both your code and your AWS setup.

It creates the project structure, setup lambda into the right region, ARN and so on. Also it will send whatever your program need along to lambda so you wont need to track all dependencies.

The manual won't go too far on Golang development but you need it to setup your environment properly. There are libraries you need to install and wrap your code so Apex packaging system will be able to pass and collect data from your program.

$ go get -u github.com/apex/go-apex

Your code will live inside an Apex Handler Function:

apex.HandleFunc(func(event json.RawMessage, ctx *apex.Context) (interface{}, error) {
... code here...
})

After a couple of Hello World tests, I created a load test lambda that uses Vegeta to run a load test to a website it receives through events. The code is on github.

{"event":{"value":"http://www.example"}}

I ran my function from command line with

echo '{"event":{"value":"http://www.example"}}' | apex invoke loadtest | python -m json.tool

The output is Vegeta's own statistics with median, 99th percentile, errors and return codes.

You may have mixed functions inside a project, each one using a different runtime by configuring a function.json file for each

{ 
“runtime”:”golang”,
“timeout”:30
}

There is a global file created by apex which controls memory footprint, ARN and other sensible settings. You will be alright with the defaults. I've increased this function's timeout because the test usually takes longer than 5 seconds to run.

Other IaaS players are releasing similar platforms and I hope Apex follows up to add compatibility.

--

--