Skip to content

Commit

Permalink
Add support for go1.x runtime (lambci#67)
Browse files Browse the repository at this point in the history
* Add support for go1.x runtime

* Add go1.x build image

* Add go1.x example

* Update README for go1.x
  • Loading branch information
mhart authored Jan 22, 2018
1 parent 5d7b8c9 commit f94bdd5
Show file tree
Hide file tree
Showing 11 changed files with 599 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ base/dump-java8/target
examples/java/build
examples/java/target
examples/java/bin
go1.x/run/aws-lambda-mock
go1.x/run/vendor
examples/go1.x/handler
examples/go1.x/vendor
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ same library versions that exist on AWS Lambda and then deploy using
the [AWS CLI](https://aws.amazon.com/cli/).

This project consists of a set of Docker images for each of the supported Lambda runtimes
(Node.js 0.10, 4.3 and 6.10, Python 2.7 and 3.6, and Java 8).
(Node.js 0.10, 4.3 and 6.10, Python 2.7 and 3.6, Java 8, and Go 1.x).

There are also a set of build images that include packages like gcc-c++, git,
zip and the aws-cli for compiling and deploying.
Expand Down Expand Up @@ -52,6 +52,9 @@ docker run --rm -v "$PWD":/var/task lambci/lambda:python2.7
# Test on Python 3.6 with a custom file named my_module.py containing a my_handler function
docker run --rm -v $PWD:/var/task lambci/lambda:python3.6 my_module.my_handler

# Test on Go 1.x with a compiled handler named my_handler and a custom event
docker run --rm -v $PWD:/var/task lambci/lambda:go1.x my_handler '{"some": "event"}'

# Test a function from the current directory on Java 8
# The directory must be laid out in the same way the Lambda zip file is,
# with top-level package source directories and a `lib` directory for third-party jars
Expand All @@ -75,6 +78,9 @@ docker run --rm -v "$PWD":/var/task lambci/lambda:build
# To use a different runtime from the default Node.js v4.3
docker run --rm -v "$PWD":/var/task lambci/lambda:build-nodejs6.10

# To resolve dependencies on go1.x (working directory is /go/src/handler, will run `dep ensure`)
docker run --rm -v "$PWD":/go/src/handler lambci/lambda:build-go1.x

# Run custom commands on a build container
docker run --rm lambci/lambda:build aws --version

Expand Down Expand Up @@ -169,12 +175,14 @@ Docker tags (follow the Lambda runtime names):
- `python2.7`
- `python3.6`
- `java8`
- `go1.x`
- `build` / `build-nodejs4.3`
- `build-nodejs`
- `build-nodejs6.10`
- `build-python2.7`
- `build-python3.6`
- `build-java8`
- `build-go1.x`

Env vars:
- `AWS_LAMBDA_FUNCTION_NAME`
Expand Down
146 changes: 146 additions & 0 deletions base/dump-go1x.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package main

import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/s3"
"log"
"os"
"os/exec"
)

func HandleRequest(ctx context.Context, event interface{}) (*s3.PutObjectOutput, error) {
filename := "go1.x.tgz"

RunShell("tar -cpzf /tmp/" + filename + " --numeric-owner --ignore-failed-read /var/runtime /var/lang")

fmt.Println("Zipping done! Uploading...")

cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
log.Fatal(err)
}

file, err := os.Open("/tmp/" + filename)
if err != nil {
log.Fatal(err)
}

resp, err := s3.New(cfg).PutObjectRequest(&s3.PutObjectInput{
ACL: s3.ObjectCannedACLPublicRead,
Body: file,
Bucket: aws.String("lambci"),
Key: aws.String("fs/" + filename),
}).Send()
if err != nil {
log.Fatal(err)
}

fmt.Println("Uploading done!")

RunShell("ps aux")

RunShell("xargs --null --max-args=1 < /proc/1/environ")

for _, a := range os.Args {
fmt.Println(a)
}
pwd, _ := os.Getwd()
fmt.Println(pwd)
for _, e := range os.Environ() {
fmt.Println(e)
}
fmt.Println(ctx)

return resp, nil
}

func RunShell(shellCmd string) {
cmd := exec.Command("sh", "-c", shellCmd)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Run()
}

func main() {
lambda.Start(HandleRequest)
}

/*
PATH=/usr/local/bin:/usr/bin/:/bin
LANG=en_US.UTF-8
TZ=:UTC
LD_LIBRARY_PATH=/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib
_LAMBDA_CONTROL_SOCKET=15
_LAMBDA_CONSOLE_SOCKET=17
LAMBDA_TASK_ROOT=/var/task
LAMBDA_RUNTIME_DIR=/var/runtime
_LAMBDA_LOG_FD=24
_LAMBDA_SB_ID=8
_LAMBDA_SHARED_MEM_FD=12
AWS_REGION=us-east-1
AWS_DEFAULT_REGION=us-east-1
AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/dump-go1x
AWS_LAMBDA_LOG_STREAM_NAME=2018/01/16/[$LATEST]12d47417179844e3ad55190a93a817d7
AWS_LAMBDA_FUNCTION_NAME=dump-go1x
AWS_LAMBDA_FUNCTION_MEMORY_SIZE=3008
AWS_LAMBDA_FUNCTION_VERSION=$LATEST
_AWS_XRAY_DAEMON_ADDRESS=169.254.79.2
_AWS_XRAY_DAEMON_PORT=2000
AWS_XRAY_DAEMON_ADDRESS=169.254.79.2:2000
AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
_X_AMZN_TRACE_ID=Parent=41bc1aa71e1174a5
_HANDLER=my_handler
_LAMBDA_RUNTIME_LOAD_TIME=1522376103407
/var/task
/var/task/my_handler
PATH=/usr/local/bin:/usr/bin/:/bin
LANG=en_US.UTF-8
TZ=:UTC
LD_LIBRARY_PATH=/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib
_LAMBDA_CONTROL_SOCKET=15
_LAMBDA_CONSOLE_SOCKET=17
LAMBDA_TASK_ROOT=/var/task
LAMBDA_RUNTIME_DIR=/var/runtime
_LAMBDA_LOG_FD=24
_LAMBDA_SB_ID=8
_LAMBDA_SHARED_MEM_FD=12
AWS_REGION=us-east-1
AWS_DEFAULT_REGION=us-east-1
AWS_LAMBDA_LOG_GROUP_NAME=/aws/lambda/dump-go1x
AWS_LAMBDA_LOG_STREAM_NAME=2018/01/16/[$LATEST]12d47417179844e3ad55190a93a817d7
AWS_LAMBDA_FUNCTION_NAME=dump-go1x
AWS_LAMBDA_FUNCTION_MEMORY_SIZE=3008
AWS_LAMBDA_FUNCTION_VERSION=$LATEST
_AWS_XRAY_DAEMON_ADDRESS=169.254.79.2
_AWS_XRAY_DAEMON_PORT=2000
AWS_XRAY_DAEMON_ADDRESS=169.254.79.2:2000
AWS_XRAY_CONTEXT_MISSING=LOG_ERROR
_X_AMZN_TRACE_ID=Parent=41bc1aa71e1174a5
_HANDLER=my_handler
_LAMBDA_RUNTIME_LOAD_TIME=1522376103407
_LAMBDA_SERVER_PORT=60304
AWS_ACCESS_KEY=
AWS_ACCESS_KEY_ID=
AWS_SECRET_KEY=
AWS_SECRET_ACCESS_KEY=
AWS_SESSION_TOKEN=
AWS_SECURITY_TOKEN=
context.Background.WithDeadline(2018-01-12 21:16:44.121702432 +0000 UTC [2.981503691s]).WithValue(
&lambdacontext.key{},
&lambdacontext.LambdaContext{
AwsRequestID:"e1e762a8-f7dd-11e7-8572-1dc9a2c870b7",
InvokedFunctionArn:"arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:dump-go1x",
Identity:lambdacontext.CognitoIdentity{CognitoIdentityID:"", CognitoIdentityPoolID:""},
ClientContext:lambdacontext.ClientContext{Client:lambdacontext.ClientApplication{InstallationID:"", AppTitle:"", AppVersionCode:"", AppPackageName:""},
Env:map[string]string(nil),
Custom:map[string]string(nil)}
}).WithValue("x-amzn-trace-id", "Root=1-5a5925b8-30ae34971b99966e26b15b1e;Parent=06346dc778d0afed;Sampled=1")
*/
20 changes: 20 additions & 0 deletions examples/go1.x/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/go1.x/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[constraint]]
name = "github.com/aws/aws-lambda-go"
version = "1.0.1"
26 changes: 26 additions & 0 deletions examples/go1.x/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Compile with:
// docker run --rm -v "$PWD":/go/src/handler lambci/lambda:build-go1.x sh -c 'dep ensure && go build handler.go'

// Run with:
// docker run --rm -v "$PWD":/var/task lambci/lambda:go1.x handler '{"Records": []}'

package main

import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

func HandleRequest(ctx context.Context, event events.S3Event) (string, error) {
fmt.Println(ctx)

fmt.Println(event)

return "Hello World!", nil
}

func main() {
lambda.Start(HandleRequest)
}
15 changes: 15 additions & 0 deletions go1.x/build/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM lambci/lambda-base:build

ENV GOLANG_VERSION=1.9.2 \
GOPATH=/go \
PATH=/go/bin:/usr/local/go/bin:$PATH

WORKDIR /go/src/handler

RUN rm -rf /var/runtime /var/lang && \
curl https://lambci.s3.amazonaws.com/fs/go1.x.tgz | tar -zx -C / && \
curl https://dl.google.com/go/go${GOLANG_VERSION}.linux-amd64.tar.gz | tar -zx -C /usr/local && \
go get github.com/golang/dep/cmd/dep && \
go install github.com/golang/dep/cmd/dep

CMD ["dep", "ensure"]
18 changes: 18 additions & 0 deletions go1.x/run/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1
WORKDIR /go/src/github.com/lambci/docker-lambda
RUN curl -sSL -o /usr/local/bin/dep https://github.com/golang/dep/releases/download/v0.3.2/dep-linux-amd64 && chmod +x /usr/local/bin/dep
COPY aws-lambda-mock.go Gopkg.toml Gopkg.lock ./
RUN dep ensure
RUN GOARCH=amd64 GOOS=linux go build aws-lambda-mock.go


FROM lambci/lambda-base

RUN rm -rf /var/runtime /var/lang && \
curl https://lambci.s3.amazonaws.com/fs/go1.x.tgz | tar -zx -C /

COPY --from=0 /go/src/github.com/lambci/docker-lambda/aws-lambda-mock /var/runtime/aws-lambda-go

USER sbx_user1051

ENTRYPOINT ["/var/runtime/aws-lambda-go"]
15 changes: 15 additions & 0 deletions go1.x/run/Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions go1.x/run/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[constraint]]
name = "github.com/aws/aws-lambda-go"
version = "1.0.1"
Loading

0 comments on commit f94bdd5

Please sign in to comment.