You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过AWS CDK部署.NET 6 C# Lambda Layer并解决程序集加载错误

问题:AWS CDK部署.NET 6 Lambda Layer时程序集加载失败

我想用AWS CDK部署基于.NET 6和C#开发的Lambda Layer,并在.NET 6 Lambda函数中调用它。部署完成后运行时出现错误:

Could not load file or assembly 'TestS3Layer, Version=1.0.0.0

TestS3Layer是Lambda函数引用的层DLL。


已执行的操作

1. 通过CDK部署Layer与Lambda函数

使用AWS CDK部署.NET 6 Lambda Layer,代码如下:

const TestS3Layer = new lambda.LayerVersion(this, 'TestS3Layer', {
    code: lambda.Code.fromAsset('G:/POC/TestS3Layer/bin/Release/net6.0/'),
    compatibleRuntimes: [lambda.Runtime.DOTNET_6],
    description: 'Layer created via AWs CDK.',
});

(参考Layer DLLs目录结构截图)

创建.NET 6 Lambda函数,功能为生成随机数据并通过TestS3Layer上传至S3存储桶,通过CDK部署并关联该Layer,代码如下:

// Create lambda function
const myFunction = new lambda.Function(this, 'TestLambdaLayerIntegrationFunction', {
    runtime: lambda.Runtime.DOTNET_6,
    handler: 'TestLambdaLayerIntegrationFunction::TestLambdaLayerIntegrationFunction.Function::FunctionHandler',
    code: lambda.Code.fromAsset('G:/POC/TestLambdaLayerIntegrationFunction/src/TestLambdaLayerIntegrationFunction/bin/Release/net6.0/'),
    memorySize: 256,
    timeout: cdk.Duration.seconds(60),
    role: existingRole,
    description: ".Net 6 Lambda function deployed using AWS CDK.",
    layers: [TestS3Layer]
});

部署完成后,通过AWS控制台测试事件触发,出现如下错误:

"cause": {
    "errorType": "FileNotFoundException",
    "errorMessage": "Could not load file or assembly 'TestS3Layer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.\n",
    "stackTrace": [
      "at TestLambdaLayerIntegrationFunction.Function..ctor()",
      "at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)"
    ]
  }

2. 通过CLI部署Layer,CDK部署Lambda函数

使用以下CLI命令发布Layer:

dotnet lambda publish-layer --region region-name --layer-name layer-name --layer-type runtime-package-store -sb s3-bucket-name -f net6.0 --package-manifest manifest.xml

该命令会将Layer发布为runtime-package-store类型,部署后的文件结构示例(以AWSSDK.S3.dll为例):
\dotnetcore\store\x64\net6.0\awssdk.s3\3.7.201.12\lib\netcoreapp3.1\AWSSDK.S3.dll

什么是Runtime Package Store?

Lambda Layer的核心目标之一是将函数依赖单独打包,避免每次代码变更都重新上传依赖。.NET Core无法直接从部署包外的目录加载程序集,需在dotnet publish阶段明确所有依赖,且**.NET Core运行时启动时需知晓所有程序集目录**。AWS .NET工具通过.NET Core的Runtime package stores特性解决该问题。

.NET Core运行时通过环境变量获取程序集目录,需配置:DOTNET_SHARED_STORE: '/opt/dotnetcore/store/'。

通过CDK部署Lambda函数并关联该Layer的ARN,同时配置环境变量,代码如下:

// Existing Lambda Layer ARN
const existingLayerArn = 'arn:aws:lambda:us-east-1:123456789012:layer:TestS3Layer:1';

// Lambda function
const myFunction = new lambda.Function(this, 'TestLambdaLayerIntegrationFunction', {
    runtime: lambda.Runtime.DOTNET_6,
    handler: 'TestLambdaLayerIntegrationFunction::TestLambdaLayerIntegrationFunction.Function::FunctionHandler',
    code: lambda.Code.fromAsset('G:/POC/TestLambdaLayerIntegrationFunction/src/TestLambdaLayerIntegrationFunction/bin/Release/net6.0/'),
    memorySize: 256,
    timeout: cdk.Duration.seconds(60),
    role: existingRole,
    environment: {
      DOTNET_SHARED_STORE: '/opt/dotnetcore/store/',
    },
    description: ".Net 6 Lambda function deployed using AWS CDK.",
    layers: [lambda.LayerVersion.fromLayerVersionArn(this, 'TestS3Layer', existingLayerArn)],
});

部署后测试正常,无错误。


提问

目前通过CLI部署Layer可正常运行,但纯CDK部署未采用runtime-package-store方式,请问我在CDK部署中遗漏了什么?如何仅通过AWS CDK实现正确部署?恳请提供参考代码示例。

内容的提问来源于stack exchange,提问作者Vishal Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 21:36:01