我正在将 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.
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"
}
};
如何为需要部署 dotnet 8 lambda 的 Python CDK 执行此 BundlingOption 操作,或者如何在使用 Python CDK 部署之前将 .net 代码构建到所需的二进制文件中?
我使用以下代码完成了此工作
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),
)