diff --git a/AWSLambdaSharpTemplate.sln b/AWSLambdaSharpTemplate.sln index 6ad29d2..d16cc7b 100644 --- a/AWSLambdaSharpTemplate.sln +++ b/AWSLambdaSharpTemplate.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqsEventFunction", "samples EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqsEventFunctionWithParallelism", "samples\SqsEventFunctionWithParallelism\SqsEventFunctionWithParallelism.csproj", "{EC8A27C9-363C-434A-BEBE-0955E1BB40FC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqsBatchResponseFunction", "samples\SqsBatchResponseFunction\SqsBatchResponseFunction.csproj", "{968317B0-6204-414B-8CC0-6B7D0B616236}" +EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{AF4300F6-9636-4925-B8D5-F98E1BDC5EF5}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kralizek.Lambda.Template", "src\Kralizek.Lambda.Template\Kralizek.Lambda.Template.csproj", "{D092A193-823E-4205-872B-E3060BC3CF1A}" @@ -62,6 +64,10 @@ Global {EC8A27C9-363C-434A-BEBE-0955E1BB40FC}.Debug|Any CPU.Build.0 = Debug|Any CPU {EC8A27C9-363C-434A-BEBE-0955E1BB40FC}.Release|Any CPU.ActiveCfg = Release|Any CPU {EC8A27C9-363C-434A-BEBE-0955E1BB40FC}.Release|Any CPU.Build.0 = Release|Any CPU + {968317B0-6204-414B-8CC0-6B7D0B616236}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {968317B0-6204-414B-8CC0-6B7D0B616236}.Debug|Any CPU.Build.0 = Debug|Any CPU + {968317B0-6204-414B-8CC0-6B7D0B616236}.Release|Any CPU.ActiveCfg = Release|Any CPU + {968317B0-6204-414B-8CC0-6B7D0B616236}.Release|Any CPU.Build.0 = Release|Any CPU {D092A193-823E-4205-872B-E3060BC3CF1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D092A193-823E-4205-872B-E3060BC3CF1A}.Debug|Any CPU.Build.0 = Debug|Any CPU {D092A193-823E-4205-872B-E3060BC3CF1A}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -86,6 +92,7 @@ Global {E93EC39F-7934-46CE-8FD1-6D882576D66D} = {3D496921-7E86-44BE-AF72-9786A924052A} {DF86D670-8E2F-40B8-899E-E544B50C4024} = {3D496921-7E86-44BE-AF72-9786A924052A} {EC8A27C9-363C-434A-BEBE-0955E1BB40FC} = {3D496921-7E86-44BE-AF72-9786A924052A} + {968317B0-6204-414B-8CC0-6B7D0B616236} = {3D496921-7E86-44BE-AF72-9786A924052A} {D092A193-823E-4205-872B-E3060BC3CF1A} = {AF4300F6-9636-4925-B8D5-F98E1BDC5EF5} {5574076F-7D6D-4AD2-9231-76A18D2A85D7} = {AF4300F6-9636-4925-B8D5-F98E1BDC5EF5} {F9D3C337-5936-4BCB-BA2F-03A9E7C148DD} = {AF4300F6-9636-4925-B8D5-F98E1BDC5EF5} diff --git a/samples/SqsBatchResponseFunction/Function.cs b/samples/SqsBatchResponseFunction/Function.cs new file mode 100644 index 0000000..1e5c97a --- /dev/null +++ b/samples/SqsBatchResponseFunction/Function.cs @@ -0,0 +1,69 @@ +using System; +using System.Text.Json.Serialization; +using System.Threading.Tasks; +using Amazon.Lambda.Core; +using Amazon.Lambda.SQSEvents; +using Kralizek.Lambda; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; + +[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] + +namespace SqsBatchResponseFunction; + +public class Function : RequestResponseFunction +{ + protected override void Configure(IConfigurationBuilder builder) + { + builder.AddEnvironmentVariables(); + } + + protected override void ConfigureLogging(ILoggingBuilder logging, IExecutionEnvironment executionEnvironment) + { + logging.AddConfiguration(Configuration.GetSection("Logging")); + + logging.AddLambdaLogger(new LambdaLoggerOptions + { + IncludeCategory = true, + IncludeLogLevel = true, + IncludeNewline = true + }); + } + + protected override void ConfigureServices(IServiceCollection services, IExecutionEnvironment executionEnvironment) + { + services.UseQueueMessageHandler(); + } +} + +public class TestMessage +{ + [JsonPropertyName("message")] + public string? Message { get; set; } +} + +public class TestMessageHandler : IMessageHandler +{ + private readonly ILogger _logger; + + public TestMessageHandler(ILogger logger) + { + _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + } + + public Task HandleAsync(TestMessage? message, ILambdaContext context) + { + _logger.LogInformation("Received notification: {Message}", message?.Message); + + if (message is { Message.Length: > 0 }) + { + if (message.Message.Contains("bad message", StringComparison.OrdinalIgnoreCase)) + { + throw new ArgumentException("message supplied was a bad message", nameof(message)); + } + } + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/samples/SqsBatchResponseFunction/SqsBatchResponseFunction.csproj b/samples/SqsBatchResponseFunction/SqsBatchResponseFunction.csproj new file mode 100644 index 0000000..de9ea76 --- /dev/null +++ b/samples/SqsBatchResponseFunction/SqsBatchResponseFunction.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + true + enable + + + + + + + + + + + + + \ No newline at end of file diff --git a/samples/SqsBatchResponseFunction/aws-lambda-tools-defaults.json b/samples/SqsBatchResponseFunction/aws-lambda-tools-defaults.json new file mode 100644 index 0000000..5e8d82c --- /dev/null +++ b/samples/SqsBatchResponseFunction/aws-lambda-tools-defaults.json @@ -0,0 +1,21 @@ +{ + "Information": [ + "This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.", + "To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.", + + "dotnet lambda help", + + "All the command line options for the Lambda command can be specified in this file." + ], + + "profile": "", + "region": "", + "configuration": "Release", + "framework": "net6.0", + "function-name": "SqsBatchResponseFunction", + "function-role": "", + "function-runtime": "dotnet6", + "function-memory-size": 128, + "function-timeout": 30, + "function-handler": "SqsBatchResponseFunction::SqsBatchResponseFunction.Function::FunctionHandlerAsync" +}