-
Notifications
You must be signed in to change notification settings - Fork 494
Create a serverless web api with multiple lambda functions (not proxy) #1484
-
Hi,
Is it possible to create multiple lambda functions having a single function handler within a serverless web api project but not having define lambda proxy?
So, I would need to have define something like this for serverless.template file:
{
"AWSTemplateFormatVersion": "2010年09月09日",
"Transform": "AWS::Serverless-2016年10月31日",
"Description": "An AWS Serverless Application that uses the ASP.NET Core framework running in Amazon Lambda.",
"Parameters": {},
"Conditions": {},
"Resources": {
"AspNetCoreFunction": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "TransfloExpress.FuelPortal.API::TransfloExpress.FuelPortal.API.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnet6",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 1500,
"Role": null,
"Policies": [
"AWSLambda_FullAccess",
"AmazonSSMReadOnlyAccess",
"AWSLambdaVPCAccessExecutionRole"
],
"Events": {
"CreateCustomerType" : {
"Type" : "Api",
"Properties" : {
"Path" : "/api/CustomerTypes",
"Method" : "POST"
}
}
}
}
},
"AspNetCoreFunction1": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "TransfloExpress.FuelPortal.API::TransfloExpress.FuelPortal.API.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnet6",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 1500,
"Role": null,
"Policies": [
"AWSLambda_FullAccess",
"AmazonSSMReadOnlyAccess",
"AWSLambdaVPCAccessExecutionRole"
],
"Events": {
"CreateCustomerType" : {
"Type" : "Api",
"Properties" : {
"Path" : "/api/CustomerTypes/{id}",
"Method" : "GET"
}
}
}
}
},
"AspNetCoreFunction2": {
"Type": "AWS::Serverless::Function",
"Properties": {
"Handler": "TransfloExpress.FuelPortal.API::TransfloExpress.FuelPortal.API.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnet6",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 1500,
"Role": null,
"Policies": [
"AWSLambda_FullAccess",
"AmazonSSMReadOnlyAccess",
"AWSLambdaVPCAccessExecutionRole"
],
"Events": {
"CreateCustomerType" : {
"Type" : "Api",
"Properties" : {
"Path" : "/api/CustomerTypes/{id}",
"Method" : "DELETE"
}
}
}
}
}
},
"Outputs": {
"ApiURL": {
"Description": "API endpoint URL for Prod environment",
"Value": {
"Fn::Sub": "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/FuelApi/"
}
}
}
}All those lambda functions point to a single handler, which is the basic one for now, I don't think if we'll need to extend this handler somehow:.
namespace TransfloExpress.FuelPortal.API; /// <summary> /// This class extends from APIGatewayProxyFunction which contains the method FunctionHandlerAsync which is the /// actual Lambda function entry point. The Lambda handler field should be set to /// /// AWSServerless3::AWSServerless3.LambdaEntryPoint::FunctionHandlerAsync /// </summary> public class LambdaEntryPoint : // The base class must be set to match the AWS service invoking the Lambda function. If not Amazon.Lambda.AspNetCoreServer // will fail to convert the incoming request correctly into a valid ASP.NET Core request. // // API Gateway REST API -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction // API Gateway HTTP API payload version 1.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction // API Gateway HTTP API payload version 2.0 -> Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction // Application Load Balancer -> Amazon.Lambda.AspNetCoreServer.ApplicationLoadBalancerFunction // // Note: When using the AWS::Serverless::Function resource with an event type of "HttpApi" then payload version 2.0 // will be the default and you must make Amazon.Lambda.AspNetCoreServer.APIGatewayHttpApiV2ProxyFunction the base class. Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction { /// <summary> /// The builder has configuration, logging and Amazon API Gateway already configured. The startup class /// needs to be configured in this method using the UseStartup<>() method. /// </summary> /// <param name="builder"></param> protected override void Init(IWebHostBuilder builder) { builder.ConfigureAppConfiguration( c => { //c.AddSystemsManager(source => { // source.Path = "/fuelApi"; // source.ReloadAfter = // TimeSpan.FromMinutes(10); //}); } ); builder .UseStartup<Startup>(); } /// <summary> /// Use this override to customize the services registered with the IHostBuilder. /// /// It is recommended not to call ConfigureWebHostDefaults to configure the IWebHostBuilder inside this method. /// Instead customize the IWebHostBuilder in the Init(IWebHostBuilder) overload. /// </summary> /// <param name="builder"></param> protected override void Init(IHostBuilder builder) { } }
Also, would it be possible to point each lambda function to a different handler if in teh future we'll gonna need it?
Sorry if I am not clear enough but I'm new in AWS.
Thank you very much for your help!
Daniela
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 2 comments
-
@danicristea7511 The above sample code when deployed, creates resources with LAMBDA_PROXY Integration Request, probably because the LambdaEntryPoint inherits from Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction class. I assume you are referring to not to have proxy+ resource created, which in this case is not created.
Regarding your 2nd question, the code generated when using Web API template mentions that LambdaEntryPoint class extends from APIGatewayProxyFunction which contains the method FunctionHandlerAsync which is the actual Lambda function entry point.. This mapping is implicit and looks like cannot be changed. Or, is your use case is referring to creating new resources with different endpoint (e.g. VehicleTypes)?
Thanks,
Ashish
Beta Was this translation helpful? Give feedback.
All reactions
-
Hi Ashish,
Thank you very much for your reply and patience!
All my confusions are because
- I do not get the difference between LAMBDA_PROXY and LAMBDA Integration Request. When and why should we use any option?
- What is the best way to build api REST web API gateway:
- using the blue print ASP.NET CORE WEB API
- or empty serverless application
?
In regard to the example I gave you, in serverless.template file, I defined 3 resources using the blue print ASP.NET core web api:
- /api/CustomerTypes POST
- /api/CustomerTypes/{id} GET
- /api/CustomerTypes/{id} DELETE
Each of them points to the same function handler FunctionHandlerAsync.
My questions were: - is it ok if all the resources (basically the endpoints) point to the same function handler? what would be the downsides?
- in what situation should I create a different function handler for each resource?
- how should I create a LAMBDA Integartion request and not a LAMBDA_PROXY integration request?
All the best,
Daniela
Beta Was this translation helpful? Give feedback.