Amplify-cli: GoLang function: how to build multiple go files ?

Created on 6 Sep 2020  ·  2Comments  ·  Source: aws-amplify/amplify-cli

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"
          }
        },

}
bug functions

Most helpful comment

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

All 2 comments

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

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

Was this page helpful?
0 / 5 - 0 ratings