如何使用AWS CDK构建、打包和发布.NET C# Lambda函数?

0

【以下的问题经过翻译处理】 我的问题基于这篇文章。我在有一个.NET项目引用另一个项目时遇到了一个问题。

我按照以下方式添加了Bundling。

我的目标:

  • 运行命令 cdk deploy 时,自动构建Lambda代码并压缩代码。

在按照文章中的步骤操作后,我得到了以下错误。

Bundling asset BervProjectMergePDFInfraStack/PdfMerger/Code/Stage...
You can invoke the tool using the following command: dotnet-lambda
Tool 'amazon.lambda.tools' (version '5.6.4') was successfully installed.
MSBuild version 17.3.2+561848881 for .NET
  Determining projects to restore...
  Skipping project "/BervProject.MergePDF.S3/BervProject.MergePDF.S3.csproj" because it was not found.
  Skipping project "/BervProject.MergePDF.S3/BervProject.MergePDF.S3.csproj" because it was not found.
  Restored /asset-input/BervProject.MergePDF.Lambda.csproj (in 9.22 sec).
/var/lang/bin/sdk/6.0.407/Microsoft.Common.CurrentVersion.targets(2066,5): warning : The referenced project '../../../BervProject.MergePDF.S3/BervProject.MergePDF.S3.csproj' does not exist. [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Startup.cs(2,28): error CS0234: The type or namespace name 'S3' does not exist in the namespace 'BervProject.MergePDF' (are you missing an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Startup.cs(3,28): error CS0234: The type or namespace name 'S3' does not exist in the namespace 'BervProject.MergePDF' (are you missing an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Functions.cs(13,26): error CS0246: The type or namespace name 'IMerger' could not be found (are you missing a using directive or an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Functions.cs(17,26): error CS0246: The type or namespace name 'IMerger' could not be found (are you missing a using directive or an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]

Build FAILED.

/var/lang/bin/sdk/6.0.407/Microsoft.Common.CurrentVersion.targets(2066,5): warning : The referenced project '../../../BervProject.MergePDF.S3/BervProject.MergePDF.S3.csproj' does not exist. [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Startup.cs(2,28): error CS0234: The type or namespace name 'S3' does not exist in the namespace 'BervProject.MergePDF' (are you missing an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Startup.cs(3,28): error CS0234: The type or namespace name 'S3' does not exist in the namespace 'BervProject.MergePDF' (are you missing an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Functions.cs(13,26): error CS0246: The type or namespace name 'IMerger' could not be found (are you missing a using directive or an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
/asset-input/Functions.cs(17,26): error CS0246: The type or namespace name 'IMerger' could not be found (are you missing a using directive or an assembly reference?) [/asset-input/BervProject.MergePDF.Lambda.csproj]
    1 Warning(s)
    4 Error(s)

Time Elapsed 00:00:14.36
Unhandled exception. System.Exception: Failed to bundle asset BervProjectMergePDFInfraStack/PdfMerger/Code/Stage, bundle output is located at D:\Projects\Github\MergePDFOnline\cdk.out\asset.8002390a958718ff5743fdca447ec443297b0f1ed8dc390b1c49e2e8cbb60427-error: Error: docker exited with status 1
   at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
   at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
   at Amazon.JSII.Runtime.Services.Client.Send[TRequest,TResponse](TRequest requestObject)
   at Amazon.JSII.Runtime.Services.Client.Create(CreateRequest request)
   at Amazon.JSII.Runtime.Services.Client.Create(String fullyQualifiedName, Object[] arguments, Override[] overrides, String[] interfaces)
   at Amazon.JSII.Runtime.Deputy.DeputyBase..ctor(DeputyProps props)
   at Constructs.Construct..ctor(DeputyProps props)
   at Amazon.CDK.Resource..ctor(DeputyProps props)
   at Amazon.CDK.AWS.Lambda.FunctionBase..ctor(DeputyProps props)
   at Amazon.CDK.AWS.Lambda.Function..ctor(Construct scope, String id, IFunctionProps props)
   at BervProject.MergePDF.Infra.BervProjectMergePDFInfraStack..ctor(Construct scope, String id, IStackProps props) in D:\Projects\Github\MergePDFOnline\BervProject.MergePDF.Infra\BervProjectMergePDFInfraStack.cs:line 32
   at BervProject.MergePDF.Infra.Program.Main(String[] args) in D:\Projec

我认为构建过程未在Lambda项目目录中运行,因此过程失败。

是否有另一种解决方案,至少可以确保Lambda将被正确部署?我当前状态是Lambda还没有任何程序集。如果需要更多代码细节,请点击此处

profile picture
EXPERTE
gefragt vor 5 Monaten73 Aufrufe
1 Antwort
0

【以下的回答经过翻译处理】 在定义Code.FromAsset对象时,确保引用包含所有依赖项(或完整解决方案,如果有的话)的正确路径。

如果仅指向单个项目的路径,CDK不会自动复制依赖项,因此构建将失败。

您可以指定捆绑容器的WorkingDirectory(请参见BundlingOptions),指向您的主Lambda函数项目,以简化构建命令。

profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen