Giter VIP home page Giter VIP logo

Comments (7)

mndeveci avatar mndeveci commented on May 25, 2024

Hi there,

I assume you have more than one lambda function inside cmd folder, since this works with single Go lambda functions. I will tag this as a bug and we will work on this.

In the meantime, you can workaround issue with following steps;

  • Update your template to point to the folder where you have go.mod file.
  • Setup custom build using Makefile to build function inside the container;
build-HelloWorldFunction:
	go build -o bootstrap cmd/helloWorld/main.go
	cp bootstrap $(ARTIFACTS_DIR)

Here was my folder structure and template;

.
├── hello-world
│   ├── Makefile
│   ├── cmd
│   │   └── helloWorld
│   │       └── main.go
│   ├── go.mod
│   └── go.sum
└── template.yaml
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: makefile
    Properties:
      CodeUri: hello-world
      Handler: cmd/helloWorld
      Runtime: provided.al2

from aws-sam-cli.

michal-sa avatar michal-sa commented on May 25, 2024

Thanks for fast respose @mndeveci. I actually only have one function inside the cmd folder where I'm running the sam build --use-container command. My folder structure looks like this:

├── func
│   ├── cmd
│   │   └── func
│   │       └── main.go
│   ├── go.mod
│   ├── go.sum
|   └── template.yaml

I tried to use the same as in your hello world example (without the Makefile), but that didn't work for me either with the same errors. Do you have a working template/structure that works inside the container?

from aws-sam-cli.

mndeveci avatar mndeveci commented on May 25, 2024

@michal-sa if you have single lambda function, then you can move your main.go file under func and it should work in that case. Your folder structure will look like;

├── func
│   ├── main.go
│   ├── go.mod
│   ├── go.sum
└── template.yaml

and your function resource should look like this;

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Metadata:
      BuildMethod: go1.x
    Properties:
      CodeUri: func/
      Handler: bootstrap
      Runtime: provided.al2

Please let us know if this does work for your case

from aws-sam-cli.

michal-sa avatar michal-sa commented on May 25, 2024

Thanks for detailed example @mndeveci.

TL;DR

I managed to get the build inside of container to work (with some workarounds especially for the provided.al2023 runtime). Will need to perform some more tests the next week to see if the binaries work during runtime as well.


I have now tested the structure you suggested using sam init (to make the setup and reproduction faster) and here are my current findings:

hello-world with provided.al2

  1. sam init --runtime provided.al2 --app-template hello-world --name func (and select go in runtime the prompt)
    Produces template:

    Metadata:
      BuildMethod: go1.x
    Properties:
      CodeUri: hello-world/
      Handler: bootstrap
      Runtime: provided.al2
      Architectures:
        - x86_64

    Tree:

    func
    ├── hello-world
    │   ├── go.mod
    │   ├── go.sum
    │   ├── main.go   
    ├── samconfig.toml
    └── template.yaml
    
  2. sam build --use-container works fine!

hello-world with provided.al2023

  1. sam init --runtime provided.al2023 --app-template hello-world --name func (and select go in runtime the prompt)
    Produces template:
    Metadata:
     BuildMethod: go1.x
    Properties:
     CodeUri: hello-world/
     Handler: bootstrap
     Runtime: provided.al2023
     Architectures:
       - x86_64
    Tree:
    func
    ├── hello-world
    │   ├── go.mod
    │   ├── go.sum
    │   ├── main.go   
    ├── samconfig.toml
    └── template.yaml
    
  2. sam build --use-container fails with:
    Build Failed
    Workflow GoModulesBuilder does not support value "False" for building in source. Using default value "True".
     Running GoModulesBuilder:Build
    Error: GoModulesBuilder:Build - Builder Failed: go: github.com/aws/[email protected]: git init --bare in 
    /root/go/pkg/mod/cache/vcs/3bc91b50dfbdd43d74dbeb370316faf0b6729c7e2769b09aeafd7c167e3f6001: exec: "git": 
    executable file not found in $PATH
    

Fixing hello-world template

I noticed that the following steps help the container build while using the provided.al2023 runtime:

  1. Bump the go.mod version to at least 1.18 (due to some outdated dependencies giving the following errors:
    # github.com/aws/aws-lambda-go/lambda
    embedding non-interface type func(context.Context, TIn) (TOut, error) requires go1.18 or later (-lang was set to go1.16; check go.mod)
    
  2. Run go mod vendor before sam build. This seems to speed up the build time as well as fixing the not found git errors. Not sure if this is a good idea as the vendor and download of dependencies will be done on another OS than the docker/lambda runtime (if that matters for go, I'm no golang expert).

Own go func

The template and function I try to get to work is more complex with e.g. refs to folders outside of the current function directory (which of course won't work well while Docker is mounting only the function folder). The debugging is a little hard, especially whilie using the provided.al2 runtime

provided.al2

After running the build command it gets stuck for a longer while (~10 minutes) after:

Running workflow 'GoModulesBuilder'
 Running GoModulesBuilder:Build

it does show helpful (debug) error messages like e.g.:

2024-02-04 12:47:18,213 | Mounting <CODE_URI_FULL_PATH> as /tmp/samcli/source:ro,delegated, inside runtime container                                                    
Using the request object from command line argument
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'NodejsNpmEsbuildBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm-esbuild', application_framework=None)'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'RustCargoLambdaBuilder' with capability 'Capability(language='rust', dependency_manager='cargo', application_framework=None)'
Found workflow 'GoModulesBuilder' to support capabilities 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Workflow GoModulesBuilder does not support value "False" for building in source. Using default value "True".
Running workflow 'GoModulesBuilder'

After 10 minutes:

..... replacement directory ../../common does not exist
2024-02-04 12:57:13,459 | Build inside container returned response {"jsonrpc": "2.0", "id": 1, "error": {"code": 400, "message": "GoModulesBuilder:Build - Builder Failed: stat /tmp/samcli/source/bootstrap: directory not    
found"}}

Of course these build bugs can probably be caught faster running a dry run build outside of docker too.

provided.al2023

This runtime still seems to fail faster mainly due to the git errors again and the go mod vendor method seems to fix the problems as well.

from aws-sam-cli.

michal-sa avatar michal-sa commented on May 25, 2024

I finally got some time to try out a more complex project structure containing references outside of the SAM container mount point.

Had to create some pre processing scripts copying the dependent directories into the SAM CodeUri folder, replacing all go.mod replace directives to be relative the mount point and finally running go mod vendor inside all of the folders containing a go.mod file.

After doing this the lambda built inside the container without errors and worked fine during runtime as well. Think this issue could be closed as the build inside container does work (although with some workarounds).

from aws-sam-cli.

github-actions avatar github-actions commented on May 25, 2024

Patch is released in v1.111.0. Closing

from aws-sam-cli.

fynnfluegge avatar fynnfluegge commented on May 25, 2024

Not able to get this running, going to use prebuilt .aws-sam directory and mount it with docker-compose. This works fine.

from aws-sam-cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.