如何限制AWS Visual Studio Toolkit发布C# Lambda的冗余打包内容?
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:
- Create or edit a publish profile;
- Click Show all settings at the bottom of the wizard;
- Navigate to the File Publish Options tab;
- Under Exclude files, add these wildcard rules:
xunit*.dll**\*.resources.dllMicrosoft.TestPlatform*.dll
- If you’re using self-contained deployment, also add
<RuntimeIdentifiers>linux-x64</RuntimeIdentifiers>to your.csprojto 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.dllinstead of**\*.resources.dll(the latter matches resource files in all subdirectories, likecs/orfr/); - 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

