Comments (7)
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.
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.
@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.
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
-
sam init --runtime provided.al2 --app-template hello-world --name func
(and selectgo
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
-
sam build --use-container
works fine!
hello-world with provided.al2023
sam init --runtime provided.al2023 --app-template hello-world --name func
(and selectgo
in runtime the prompt)
Produces template:Tree:Metadata: BuildMethod: go1.x Properties: CodeUri: hello-world/ Handler: bootstrap Runtime: provided.al2023 Architectures: - x86_64
func ├── hello-world │ ├── go.mod │ ├── go.sum │ ├── main.go ├── samconfig.toml └── template.yaml
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:
- 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)
- Run
go mod vendor
beforesam build
. This seems to speed up the build time as well as fixing the not foundgit
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.
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.
Patch is released in v1.111.0. Closing
from aws-sam-cli.
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)
- Bug: TITLE HOT 2
- Feature request: TITLE HOT 3
- Sam Build fails when publishing against dotnet8 runtime HOT 2
- Bug: sam local start-api - KeyError: 'content-type' (WebAdapter + Nextjs) HOT 2
- Sam build failed when trying to create a S3 Express One Zone bucket HOT 6
- Bug: Layers fail to deploy correctly HOT 2
- Bug: Updating the CloudFormation stack via "sam deploy" gives an error "Update of resource type is not permitted" HOT 2
- Bug: sam build - TypeError HOT 5
- Bug: sam validate not support dotnet8 HOT 4
- local invoke options for users without root Docker access? HOT 1
- Support running lambdas build from a docker image HOT 1
- Feature request: A way to convert Jenkin YML into SAM yml HOT 1
- المملكة HOT 2
- Bug: sam local invoke - ReadTimeout HOT 3
- Feature request: TITLE HOT 2
- Bug: sam sync crashes when a "prepare" statement is in package.json HOT 6
- Verifying the integrity of the AWS SAM CLI installer HOT 3
- feat: Shell completion HOT 1
- How to report bug in StepFunctionsLocal.jar ? HOT 1
- Bug: sam build - AttributeError HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-sam-cli.