Skip to content

Commit 8168eb9

Browse files
author
JayJamieson
committed
added readme,basic handlers and build scripts
1 parent 76ec9d1 commit 8168eb9

File tree

9 files changed

+130
-1
lines changed

9 files changed

+130
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
bin/main
17+
bin/main.local
18+
function.zip

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM amazonlinux:latest
2+
3+
RUN yum install -y tar xz gzip gcc
4+
5+
RUN curl -L -o golang.tar.gz https://go.dev/dl/go1.18.3.linux-amd64.tar.gz
6+
7+
RUN rm -rf /usr/local/go && tar -C /usr/local -xzf golang.tar.gz
8+
9+
WORKDIR /project
10+
11+
COPY go.mod go.sum ./
12+
13+
RUN /usr/local/go/bin/go mod download -x
14+
15+
ENV PATH="${PATH}:/usr/local/go/bin"

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,26 @@
1-
# go-lambda
1+
# go-lambda
2+
3+
## Requirements
4+
5+
- go 1.18
6+
- zip
7+
- docker
8+
- terraform
9+
10+
## Setup (optional)
11+
12+
Although not require specifically to run go in lambdas, can be useful to avoid compatibility issues
13+
when using cgo bindings or third party libraries using cgo such as sqlite3.
14+
15+
- build docker container for building lambda function binary fully compatible with aws lambda runtime
16+
- `docker build -t lambda-build .`
17+
18+
## Build
19+
20+
- `go build -o ./bin/main.local ./cmd/handler/main.go` for locally running lambda handler
21+
- `go build -o ./bin/main ./main.go` for lambda binary. Can be run locally if `_LAMBDA_SERVER_PORT` and `AWS_LAMBDA_RUNTIME_API` are defined
22+
but this is not tested and behaviour is undefined
23+
24+
## Deploy
25+
26+
- `aws lambda update-function-code --function-name go-lambda --zip-file fileb://function.zip`

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
docker run -it -v $(pwd):/project lambda-build go build -o ./bin/main.local ./cmd/handler/main.go
4+
docker run -it -v $(pwd):/project lambda-build go build -o ./bin/main ./main.go
5+
6+
rm function.zip
7+
8+
zip -j function.zip ./bin/main

cmd/handler/main.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"time"
8+
9+
"github.com/JayJamieson/go-lambda/handler"
10+
)
11+
12+
func main() {
13+
//this lambda setup is used for testing/running locally without deployment
14+
//if specific events are required we can read in json event samples or
15+
//command line flags to help build the events
16+
17+
lambdaMaxRuntime := time.Now().Add(15 * time.Minute)
18+
19+
ctx, cancel := context.WithDeadline(context.Background(), lambdaMaxRuntime)
20+
defer cancel()
21+
22+
handler := handler.NewHandler()
23+
24+
err := handler(ctx)
25+
26+
if err != nil {
27+
log.Fatal(err.Error())
28+
os.Exit(0)
29+
}
30+
31+
os.Exit(0)
32+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/JayJamieson/go-lambda
2+
3+
go 1.18
4+
5+
require (
6+
github.com/aws/aws-lambda-go v1.32.0
7+
github.com/aws/aws-sdk-go-v2 v1.16.5
8+
github.com/aws/aws-sdk-go-v2/config v1.15.10
9+
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.11
10+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/aws/aws-lambda-go v1.32.0 h1:i8MflawW1hoyYp85GMH7LhvAs4cqzL7LOS6fSv8l2KM=
2+
github.com/aws/aws-lambda-go v1.32.0/go.mod h1:IF5Q7wj4VyZyUFnZ54IQqeWtctHQ9tz+KhcbDenr220=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
6+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=

handler/handler.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package handler
2+
3+
import (
4+
"context"
5+
"log"
6+
)
7+
8+
func NewHandler() func(ctx context.Context) error {
9+
// similar to http middlewares, initialize dependencies here and use in
10+
// returned handler function.
11+
return func(ctx context.Context) error {
12+
log.Print("Running lambda")
13+
defer log.Print("Lambda complete")
14+
return nil
15+
}
16+
}

main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"github.com/JayJamieson/go-lambda/handler"
5+
"github.com/aws/aws-lambda-go/lambda"
6+
)
7+
8+
func main() {
9+
//https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html
10+
//NewHandler returns handler function this is useful for passing
11+
//in environment configurations and dependencies
12+
handler := handler.NewHandler()
13+
lambda.Start(handler)
14+
}

0 commit comments

Comments
 (0)