Docker builds for apex(1)

TJ Holowaychuk
1 min readFeb 13, 2017

--

By default the apex Lambda function manager handles cross-compilation for Go programs, however that’s not always enough. When the function requires C bindings, or you’re deploying functions written in a language such a Rust without good cross-compilation support, you can defer to performing builds in Docker.

Amazon provides a dockerhub image “amazonlinux” representing the OS that Lambda runs, so this is a good place to start! The GOPATH is set to /src, which is where we’ll mount the host GOPATH. This prevents the need to copy any source files into the container, and no need to go get all of the dependencies.

FROM amazonlinux
RUN yum install gcc -y
RUN curl -L https://storage.googleapis.com/golang/go1.7.5.linux-amd64.tar.gz | tar -xz -C /usr/local
ENV PATH=$PATH:/usr/local/go/bin
ENV GOPATH=/src

Next you’ll need to perform the build:

docker build -t lambda-go .

Now that you have something to build against, all you need is to adjust your function.json file with a hook. The build hook below sets the working directory to the function’s dir, exposes the host GOPATH, and performs the build.

{
"description": "My cool function",
"timeout": 60,
"memory": 1024,
"hooks": {
"build": "docker run --rm -w /src/src/github.com/tj/myproject/functions/mycoolfunction -v $GOPATH:/src lambda-go go build -o main",
"clean": "rm main"
}
}

Now every time you run apex deploy mycoolfunction Apex will run the build hook to produce main .

In the future Apex may support first-class support for some Docker-based builds to help ease of deploying Rust, Swift, and so on, but hopefully this is useful for now!

--

--