How to build, package, and publish .NET C# Lambda functions with the AWS CDK?

0

My question is based on this article. I found an issue when I have a .NET project referencing another project.

I added the Bundling Options like this.

            var buildOption = new BundlingOptions()
            {
                Image = Runtime.DOTNET_6.BundlingImage,
                User = "root",
                OutputType = BundlingOutput.ARCHIVED,
                Command = new string[] {
                   "/bin/sh",
                    "-c",
                    " dotnet tool install -g Amazon.Lambda.Tools"+
                    " && dotnet build"+
                    " && dotnet lambda package --output-package /asset-output/function.zip"
                    }
            };

My goal:

  • Automatically build the lambda code and zip the code when running: cdk deploy.

After I followed the article, I got these errors.

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:\Projects\Github\MergePDFOnline\BervProject.MergePDF.Infra\Program.cs:line 13

I think the build process is not running in the lambda project directory, so the process failed.

Do you have another solution or alternative at least I can ensure the lambda will be deployed correctly. My current condition is the Lambda doesn't have any assemblies yet.


If you need more details about my code is here.

2개 답변
2
수락된 답변

When defining your Code.FromAsset object, make sure you reference the correct path containing all the dependencies (or the complete solution, if you have one).

If you only point to your single project's path, CDK won't copy the dependencies automatically, so the build will fail.

You can then specify the WorkingDirectory for your bundling container (see BundlingOptions) pointing to your main Lambda function project to simplify the build commands.

profile pictureAWS
답변함 일 년 전
profile picture
전문가
검토됨 2일 전
  • Thank you! This is what I'm looking for. I just realized I only define the project path without the dependencies.

0

Read the Blog Post Build, package, and publish .NET C# Lambda functions with the AWS CDK This post explore how to streamline building, packaging, and publishing .NET Lambda functions using AWS CDK.

AWS
답변함 10달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인