Golang Example

Introduction

This example shows how to build and executo a Golang lambda under Gestalt Laser. This will use a build container so that we can be sure of binary compatibility.

We're currently using a docker container built from the ubuntu:trusty.

In order to remain consistent, lambda authors should use the following container to build and publish their executables : galacticfog/build-go:1.0 which is also based on the above listed docker images.

The following is a brief example of how to create a golang lambda.

Requirements

  • docker must be installed on the system
  • artifact hosting like Amazon S3 or similar
  • Gestalt Platform access

HelloWorld Go Lambda

Simplest possible Golang lambda. Note that you must implement main() entry point in order for your lambda to be executed by the system. The code and Makefile for this lambda is container in Hello World Golang

main.go

In order to demonstrate a super simple lambda, please open your favorite text editor and create a lambda that looks like the following :

package main

import "fmt"

func main() {
  fmt.Println("Hello from Gestalt")
}

build.sh

In the example directory there is a build script that shows how to build your executable to be sure it's binary compatible with the container it will be executed in.

The build script does the following :

#!/bin/bash

echo building go executable...

docker run --rm -v "$PWD":/tmp -w /tmp galacticfog/build-go:1.0 /bin/bash -c "go build $1"

As you can see above, you should be in the working directory that contains your main.go file. This docker command will mount the current working directory and then build a Golang executable and leave it in your CWD. You build using the following command :

./build.sh main.go

The next manual step is to build a zip file from your executable. Something like the following :

zip -r hello_golang.zip main

Define the Lambda in Gestalt

  1. Navigate to your desired Environment and on the Create Menu Click Create Lambda
  2. Select the desired Lambda Provider and Name
  3. Select the a golang Runtime and select Package Type
  4. Specify the Handler as main
  5. Specify the Package URL as the place where you hosted your artifact
  6. Make sure to click the Compressed Package radio box to indicate that your artifact needs to be inflated.

No other options are technically necessary for this example

Expose an API Endpoint

Now that we've created our Lambda we need to expose an api endoint.

  1. On the same Lambda screen, in the Public Endpoints section click Add Endpoint
  2. Select an API and click Next
  3. Enter a Relative Path e.g /hello-go
  4. Check only the GET Allowed HTTP Method
  5. Check the Rate Limit and enter 60 (the number of requests allowed per minute)
  6. Click Finish
  7. Click or copy the Public URL which you will use to make your REST API call

Now click on the URL and see the output of your first Hello World in Golang!