playground/Lambda/SQSProcessorFunction/Function.cs (33 lines of code) (raw):
using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using OpenTelemetry.Instrumentation.AWSLambda;
using OpenTelemetry.Trace;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace SQSProcessorFunction;
public class Function
{
IHost _host;
TracerProvider _traceProvider;
public Function()
{
var builder = new HostApplicationBuilder();
builder.AddServiceDefaults();
_host = builder.Build();
_traceProvider = _host.Services.GetRequiredService<TracerProvider>();
}
public Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
=> AWSLambdaWrapper.TraceAsync(_traceProvider, async (evnt, context) =>
{
foreach (var message in evnt.Records)
{
await ProcessMessageAsync(message, context);
}
}, evnt, context);
private async Task ProcessMessageAsync(SQSEvent.SQSMessage message, ILambdaContext context)
{
context.Logger.LogInformation($"Processed message {message.Body}");
// TODO: Do interesting work based on the new message
await Task.CompletedTask;
}
}