Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Internally, `lambda-builder` detects a given language and builds the app accordi
- dotnetcore3.1
- `go`
- default build image: `lambci/lambda:build-go1.x`
- requirement: `go.mod`
- requirement: `go.mod` or `main.go`
- runtimes:
- provided.al2
- `nodejs`
Expand Down
15 changes: 12 additions & 3 deletions builders/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func NewGoBuilder(config Config) (GoBuilder, error) {
}

func (b GoBuilder) Detect() bool {
if io.FileExistsInDirectory(b.Config.WorkingDirectory, "go.sum") {
if io.FileExistsInDirectory(b.Config.WorkingDirectory, "go.mod") {
return true
}

if io.FileExistsInDirectory(b.Config.WorkingDirectory, "main.go") {
return true
}

Expand Down Expand Up @@ -76,8 +80,13 @@ puts-step() {
}

install-gomod() {
puts-step "Downloading dependencies via go mod"
go mod download 2>&1 | indent
if [[ -f "go.mod" ]]; then
puts-step "Downloading dependencies via go mod"
go mod download 2>&1 | indent
else
puts-step "Missing go.mod, downloading dependencies via go get"
go get
fi

puts-step "Compiling via go build"
go build -o bootstrap main.go 2>&1 | indent
Expand Down
7 changes: 7 additions & 0 deletions test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ teardown_file() {
[[ "$status" -eq 0 ]]
}

@test "[build] go without go modules" {
run $LAMBDA_BUILDER_BIN build --working-directory tests/go-nomod
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]
}

@test "[build] hooks" {
run $LAMBDA_BUILDER_BIN build --working-directory tests/hooks
echo "output: $output"
Expand Down
20 changes: 20 additions & 0 deletions tests/go-nomod/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"context"
"fmt"

"github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
return fmt.Sprintf("Hello %s!", name.Name), nil
}

func main() {
lambda.Start(HandleRequest)
}
61 changes: 61 additions & 0 deletions tests/go-nomod/test.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env bats

export LAMBDA_ROLE="arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
export AWS_ACCOUNT_ID="$(aws sts get-caller-identity | jq -r ".Account")"
export LAMBDA_FUNCTION_NAME=lambda-go1x
export LAMBDA_RUNTIME=provided.al2
export LAMBDA_HANDLER=bootstrap

setup() {
aws lambda delete-function --function-name "$LAMBDA_FUNCTION_NAME" 2>/dev/null || true
aws iam detach-role-policy --role-name "$LAMBDA_FUNCTION_NAME" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null || true
aws iam delete-role --role-name "$LAMBDA_FUNCTION_NAME" 2>/dev/null || true
}

teardown() {
aws lambda delete-function --function-name "$LAMBDA_FUNCTION_NAME" 2>/dev/null || true
aws iam detach-role-policy --role-name "$LAMBDA_FUNCTION_NAME" --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole 2>/dev/null || true
aws iam delete-role --role-name "$LAMBDA_FUNCTION_NAME" 2>/dev/null || true
}

@test "aws test" {
run /bin/bash -c "lambda-builder build"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "aws iam create-role --role-name '$LAMBDA_FUNCTION_NAME' --tags 'Key=app,Value=lambda-builder' --tags 'Key=com.dokku.lambda-builder/runtime,Value=$LAMBDA_RUNTIME' --assume-role-policy-document '{\"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Principal\": {\"Service\": \"lambda.amazonaws.com\"}, \"Action\": \"sts:AssumeRole\"}]}'"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "aws iam attach-role-policy --role-name '$LAMBDA_FUNCTION_NAME' --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "sleep 10"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "aws lambda create-function --function-name '$LAMBDA_FUNCTION_NAME' --package-type Zip --tags 'app=lambda-builder,com.dokku.lambda-builder/runtime=$LAMBDA_RUNTIME' --role 'arn:aws:iam::${AWS_ACCOUNT_ID}:role/$LAMBDA_FUNCTION_NAME' --zip-file fileb://lambda.zip --runtime '$LAMBDA_RUNTIME' --handler '$LAMBDA_HANDLER'"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "sleep 10"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "aws lambda get-function --function-name '$LAMBDA_FUNCTION_NAME'"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]

run /bin/bash -c "aws lambda invoke --cli-binary-format raw-in-base64-out --function-name '$LAMBDA_FUNCTION_NAME' --payload '{\"name\": \"World\"}' response.json"
echo "output: $output"
echo "status: $status"
[[ "$status" -eq 0 ]]
}