Skip to content

Commit

Permalink
Partial batch response sample
Browse files Browse the repository at this point in the history
  • Loading branch information
bartpio committed Feb 3, 2023
1 parent 00815ad commit b6630ef
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
7 changes: 7 additions & 0 deletions AWSLambdaSharpTemplate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
69 changes: 69 additions & 0 deletions samples/SqsBatchResponseFunction/Function.cs
Original file line number Diff line number Diff line change
@@ -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<SQSEvent, SQSBatchResponse>
{
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<TestMessage, TestMessageHandler>();
}
}

public class TestMessage
{
[JsonPropertyName("message")]
public string? Message { get; set; }
}

public class TestMessageHandler : IMessageHandler<TestMessage>
{
private readonly ILogger<TestMessageHandler> _logger;

public TestMessageHandler(ILogger<TestMessageHandler> 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;
}
}
19 changes: 19 additions & 0 deletions samples/SqsBatchResponseFunction/SqsBatchResponseFunction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Logging.AspNetCore" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Kralizek.Lambda.Template.Sqs\Kralizek.Lambda.Template.Sqs.csproj" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions samples/SqsBatchResponseFunction/aws-lambda-tools-defaults.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit b6630ef

Please sign in to comment.