Search code examples
pythonpython-3.x.net-coreaws-lambdaaws-cdk

Using Python CDK to bundle dotnet 8 code to AWS Lambda function


I am using CDK with Python and trying to build, package and deploy dotnet 8 code as a lambda function.

This python code below gets an error Error: .NET binaries for Lambda function are not correctly installed in the /var/task directory of the image when the image was built.

from constructs import Construct
from aws_cdk import (
    Duration,
    Stack,
    aws_iam as iam,
    aws_lambda as lambda_
)
    csharp_lambda = lambda_.Function(
        self, "PythonCdkDotnetLambda",
        runtime=lambda_.Runtime.DOTNET_8,
        handler="helloworld::helloworld.Functions::ExecuteFunc",  
        code=lambda_.Code.from_asset("../path/to/src"),
    )

When using CDK with dotnet there is a bundling option like below where the code is built and packaged

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

How do you do this BundlingOption for Python CDK that needs to deploy a dotnet 8 lambda or how do you build .net code into the required binaries before deploying using Python CDK?


Solution

  • I got this working using the following code

            bundling_options = {
            "command": [
                "/bin/sh",
                "-c",
                " dotnet tool install -g Amazon.Lambda.Tools"+
                " && dotnet build" +
                " && dotnet lambda package --project-location helloworld/ --output-package /asset-output/function.zip"
            ],
            "image": lambda_.Runtime.DOTNET_8.bundling_image,
            "user": "root"
        }
                
        
        csharp_lambda = lambda_.Function(
            self, "PythonCdkDotnetLambda",
            runtime=lambda_.Runtime.DOTNET_8,
            handler="helloworld::helloworld.Functions::ExecuteFunc",  
            code=lambda_.Code.from_asset("../path/to/lambda", bundling=bundling_options),
        
        )