Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda Annotations API Routing #1616

Open
1 of 2 tasks
jeastham1993 opened this issue Nov 16, 2023 · 2 comments
Open
1 of 2 tasks

Lambda Annotations API Routing #1616

jeastham1993 opened this issue Nov 16, 2023 · 2 comments
Assignees
Labels
annotations feature-request A feature should be added or improved. p2 This is a standard priority issue

Comments

@jeastham1993
Copy link
Contributor

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

@jeastham1993 jeastham1993 added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Nov 16, 2023
@ashishdhingra
Copy link
Contributor

Needs review with the team.

@ashishdhingra ashishdhingra added needs-review annotations p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. needs-review labels Nov 16, 2023
@andy-potter-evotix
Copy link

We have a MACH architecture platform which would really benefit from this feature. We are using .NET8 with AOT and the shape of our APIs and their usage pattern means we don't want to run discreet lambdas for each method. Having the ability to do what is requested here would be very useful to us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
annotations feature-request A feature should be added or improved. p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

4 participants