Lambda Annotations API Routing
@normj is already working on this.
Since Nov 17, 2023.
Assessment
This issue has not been assessed yet.
Description
Describe the feature
With the release of the [LambdaGlobalProperties] attribute that can auto-generate the static Main() method for non managed and native AOT runtimes this functionality could be extended to provide API routing capabilities. The API annotations on methods could be used as part of a switch statement for the inbound API event.
Use Case
Running a simple web API on Lambda without needing to introduce the overhead of ASP.NET. Removes the need to set the ANNOTATIONS_HANDLER environment variable when running multiple API endpoints in the same Lambda function.
Allows the use of an API Gateway {proxy+} route with all API request routing to the same Lambda function.
Proposed Solution
The GenerateMain global properties attribute adds a switch statement based on an environment variable. Introduce a new ApiRouting Lambda Global Property that will generate the static Main method. Include a switch statement that uses a combination of the HttpMethod and Path from the incoming API event to route to the correct method.
Example of generated code:
public static async Task Main(string[] args)
{
Func<APIGatewayProxyRequest , Task<APIGatewayProxyResponse>> apiHandler = Handler;
await Amazon.Lambda.RuntimeSupport.LambdaBootstrapBuilder.Create(apiHandler, new Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer()).Build().RunAsync();
}
public async static Task<APIGatewayProxyResponse> Handler(APIGatewayProxyRequest request)
{
switch ($"{request.HttpMethod.ToUpper()} {request.Path.ToUpper()}")
{
case "GET /products":
return GetProductsHandler();
default:
return new APIGatewayProxyResponse()
{
StatusCode = 404
};
}
}
Other Information
No response
Acknowledgements
- I may be able to implement this feature request
- This feature might incur a breaking change
AWS .NET SDK and/or Package version used
Amazon.Lambda.Annotations
Targeted .NET Platform
.NET 6, .NET 8
Operating System and version
All
- Dominant language
- C#
- Stars
- 1.7k
- Forks
- 503
- Avg merge
- 1d 18h
- Merged PRs (30d)
- 21
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from aws/aws-lambda-dotnet
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
aws/aws-lambda-dotnet#2571 · 1 comment ·
-
bug module/aspnetcore-support p2 queued
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
aws/aws-lambda-dotnet#1123 · 4 comments · 1 reaction ·
-
feature-request
Difficulty 4/5 3-5 days Newbie friendliness 68/100
aws/aws-lambda-dotnet#2572 · 1 comment ·
-
feature-request needs-triage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
aws/aws-lambda-dotnet#2551 · 1 comment ·
-
feature-request needs-review
Difficulty 5/5 Over a week Newbie friendliness 35/100
aws/aws-lambda-dotnet#2519 ·
All issues in aws/aws-lambda-dotnet
Similar issues
-
type/automation type/tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
t/bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
ci-failure-cause test-failure
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
area:auth FE mvp P3
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
klasolsson81/jobbliggaren#1788 ·