Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a newer builder for golang #126

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -141,7 +141,9 @@ 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:
- With `go.mod`: `golang:1.21-bookworm`
- Without `go.mod`: `golang:1.17-buster`
- requirement: `go.mod` or `main.go`
- runtimes:
- provided.al2
Expand Down
15 changes: 13 additions & 2 deletions builders/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ type GoBuilder struct {

func NewGoBuilder(config Config) (GoBuilder, error) {
var err error
config.BuilderBuildImage, err = getBuildImage(config, "lambci/lambda:build-go1.x")
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
}
Expand Down Expand Up @@ -79,11 +84,12 @@ 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

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() {
Expand Down Expand Up @@ -111,6 +117,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
Expand Down
Loading