Docker 构建中的 Aws CodeDeploy CDK Lambda Java 失败,代码为 return 125

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

我创建了一个包含 2 个模块的多模式 Maven 项目:

  1. Lambda 服务
  2. CDK 基础设施

CDK 基础设施有一个自部署管道,该管道有一个部署 lambdaService 的阶段。这就是我在 CDK 中创建 Lambda 函数的方式:

    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/";
    }
  }

当我从本地计算机 运行 cdk synth(Windows);它可以毫无问题地生成所有模板。当我从 cygwin(linux); 运行 cdk synth;与 Windows 构建中一样,它可以毫无问题地生成所有模板。

但是当我将我的代码推送到 github 并且它触发了使用 cdk synth 命令触发代码部署构建的管道时,它失败了:

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

知道为什么我在 codeDeploy 中得到不同的结果吗?

我在管道创建中找到了解决方案。我的管道代码是:

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()));

然后我添加了:.dockerEnabledForSynth(true) 并且成功了。

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()));