Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

nativeAOT, calling AddAWSLambdaHosting, file size increases 6MB #1906

Answered by normj
sudoudaisuke asked this question in Q&A
Discussion options

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
with AddAWSLambdaHosting: 13,929,400 bytes

[environment]
dotnet: 9.0 with publishaot
framework: asp.net minimal api
library: Amazon.Lambda.AspNetCoreServer.Hosting 1.7.3
runtime: ApiGateway HTTP API + AWS Lambda x86_64 Container ( public.ecr.aws/lambda/provided:al2023 )

[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 ./
You must be logged in to vote

@sudoudaisuke When I want to understand the size of a Native AOT executable I use this tool. https://github.com/MichalStrehovsky/sizoscope

Replies: 1 comment

Comment options

@sudoudaisuke When I want to understand the size of a Native AOT executable I use this tool. https://github.com/MichalStrehovsky/sizoscope

You must be logged in to vote
0 replies
Answer selected by sudoudaisuke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /