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

GoLang function: how to build multiple go files ? #5269

Closed
maximelebastard opened this issue Sep 6, 2020 · 3 comments · Fixed by #5262
Closed

GoLang function: how to build multiple go files ? #5269

maximelebastard opened this issue Sep 6, 2020 · 3 comments · Fixed by #5262
Assignees
Labels
bug Something isn't working functions Issues tied to the functions category

Comments

@maximelebastard
Copy link

maximelebastard commented Sep 6, 2020

Which Category is your question related to?
Functions

Amplify CLI Version
4.29.0

Provide additional details e.g. code snippets

Hi, I want to integrate a Lambda written in Go to my amplify project.
It is split in multiple files in a same package, which worked well with AWS SAM.
But it doesn't seems to be supported with amplify.

Here is the error I get when I want to build the sample code below:

amplify build function tryToCompileGo
⠙ Building resources. This may take a few minutes...# command-line-arguments
./main.go:15:2: undefined: HelloTest
✖ An error occurred when building the resources.
Error: Command failed with exit code 2: go build -o /Users/maxime/workspace/myproject/amplify/backend/function/tryToCompileGo/bin/main main.go
    at makeError (/Users/maxime/.config/yarn/global/node_modules/execa/lib/error.js:59:11)
    at Object.module.exports.sync (/Users/maxime/.config/yarn/global/node_modules/execa/index.js:188:17)
    at Object.exports.executeCommand (/Users/maxime/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/runtime.ts:24:24)
    at Object.exports.buildResourceInternal (/Users/maxime/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/runtime.ts:108:5)
    at Object.exports.buildResource (/Users/maxime/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/runtime.ts:161:3)
    at Object.build (/Users/maxime/.config/yarn/global/node_modules/amplify-go-function-runtime-provider/src/index.ts:23:23)
    at buildResource (/Users/maxime/.config/yarn/global/node_modules/amplify-provider-awscloudformation/lib/build-resources.js:49:36)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 0)
    at async Promise.all (index 0)
There was an error building the function resources

If the command triggered by amplify were

go build -o /Users/maxime/workspace/myproject/amplify/backend/function/tryToCompileGo/bin/main .

instead of

go build -o /Users/maxime/workspace/myproject/amplify/backend/function/tryToCompileGo/bin/main main.go

It would work

Code

src/hello.go

package main

func HelloTest() {
	println("Hello !")
}

src/main.go

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) {
	HelloTest()
	return fmt.Sprintf("Hello %s!", name.Name), nil
}

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

go.mod

module lambda

go 1.14

require github.com/aws/aws-lambda-go v1.19.1

tryToCompileGo-cloudformation-template.json (truncated)

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Resources": {
        "LambdaFunction": {
          "Type": "AWS::Lambda::Function",
          "Metadata": {
            "aws:asset:path": "./src",
            "aws:asset:property": "Code"
          },
          "Properties": {
            "Handler": "main",
            "FunctionName": {
                "Fn::If": [
                    "ShouldNotCreateEnvResources",
                    "tryToCompileGo",
                    {

                        "Fn::Join": [
                            "",
                            [
                                "tryToCompileGo",
                                "-",
                                {
                                    "Ref": "env"
                                }
                            ]
                        ]
                    }
                ]
            },
            "Environment": {
                "Variables" : {"ENV":{"Ref":"env"},"REGION":{"Ref":"AWS::Region"}}
            },
            "Role": { "Fn::GetAtt" : ["LambdaExecutionRole", "Arn"] },
            "Runtime": "go1.x",
            "Layers": [],
            "Timeout": "25"
          }
        },
        
}
@nikhname nikhname added functions Issues tied to the functions category question General question labels Sep 8, 2020
@nikhname nikhname added bug Something isn't working and removed question General question labels Sep 14, 2020
@jhockett
Copy link
Contributor

Thanks for the detailed bug report @maximelebastard! A fix will be available in the next release.

@maximelebastard
Copy link
Author

Hey @jhockett, a quick message to confirm it now works correctly. Thank you for the quick fix ! 👍

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working functions Issues tied to the functions category
Projects
None yet
3 participants