How Do You Have Multiple Functions in an AWS Lambda Assembly using Top-Level statements #1195
-
Reviewing the section 'Executable assemblies' from https://aws.amazon.com/blogs/compute/introducing-the-net-6-runtime-for-aws-lambda/ confuses me on how one might migrate from a .net 5.0 class library with multiple lambda functions to .net 6.0 lambda. Obviously, with a .net 5.0 lambda assembly, you could have more than one AWS lambda function, by setting the handler as specified for .net 5.0. How would you have multiple lambda functions in one library with top level statements? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
@hounddog22030 Thanks for starting discussion on this scenario. As I understand your scenario, you have multiple Lambda function handlers defined in a class library and while publishing to Lambda, you define fully qualified function handler for your scenario. The drawback I see in this approach is that it increases the Lambda function deployment size since all your unused handlers are defined in the same assembly. With top level statements, I do not think you could have multiple Lambda functions in the same project. For instance, something like doesn't work: using Amazon.Lambda.Core;
using Amazon.Lambda.RuntimeSupport;
using Amazon.Lambda.S3Events;
using Amazon.Lambda.Serialization.SystemTextJson;
// The function handler that will be called for each Lambda event
var simpleHandler = (string input, ILambdaContext context) =>
{
return input.ToUpper();
};
// The function handler that will be called for each Lambda event
var s3EventHandler = (S3Event evnt, ILambdaContext context) =>
{
foreach (var record in evnt.Records)
{
// Run business logic on the text contexts of the S3 object
}
return "S3Event handled";
};
// Build the Lambda runtime client passing in the handler to call for each
// event and the JSON serializer to use for translating Lambda JSON documents
// to .NET types.
await LambdaBootstrapBuilder.Create(simpleHandler, new DefaultLambdaJsonSerializer())
.Build()
.RunAsync();
await LambdaBootstrapBuilder.Create(s3EventHandler, new DefaultLambdaJsonSerializer())
.Build()
.RunAsync(); As code comment also mentions, you need to |
Beta Was this translation helpful? Give feedback.
-
As @ashishdhingra said by default Lambda functions using top-level statements or Lambda function deployed as executable assembly are for a single function. You could define your own environment variable and then check that environment variable to decide which handler to register with the Lambda bootstrap. Also keep in mind that using the class library approach that you are used to in previous runtimes is just as valid as using executable assemblies with top-level statements. Don't feel like you have to migrate to top-level statements if it doesn't fit with your use case. |
Beta Was this translation helpful? Give feedback.
As @ashishdhingra said by default Lambda functions using top-level statements or Lambda function deployed as executable assembly are for a single function. You could define your own environment variable and then check that environment variable to decide which handler to register with the Lambda bootstrap. Also keep in mind that using the class library approach that you are used to in previous runtimes is just as valid as using executable assemblies with top-level statements. Don't feel like you have to migrate to top-level statements if it doesn't fit with your use case.