使用 AWS cdk 部署 .NET6 lambda 函数时未找到引用的 class 库项目

Referenced class library projects are not found when using AWS cdk to deploying a .NET6 lambda function

我正在尝试在 Typescript 中使用 AWS CDK 部署基于 .NET6 的 AWS Lambda 函数 -“ABC.DynamoDBConnectLambda”。 “ABC.DynamoDBConnectLambda”引用了另外两个自定义 class 库——“ABC.Data”和“ABC.Services”,它们都位于同一个“src”文件夹中。

在 运行ning“cdk synth”之后,我收到以下错误消息:

 Skipping project "/ABC.Data/ABC.Data.csproj" because it was not found.
 Skipping project "/ABC.Services/ABC.Services.csproj" because it was not found.
 Restored /asset-input/ABC.DynamoDBConnectLambda.csproj (in 26.49 sec).
    /var/lang/bin/sdk/6.0.201/Microsoft.Common.CurrentVersion.targets(2065,5): warning : The referenced project '../ABC.Data/ABC.Data.csproj' does not exist. [/asset-input/ABC.Data/ABC.Data.csproj]
/var/lang/bin/sdk/6.0.201/Microsoft.Common.CurrentVersion.targets(2065,5): warning : The referenced project '..//ABC.Services/ABC.Services.csproj' does not exist. [/asset-input/ABC.Services.csproj]
/var/lang/bin/sdk/6.0.201/Microsoft.Common.CurrentVersion.targets(2065,5): warning : The referenced project '../Trustees.SFTPServices.Data/Trustees.SFTPServices.Data.csproj' does not exist.

如有任何帮助或建议,我们将不胜感激。提前致谢。

我做了一些研究,它似乎与发布项目的命令有关,下面是我运行:

的dotnet cli
dotnet publish -c Release -r linux-x64 --self-contained -o /asset-output

下面是我手头的完整cdk代码:

const dynamoDbConnectLambda = new lambda.Function(this, 'dynamoDBEventHandler', {
  runtime: lambda.Runtime.DOTNET_6,
  timeout: cdk.Duration.seconds(120),
  handler: 'bootstrap',
  code: lambda.AssetCode.fromAsset(path.join(__dirname,'../../src/Lambda.DynamoDBConnect'), {
    bundling: {
      image: lambda.Runtime.DOTNET_6.bundlingImage,
      command: [
        'bash', '-c',
        'export DOTNET_CLI_HOME=/tmp/DOTNET_CLI_Home && dotnet publish -c Release -r linux-x64 --self-contained -o /asset-output'
      ]
    }
  })
});

下面是 lambda 项目的 csproject 文件 - “ABC.DynamoDBConnect”

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <LangVersion>latest</LangVersion>
    <AWSProjectType>Lambda</AWSProjectType>
    <AssemblyName>bootstrap</AssemblyName>
    <!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
    <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
  </PropertyGroup>
  <!-- 
  When publishing Lambda functions for ARM64 to the provided.al2 runtime a newer version of libicu needs to be included
  in the deployment bundle because .NET requires a newer version of libicu then is preinstalled with Amazon Linux 2.
  -->
  <ItemGroup Condition="'$(RuntimeIdentifier)' == 'linux-arm64'">
    <RuntimeHostConfigurationOption Include="System.Globalization.AppLocalIcu" Value="68.2.0.9" />
    <PackageReference Include="Microsoft.ICU.ICU4C.Runtime" Version="68.2.0.9" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Amazon.Lambda.RuntimeSupport" Version="1.7.0" />
    <PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
    <PackageReference Include="Amazon.Lambda.S3Events" Version="2.0.1" />
    <PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.3.0" />
    <PackageReference Include="AWSSDK.Core" Version="3.7.10.2" />
    <PackageReference Include="AWSSDK.DynamoDBv2" Version="3.7.3.17" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\ABC.Service.csproj" />
    <ProjectReference Include="..\ABC.Data.csproj" />
</Project>

原来问题出在下面这一行:它只获取 src 文件夹中的项目 - Lambda.DynamoDBConnect,但是 ABC.ServiceABC.Data 被忽略了因为它们都存在于同一级别。

code: lambda.AssetCode.fromAsset(path.join(__dirname,'../../src/Lambda.DynamoDBConnect')

解决方案是更新代码如下,所有项目都原样复制,因此可以相互找到。

code: lambda.AssetCode.fromAsset(path.join(__dirname,'../../src')

PS:此解决方案有一个缺点,它会复制 src 文件夹中的所有内容,这意味着文件夹中存在的其他 un-needed 项目也会被复制。这可能会导致 re-thinking 左右的性能问题。