amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

Build go function fails if there are more than main.go in src

Open nam-truong-le opened this issue 10 months ago • 8 comments

How did you install the Amplify CLI?

yarn

If applicable, what version of Node.js are you using?

20

Amplify CLI Version

12.11.1

What operating system are you using?

Ubuntu

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Describe the bug

This change here fails go function build if there are more than main.go in src.

Expected behavior

Go function should be built

Reproduction steps

  1. Create a lambda function using go
  2. in main.go use a function defined in another.go
  3. Build will fail

Project Identifier

No response

Log output

No response

Additional information

No response

Before submitting, please confirm:

  • [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • [X] I have removed any sensitive information from my code snippets and submission.

nam-truong-le avatar Apr 18 '24 13:04 nam-truong-le

Hey @nam-truong-le, thank you for reaching out. Marking as bug for further investigation.

ykethan avatar Apr 18 '24 16:04 ykethan

Hey @nam-truong-le, wanted update on this issue. With the latest update to the Lambda functions on https://github.com/aws-amplify/amplify-cli/pull/13671. You will need to upgrade the local go runtime to match the Lambda function. refer to AWS Documentation providing additional information.

On a quick test with the following steps the push did not run into an error on a new function

  1. amplify add function
  2. created a folder say test folder with test.go file
// <function-name>/src/test/test.go
package test

import (
	"fmt"
  )

func DoSomething() {
	fmt.Println("Hello World!")
  }

in main.go

// <function-name>/src/main.go
package main

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

  test "lambda/test"
)

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() {
  test.DoSomething()
  lambda.Start(HandleRequest)
}

ykethan avatar Apr 18 '24 17:04 ykethan

@ykethan which version of @aws-amplify/cli are you using?

nam-truong-le avatar Apr 18 '24 20:04 nam-truong-le

@nam-truong-le this is on the latest version of Amplify CLI: 12.11.1

ykethan avatar Apr 19 '24 14:04 ykethan

I use the same version of CLI but I get this:

# command-line-arguments
Error: ./main.go:63:6: undefined: processOrder
🛑 go failed, error message was Command failed with exit code 1: go build -o ../bin/bootstrap main.go

Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/

Session Identifier: ca285f60-6882-4563-a1ea-1ba98447a976
Error: Process completed with exit code 1.

As you can see, the build command is go build -o ../bin/bootstrap main.go and this won't work, I can repro the issue with just a simple go repo where I use another function outside of main.go.

nam-truong-le avatar Apr 28 '24 06:04 nam-truong-le

Hey @nam-truong-le, could you provide us the reproduction steps or sample snippets of the example with the file paths used to reproduce the issue?

ykethan avatar Apr 30 '24 17:04 ykethan

I am experiencing the same issue with my Go Lambda functions. In my case I have multiple files in my main package. Everything outside main.go is ignored as the command used is go build -o ../bin/bootstrap main.go.

I'm using the 12.1.1 version of the CLI on Ubuntu 20.04.6 LTS.

This are the steps taken to reproduce: 1 - amplify build function gomultiplefiles 2 - On amplify/backend/function/gomultiplefiles/src/main.go

package main

import (
	"context"
	"fmt"

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

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

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

2 - On amplify/backend/function/gomultiplefiles/src/types.go

package main

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

3 - Running amplify build function gomultiplefiles outputs the following error

# command-line-arguments
./main.go:10:46: undefined: MyEvent
🛑 go failed, error message was Command failed with exit code 1: go build -o ../bin/bootstrap main.go

If the command changed from go build -o ../bin/bootstrap main.go to go build -o ../bin/bootstrap ., it would work. This same situation is mentioned in this issue #5269 from some years ago.

jo-su avatar May 02 '24 10:05 jo-su

Hey @jo-su, thank you for the information. I was able to reproduce the issue. Marking a bug. reproduced the issue with the following test

  1. created a go function
  2. modified the function according to https://github.com/aws-amplify/amplify-cli/issues/13725#issuecomment-2090118621
  3. changes directory into the function folder to and ran go build -o bootstrap main.go which errored out with ./main.go:13:46: undefined: MyEvent. but running go build -o bootstrap . built the function.

Noticed an error Error: fork/exec /var/task/bootstrap: exec format error Runtime.InvalidEntrypoint on the Lambda function when the function was pushed with an older version of Amplify CLI. From the AWS documentation: https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html#golang-handler-naming the function will refer to function code named bootstrap, which previously was handler in go1.x runtime. ensure the go.mod and the CloudFormation template are updated to use provided.al2023.

ykethan avatar May 06 '24 22:05 ykethan