Skip to content

Commit 6c3493c

Browse files
committed
Handle both stream optimized and api gateway optimized
1 parent a9e06fb commit 6c3493c

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
</ItemGroup>
1313
<!-- AWS -->
1414
<ItemGroup>
15+
<PackageVersion Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
1516
<PackageVersion Include="Amazon.Lambda.AspNetCoreServer.Hosting" Version="1.9.0" />
1617
<PackageVersion Include="Amazon.Lambda.RuntimeSupport" Version="1.13.4" />
1718
<PackageVersion Include="Amazon.Lambda.Core" Version="2.7.1" />

src/api/Elastic.Documentation.Api.Lambda/Elastic.Documentation.Api.Lambda.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</ItemGroup>
2828

2929
<ItemGroup>
30+
<PackageReference Include="Amazon.Lambda.AspNetCoreServer.Hosting"/>
3031
<PackageReference Include="Elastic.OpenTelemetry" />
3132
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
3233
</ItemGroup>

src/api/Elastic.Documentation.Api.Lambda/Program.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

5+
using System.Text.Json.Serialization;
6+
using Amazon.Lambda.APIGatewayEvents;
7+
using Amazon.Lambda.Serialization.SystemTextJson;
8+
using Elastic.Documentation.Api.Core.AskAi;
9+
using Elastic.Documentation.Api.Core.Search;
510
using Elastic.Documentation.Api.Infrastructure;
611
using Elastic.Documentation.ServiceDefaults;
7-
using Microsoft.Extensions.Logging;
812
using OpenTelemetry;
913
using OpenTelemetry.Metrics;
1014
using OpenTelemetry.Trace;
@@ -48,12 +52,24 @@
4852
process.Refresh();
4953
Console.WriteLine($"Elastic OTel configured. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
5054

51-
// Configure Kestrel to listen on port 8080 for Lambda Web Adapter
52-
// Lambda Web Adapter expects the app to run as a standard HTTP server on localhost:8080
53-
_ = builder.WebHost.ConfigureKestrel(serverOptions =>
55+
// If we are running in Lambda Web Adapter response_stream mode, configure Kestrel to listen on port 8080
56+
// Otherwise, configure AWS Lambda hosting for API Gateway HTTP API
57+
if (Environment.GetEnvironmentVariable("AWS_LWA_INVOKE_MODE") == "response_stream")
5458
{
55-
serverOptions.ListenLocalhost(8080);
56-
});
59+
// Configure Kestrel to listen on port 8080 for Lambda Web Adapter
60+
// Lambda Web Adapter expects the app to run as a standard HTTP server on localhost:8080
61+
_ = builder.WebHost.ConfigureKestrel(serverOptions =>
62+
{
63+
serverOptions.ListenLocalhost(8080);
64+
});
65+
}
66+
else
67+
{
68+
// Configure AWS Lambda hosting with custom JSON serializer context for API Gateway HTTP API
69+
_ = builder.Services.AddAWSLambdaHosting(LambdaEventSource.HttpApi, new SourceGeneratorLambdaJsonSerializer<LambdaJsonSerializerContext>());
70+
_ = builder.WebHost.UseKestrelHttpsConfiguration();
71+
}
72+
5773
process.Refresh();
5874
Console.WriteLine($"Kestrel configured to listen on port 8080. Memory: {process.WorkingSet64 / 1024 / 1024} MB");
5975

@@ -82,3 +98,10 @@
8298
Console.WriteLine($"Stack trace: {ex.StackTrace}");
8399
throw;
84100
}
101+
102+
[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyRequest))]
103+
[JsonSerializable(typeof(APIGatewayHttpApiV2ProxyResponse))]
104+
[JsonSerializable(typeof(AskAiRequest))]
105+
[JsonSerializable(typeof(SearchRequest))]
106+
[JsonSerializable(typeof(SearchResponse))]
107+
internal sealed partial class LambdaJsonSerializerContext : JsonSerializerContext;

0 commit comments

Comments
 (0)