By using AWS re:Post, you agree to the AWS re:Post Terms of Use

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 Answers
2
Accepted Answer

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
answered 2 years ago
profile picture
EXPERT
reviewed 7 months ago
  • Thank you! This is what I'm looking for. I just realized I only define the project path without the dependencies.

  • Can you expand your answer with code snippets? I changed the path to the .sln folder, but then I got this error:

    Unhandled exception. System.Exception: EBUSY: resource busy or locked, open 'C:\Users\jdiniz\source\repos\Trashbin\.vs\Trashbin\FileContentIndex\a1cb433f-f318-4424-8af1-5f02a32a0e30.vsidx'
       at Amazon.JSII.Runtime.Services.Client.TryDeserialize[TResponse](String responseJson)
       at Amazon.JSII.Runtime.Services.Client.ReceiveResponse[TResponse]()
    

    I tried to specify Exclude for .vs but it does not work.

    After I close Visual Studio, I get another error:

    MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
    Unhandled exception. System.Exception: Failed to bundle asset MyStack/MyTask/Code/Stage, bundle output is located at C:\Users\jdiniz\source\repos\Trashbin\cdk.out\asset.7b0ab255137ca264b0e1942636255964ac4d7b7791afa0f03ba0190b69b4971e-error: Error: docker exited with status 1
    --> Command: docker run --rm -u root -v "C:\\Users\\jdiniz\\source\\repos\\Trashbin:/asset-input:delegated" -v "C:\\Users\\jdiniz\\source\\repos\\Trashbin\\cdk.out\\asset.7b0ab255137ca264b0e1942636255964ac4d7b7791afa0f03ba0190b69b4971e:/asset-output:delegated" -w "/asset-input/Joba/Task" "public.ecr.aws/sam/build-dotnet8" "/bin/sh" -c " dotnet tool install -g Amazon.Lambda.Tools && dotnet build && dotnet lambda package --output-package /asset-output/function.zip"
       at Amazon.JSII.Runtime.Service
    
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
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions