From 007fdba77d535ebfd5154ba8ef1e2a3e6b3d5ee4 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 5 Dec 2023 14:16:12 -0500 Subject: [PATCH 1/4] fix: use a newer builder for golang Without this, using newer libraries fails due to the old builder having a now unsupported golang. --- builders/go.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/builders/go.go b/builders/go.go index 77e2c19..9633dc8 100644 --- a/builders/go.go +++ b/builders/go.go @@ -8,7 +8,7 @@ type GoBuilder struct { func NewGoBuilder(config Config) (GoBuilder, error) { var err error - config.BuilderBuildImage, err = getBuildImage(config, "lambci/lambda:build-go1.x") + config.BuilderBuildImage, err = getBuildImage(config, "golang:1.21-bookworm") if err != nil { return GoBuilder{}, err } @@ -83,7 +83,7 @@ install-gomod() { fi puts-step "Compiling via go build" - go build -o bootstrap main.go 2>&1 | indent + CGO_ENABLED=0 go build -o bootstrap main.go 2>&1 | indent } hook-pre-compile() { @@ -111,6 +111,11 @@ hook-package() { return fi + if ! command -v zip >/dev/null 2>&1; then + puts-step "Installing zip dependency for packaging" + apt update && apt install -y --no-install-recommends zip + fi + puts-step "Creating package at lambda.zip" zip -q -r lambda.zip bootstrap mv lambda.zip /var/task/lambda.zip From f848c33fd3f4fe1a60ad44024fca4f2c80781695 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Tue, 5 Dec 2023 14:17:10 -0500 Subject: [PATCH 2/4] docs: update golang version in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4924b42..11278ec 100644 --- a/README.md +++ b/README.md @@ -141,7 +141,7 @@ Internally, `lambda-builder` detects a given language and builds the app accordi - dotnet6 - dotnetcore3.1 - `go` - - default build image: `lambci/lambda:build-go1.x` + - default build image: `golang:1.21-bookworm` - requirement: `go.mod` or `main.go` - runtimes: - provided.al2 From e4e0a79fb993655d6af0200fa258f989861b7b3b Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Dec 2023 00:58:29 -0500 Subject: [PATCH 3/4] fix: add support for projects without a go.mod --- README.md | 8 +++++--- builders/go.go | 7 ++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 11278ec..6f1c62d 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ lambda-builder build --generate-image --builder dotnet #### Building an image -A docker image can be produced from the generated artifact by specifying the `--generate-image` flag. This also allows for multiple `--label` flags as well as specifying a single image tag via either `-t` or `--tag`: +A docker image can be produced from the generated artifact by specifying the `--generate-image` flag. This also allows for multiple `--label` flags as well as specifying a single image tag via either `-t` or `--tag`: ```shell # will write a lambda.zip in the specified path @@ -111,7 +111,7 @@ aws lambda invoke --endpoint http://localhost:9001 --no-sign-request --function- curl -d '{}' http://localhost:9001/2015-03-31/functions/function.handler/invocations # the function can also be invoked directly from a container if desired -docker run --rm "lambda-builder/$APP:latest" function.handler '{"name": "World"}' +docker run --rm "lambda-builder/$APP:latest" function.handler '{"name": "World"}' ``` #### Generating a Procfile @@ -141,7 +141,9 @@ Internally, `lambda-builder` detects a given language and builds the app accordi - dotnet6 - dotnetcore3.1 - `go` - - default build image: `golang:1.21-bookworm` + - default build image: + - With `go.mod`: `golang:1.21-bookworm` + - Without `go.mod`: `golang:1.17-buster` - requirement: `go.mod` or `main.go` - runtimes: - provided.al2 diff --git a/builders/go.go b/builders/go.go index 9633dc8..5366390 100644 --- a/builders/go.go +++ b/builders/go.go @@ -8,7 +8,12 @@ type GoBuilder struct { func NewGoBuilder(config Config) (GoBuilder, error) { var err error - config.BuilderBuildImage, err = getBuildImage(config, "golang:1.21-bookworm") + defaultBuilder := "golang:1.21-bookworm" + if !io.FileExistsInDirectory(config.WorkingDirectory, "go.mod") { + defaultBuilder = "golang:1.17-bullseye" + } + + config.BuilderBuildImage, err = getBuildImage(config, defaultBuilder) if err != nil { return GoBuilder{}, err } From c1e034975670218fe33f761d29a93f1ace0d8254 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 7 Dec 2023 01:15:26 -0500 Subject: [PATCH 4/4] fix: disable GO111MODULE when running without a go.mod file --- builders/go.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builders/go.go b/builders/go.go index 5366390..7d5e321 100644 --- a/builders/go.go +++ b/builders/go.go @@ -84,6 +84,7 @@ install-gomod() { go mod download 2>&1 | indent else puts-step "Missing go.mod, downloading dependencies via go get" + go env -w GO111MODULE=off go get fi