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

如何限制AWS Visual Studio Toolkit发布C# Lambda的冗余打包内容?

How to Trim Redundant Files from AWS Lambda Deployment Packages for C#

Hey there, this is a super common pain point when using the AWS Toolkit for Visual Studio to publish C# Lambda functions—especially when your solution includes test projects. Those extra xunit libraries, TestPlatform components, and multi-language resource DLLs bloat your package, hurt cold start times, and add unnecessary S3 costs. Let’s break down actionable fixes to clean up your deployment zip:


1. Fix Project-Level Exclusions (Most Likely Fix for Your Failed Attempt)

Chances are your earlier exclusion attempts didn’t work because either:

  • Your Lambda project accidentally references the test project, forcing those dependencies to be included;
  • Your exclusion rules used incorrect wildcards.

Open your Lambda project’s .csproj file and add these rules directly (this is more reliable than Visual Studio’s UI):

<!-- Remove test-related libraries entirely -->
<ItemGroup>
  <Content Remove="xunit*.dll" />
  <Content Remove="Microsoft.TestPlatform*.dll" />
  <Content Remove="Microsoft.VisualStudio.TestPlatform*.dll" />
  <!-- Exclude ALL multi-language resource DLLs in subdirectories -->
  <Content Remove="**\*.resources.dll" />
</ItemGroup>

<!-- Alternatively, use the dedicated exclusion property for AWS deployments -->
<PropertyGroup>
  <ExcludeFilesFromDeployment>xunit*.dll;Microsoft.TestPlatform*.dll;Microsoft.VisualStudio.TestPlatform*.dll;**\*.resources.dll</ExcludeFilesFromDeployment>
</PropertyGroup>

Critical First Step: Double-check your Lambda project’s references and remove any accidental references to your test projects—this is the #1 cause of test dependencies sneaking into the package.


2. Use AWS Lambda Toolchain Configuration

If you have an aws-lambda-tools-defaults.json file in your Lambda project root (auto-created by the AWS Toolkit), add an exclude field to define global exclusion rules:

{
  "profile": "your-aws-profile",
  "region": "your-region",
  "configuration": "Release",
  "framework": "net6.0",
  "function-name": "YourLambdaFunction",
  "exclude": "xunit*.dll,Microsoft.TestPlatform*.dll,Microsoft.VisualStudio.TestPlatform*.dll,**/*.resources.dll,**/Test*/**"
}

The **/Test*/** rule will exclude any test project output folders that might have been copied into your build directory.


3. Fine-Tune with Visual Studio Publish Profiles

When using the "Publish to AWS" wizard:

  1. Create or edit a publish profile;
  2. Click Show all settings at the bottom of the wizard;
  3. Navigate to the File Publish Options tab;
  4. Under Exclude files, add these wildcard rules:
    • xunit*.dll
    • **\*.resources.dll
    • Microsoft.TestPlatform*.dll
  5. If you’re using self-contained deployment, also add <RuntimeIdentifiers>linux-x64</RuntimeIdentifiers> to your .csproj to avoid packaging unnecessary runtime language packs (Lambda only needs the Linux runtime).

4. Command-Line Packaging (Full Control)

For ultimate control, use the AWS Lambda .NET CLI tool to package your function directly—this bypasses any Visual Studio quirks:

dotnet lambda package --configuration Release --framework net6.0 --output-package bin/Release/net6.0/YourLambda.zip --exclude "xunit*.dll,**/*.resources.dll,Microsoft.TestPlatform*.dll"

This ensures only the Lambda project’s required dependencies are included.


Why Your Earlier Exclusion Attempt Failed

Most commonly:

  • You forgot to remove the test project reference from your Lambda project;
  • You used *.resources.dll instead of **\*.resources.dll (the latter matches resource files in all subdirectories, like cs/ or fr/);
  • Your exclusion rules were applied at the solution level instead of the Lambda project level.

These fixes should drastically reduce your deployment package size, speed up cold starts, and cut down on unnecessary costs.

内容的提问来源于stack exchange,提问作者Randall Borck

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:55:44