Java - Wrong build layer and function order
Description
Building layers is a great feature, but it's happening after function build. For compiled languages like Java, it requires us to build layer fist to use it as reference on pom.xml, so it would be available as maven dependency.
Steps to reproduce
- Clone this sample repo that I've created to reproduce it easily
- Try to build it using
sam build
OR
- Create a java8 project (it can be
HelloWorldFunctiontemplate) - Add a new maven module to be your
sample-layerwith some dummy code there - Add
sample-layerto your template.yaml
....
Resources:
SampleLayer:
Type: AWS::Serverless::LayerVersion
Properties:
LayerName: sample-layer
Description: "Sample layer description"
ContentUri: sample-layer
CompatibleRuntimes:
- java8
LicenseInfo: 'MIT'
RetentionPolicy: Retain
Metadata:
BuildMethod: java8
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: HelloWorldFunction
Handler: helloworld.App::handleRequest
Runtime: java8
MemorySize: 512
Layers:
- !Ref SampleLayer
....
....
- In order to use it, declare
sample-layeras a dependency onHelloWorldFunction/pom.xml
<dependencies>
....
<dependency>
<groupId>helloworld</groupId>
<artifactId>sample-layer</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
- In your
helloworld.App(lambda code), import dummy code and call it - Try to build using
sam build
Observed result
With no --debug (it's easier to see the wrong order)
$ sam build
Building function 'HelloWorldFunction'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Build Failed
Error: JavaMavenWorkflow:MavenBuild - Maven Failed: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.054 s
[INFO] Finished at: 2020-06-10T18:55:32-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
With --debug
$ sam build --debug
Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
'build' command is called
No Parameters detected in the template
3 resources found in the template
Found Serverless function with name='HelloWorldFunction' and CodeUri='HelloWorldFunction'
No Parameters detected in the template
Building function 'HelloWorldFunction'
Looking for a supported build workflow in following directories: ['C:\\Users\\tomasg\\workspace\\myidentity\\sam-app-with-layers\\HelloWorldFunction', 'C:\\Users\\tomasg\\workspace\\myidentity\\sam-app-with-layers']
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', 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 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
Found workflow 'JavaMavenWorkflow' to support capabilities 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Running workflow 'JavaMavenWorkflow'
Running JavaMavenWorkflow:CopySource
JavaMavenWorkflow:CopySource succeeded
Running JavaMavenWorkflow:MavenBuild
Maven logs: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.048 s
[INFO] Finished at: 2020-06-10T18:57:24-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
JavaMavenWorkflow:MavenBuild failed
Traceback (most recent call last):
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\actions.py", line 36, in execute
self.subprocess_maven.build(self.scratch_dir)
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\maven.py", line 34, in build
raise MavenExecutionError(message=stdout.decode("utf8").strip())
aws_lambda_builders.workflows.java_maven.maven.MavenExecutionError: Maven Failed: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.048 s
[INFO] Finished at: 2020-06-10T18:57:24-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflow.py", line 269, in run
action.execute()
File "C:\Program Files\Amazon\AWSSAMCLI\runtime\lib\site-packages\aws_lambda_builders\workflows\java_maven\actions.py", line 38, in execute
raise ActionFailedError(str(ex))
aws_lambda_builders.actions.ActionFailedError: Maven Failed: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.048 s
[INFO] Finished at: 2020-06-10T18:57:24-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Build Failed
Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 3112, 'exitReason': 'WorkflowFailedError', 'exitCode': 1, 'requestId': '982acee1-7df0-4476-b348-aa592cc8b01b', 'installationId': 'd035c69e-ae0b-4f22-b7b7-6a39610f7b37', 'sessionId': '41c5ecc4-57d7-4833-954f-e0cfb5567183', 'executionEnvironment': 'CLI', 'pyversion': '3.7.6', 'samcliVersion': '0.52.0'}}]}
HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: JavaMavenWorkflow:MavenBuild - Maven Failed: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.048 s
[INFO] Finished at: 2020-06-10T18:57:24-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
Workaround
- Build
SampleLayerfirst
$ sam build SampleLayer
Building layer 'SampleLayer'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Running JavaMavenWorkflow:MavenCopyDependency
Running JavaMavenWorkflow:MavenCopyArtifacts
Build Succeeded
Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
- Build entire project
$ sam build
Building function 'HelloWorldFunction'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Running JavaMavenWorkflow:MavenCopyDependency
Running JavaMavenWorkflow:MavenCopyArtifacts
Building layer 'SampleLayer'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Running JavaMavenWorkflow:MavenCopyDependency
Running JavaMavenWorkflow:MavenCopyArtifacts
Build Succeeded
Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
Expected result
Since Layer code is a dependency for Function, I do expect it be builded before.
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
- OS: Windows 10
sam --version: SAM CLI, version 0.52.0
I have created a sample project be easier to reproduce this issue here.
ohh this is a miss on my end here: https://github.com/awslabs/aws-sam-cli/blob/develop/samcli/lib/build/app_builder.py#L118-L126
And now since you have mentioned it, I don't see a reason of building functions before layers in any case. I'll try to make the code change before next release.
@gustahrodrigues I don't think this will work like you expect it too. We do not upload the artifacts from build to a local maven repo (that I know of). If we do, this would then only work on your local machine and would break for any customers doing builds through --use-container. Layers are meant to be their own unit, doing this seems to break that assumption. With Layers, you can expect the files to be on the classpath in lambda but that is it. Why do you need to build your function with the layer in this way?
This could be a gap in my understand or me applying concepts incorrectly from interpreted languages. Did the PR you posted actually solve this for you?
@jfuss JavaMavenWorkflow is performing an mvn clean install, which means that built artifact would be available on my local .m2/repository.
My PR fixed the issue for me:
$ sam build
Building function 'HelloWorldFunction'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Build Failed
Error: JavaMavenWorkflow:MavenBuild - Maven Failed: [INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< helloworld:HelloWorld >------------------------
[INFO] Building A sample Hello World created for SAM CLI. 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for helloworld:sample-layer:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.028 s
[INFO] Finished at: 2020-06-12T14:28:34-03:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project HelloWorld: Could not resolve dependencies for project helloworld:HelloWorld:jar:1.0: Could not find artifact helloworld:sample-layer:jar:1.0-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
With my fix:
$ samdev build
Building layer 'SampleLayer'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Running JavaMavenWorkflow:MavenCopyDependency
Running JavaMavenWorkflow:MavenCopyArtifacts
Building function 'HelloWorldFunction'
Running JavaMavenWorkflow:CopySource
Running JavaMavenWorkflow:MavenBuild
Running JavaMavenWorkflow:MavenCopyDependency
Running JavaMavenWorkflow:MavenCopyArtifacts
Build Succeeded
Built Artifacts : .aws-sam\build
Built Template : .aws-sam\build\template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
I'm not using --use-container option. However, you're right, since each build would be done in a brand-new container.
Is there an option to share my local .m2/repository with build container (maybe a docker volume)? If so, it would be good also to speed up build process inside container, since for each build, maven will unnecessarily download all dependencies again.
There is currently no way to share anything with the build container sam build creates during build.
I still don't understand why you need to claim this as a dependency, as it leads to other problems: https://github.com/awslabs/aws-sam-cli/issues/2047
Hi @gustahrodrigues I think the issue is still relevant would you look at the reviewed PR or maybe consider drafting a new one based on our newest changes? If not please also let us know if you want us to take over and work on another PR for you!
Hi @gustahrodrigues I think the issue is still relevant would you look at the reviewed PR or maybe consider drafting a new one based on our newest changes? If not please also let us know if you want us to take over and work on another PR for you!
You guys can take over. I'm not working with SAM for a while so I'm out of context
This behavior change is implemented with SAM CLI v1.38.0, resolving this issue.