nativeAOT, calling AddAWSLambdaHosting, file size increases 6MB #1906
-
Hello. I'm building dotnet AOT binary. when I call builder.Services.AddAWSLambdaHosting()`, the file size increases by 6MB. How do I reduce file size, for API Gateway and Lambda. without AddAWSLambdaHosting: 7,976,992 bytes [environment] [Program.cs] using System.Text.Json.Serialization;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Serialization.SystemTextJson;
var builder = WebApplication.CreateSlimBuilder(args);
//builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonSerializerContext>());
builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default));
var app = builder.Build();
app.MapGet("/", (AppRequest request) => new AppResponse(request.Status));
app.Run();
[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyRequest), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyResponse), GenerationMode = JsonSourceGenerationMode.Default)]
internal partial class LambdaJsonSerializerContext : JsonSerializerContext { }
internal record AppResponse(string Status);
internal record AppRequest(string Status);
[JsonSerializable(typeof(AppRequest), GenerationMode = JsonSourceGenerationMode.Metadata)]
[JsonSerializable(typeof(AppResponse), GenerationMode = JsonSourceGenerationMode.Default)]
internal partial class AppJsonSerializerContext : JsonSerializerContext { } [sample.csproj] <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<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>
<!-- Generate Native AOT image during publishing to improve cold start time. -->
<PublishAot>true</PublishAot>
<!-- StripSymbols tells the compiler to strip debugging symbols from the final executable if we're on Linux and put them into their own file.
This will greatly reduce the final executable's size.-->
<StripSymbols>true</StripSymbols>
<!-- TrimMode partial will only trim assemblies marked as trimmable. To reduce package size make all assemblies trimmable and set TrimMode to full.
If there are trim warnings during build, you can hit errors at runtime.-->
<TrimMode>full</TrimMode>
<DebuggerSupport>false</DebuggerSupport>
<OptimizationPreference>Size</OptimizationPreference>
<DebuggerSupport>false</DebuggerSupport>
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<EventSourceSupport>false</EventSourceSupport>
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
<MetadataUpdaterSupport>false</MetadataUpdaterSupport>
<MetricsSupport>false</MetricsSupport>
<StackTraceSupport>false</StackTraceSupport>
<UseSystemResourceKeys>true</UseSystemResourceKeys>
<XmlResolverIsNetworkingEnabledByDefault>false</XmlResolverIsNetworkingEnabledByDefault>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.7.3" />
</ItemGroup>
</Project> [dockerfile] FROM mcr.microsoft.com/dotnet/sdk:9.0-bookworm-slim-amd64 AS build
WORKDIR /src
RUN apt-get update && apt-get install gcc zlib1g-dev -y
COPY . .
RUN dotnet publish "sample.csproj" --configuration Release --runtime linux-x64 --output /app/publish
FROM public.ecr.aws/lambda/provided:al2023-x86_64
COPY --from=build /app/publish ./ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@sudoudaisuke When I want to understand the size of a Native AOT executable I use this tool. https://github.com/MichalStrehovsky/sizoscope |
Beta Was this translation helpful? Give feedback.
@sudoudaisuke When I want to understand the size of a Native AOT executable I use this tool. https://github.com/MichalStrehovsky/sizoscope