Search code examples
amazon-web-servicesdockeraws-lambdaaws-cdkaws-code-deploy

Aws CodeDeploy CDK Lambda Java in Docker build fails with return code 125


I have created a multi modal maven project which has 2 modules:

  1. LambdaService
  2. CDK infrastructure

CDK infrastructure has a self deploying pipeline and the pipeline has a stage which deploys lambdaService. This is how I created the Lambda Function in CDK:

    List<String> serviceInstructions = Arrays.asList(
        "/bin/sh",
        "-c",
        "mvn clean install " +
            "&& cp /asset-input/target/cloud.jar /asset-output/");

    BundlingOptions.Builder builderOptions = BundlingOptions.builder()
        .command(serviceInstructions)
        .image(Runtime.JAVA_11.getBundlingImage())
        .volumes(singletonList(
            // Mount local .m2 repo to avoid download all the dependencies again inside the container
            DockerVolume.builder()
                .hostPath(System.getProperty("user.home") + "/.m2/")
                .containerPath("/root/.m2/")
                .build()
        ))
        .user("root")
        .outputType(BundlingOutput.ARCHIVED);

    Function function = new Function(this, "TestLambda", FunctionProps.builder()
        .runtime(Runtime.JAVA_11)
        .code(Code.fromAsset( getServiceModuleUrl(), AssetOptions.builder()
            .bundling(builderOptions.build()).build()))
        .handler("com.potatoes.company.lambda.TestLambda")
        .build()); 
  private String getServiceModuleUrl(){
    try {
      return Path.of(this.getClass().getClassLoader().getResource("").toURI()).getParent().getParent().getParent().resolve("service").toString();
    }catch (Exception ex){
      return "../service/";
    }
  }

When I run cdk synth from my local computer(Windows); It produces all the templates without a problem. When I run cdk synth from cygwin(linux); It produces all the templates without a problem as in the Windows build.

But when I push my code to github and it triggers the Pipeline which triggers Code deploy build with cdk synth command, it fails with :

Caused by: org.apache.maven.plugin.MojoExecutionException: An exception occured while executing the Java class. Failed to bundle asset CloudPipeline/CloudStage/CloudStack/TestLambda/Code/Stage, bundle output is located at /codebuild/output/src946627024/src/cdk.out/asset.2ace4f1767158022578fec53202724b1f54fbe7cef310b13aa0b9752a0c73f50-error: Error: docker exited with status 125
Error: Failed to bundle asset CloudPipeline/CloudStage/CloudStack/TestLambda/Code/Stage, bundle output is located at /codebuild/output/src946627024/src/cdk.out/asset.2ace4f1767158022578fec53202724b1f54fbe7cef310b13aa0b9752a0c73f50-error: Error: docker exited with status 125

Any idea why do I get different result in codeDeploy?


Solution

  • I have found the solution at pipeline creatio. My pipeline code was:

    CodePipeline pipeline = CodePipeline.Builder.create(this, "pipeline")
        .pipelineName("PotatoesCloudPipeline")
        .synth(ShellStep.Builder.create("Synth")
            .input(CodePipelineSource.gitHub("PotatoesCompany/potatoesCloud", "master", GitHubSourceOptions.builder()
                .trigger(GitHubTrigger.WEBHOOK)
                .authentication(SecretValue.plainText("NiceSecretAuth")).build()))
            .commands(Arrays.asList("npm install -g aws-cdk", "cdk synth"))
            .build())
        .build();
    
    pipeline.addStage(new PotatoesCloudStage(this, "PotatoesCloudStage", getStageProps()));
    

    Then I added: .dockerEnabledForSynth(true) and it worked.

    CodePipeline pipeline = CodePipeline.Builder.create(this, "pipeline")
        .pipelineName("PotatoesCloudPipeline")
        .synth(ShellStep.Builder.create("Synth")
            .input(CodePipelineSource.gitHub("PotatoesCompany/potatoesCloud", "master", GitHubSourceOptions.builder()
                .trigger(GitHubTrigger.WEBHOOK)
                .authentication(SecretValue.plainText("NiceSecretAuth")).build()))
            .commands(Arrays.asList("npm install -g aws-cdk", "cdk synth"))
            .build())
        .dockerEnabledForSynth(true)
        .build();
    
    pipeline.addStage(new PotatoesCloudStage(this, "PotatoesCloudStage", getStageProps()));