使用Python CDK将dotnet 8代码打包为AWS Lambda函数

0 投票
2 回答
30 浏览
提问于 2025-04-12 05:07

我正在使用CDK和Python,想要把dotnet 8的代码打包并部署为一个lambda函数。

下面这段Python代码出现了一个错误:Error: .NET binaries for Lambda function are not correctly installed in the /var/task directory of the image when the image was built. 这意思是说,构建镜像时,Lambda函数所需的.NET二进制文件没有正确安装在/var/task目录下。

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"),
    )

在使用CDK和dotnet时,有一个打包选项,像下面这样,可以将代码构建并打包。

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

那么,如何在Python CDK中实现这个打包选项,以便部署一个dotnet 8的lambda函数?或者在使用Python CDK之前,如何将.NET代码构建成所需的二进制文件?

2 个回答

0

我用以下代码把这个搞定了

        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),
    
    )

撰写回答