Skip to content

Lambda sqs annotations updates #7572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RegularEndpoint\RegularEndpoint.csproj
44 changes: 44 additions & 0 deletions samples/aws/lambda-sqs-annotations/SQSLambda_4/AWSLambda-SQS.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 15.0.26730.12
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServerlessEndpoint", "ServerlessEndpoint\ServerlessEndpoint.csproj", "{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RegularEndpoint", "RegularEndpoint\RegularEndpoint.csproj", "{06754C55-CA74-4F17-9472-244B8A3FEB1E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Messages", "Messages\Messages.csproj", "{1BEFE981-8E75-4152-B6E3-8D488AE8530F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x64.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x64.Build.0 = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x86.ActiveCfg = Debug|Any CPU
{D9FB3A23-D74F-4754-9A22-ACF829EEEB70}.Debug|x86.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x64.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x64.Build.0 = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x86.ActiveCfg = Debug|Any CPU
{06754C55-CA74-4F17-9472-244B8A3FEB1E}.Debug|x86.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x64.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x64.Build.0 = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x86.ActiveCfg = Debug|Any CPU
{1BEFE981-8E75-4152-B6E3-8D488AE8530F}.Debug|x86.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6C66C18C-5075-4811-AF40-2997E8863F3B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NServiceBus" Version="10.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using NServiceBus;

public class ResponseMessage : IMessage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using NServiceBus;

public class TriggerMessage : IMessage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using NServiceBus;

Console.Title = "RegularEndpoint";

var endpointConfiguration = new EndpointConfiguration("RegularEndpoint");

endpointConfiguration.UseSerialization<SystemJsonSerializer>();
endpointConfiguration.UseTransport<SqsTransport>();

var endpointInstance = await Endpoint.Start(endpointConfiguration);

Console.WriteLine("Press [ENTER] to send a message to the serverless endpoint queue.");
Console.WriteLine("Press [Esc] to exit.");

while (true)
{
var key = Console.ReadKey();
Console.WriteLine();
switch (key.Key)
{
case ConsoleKey.Enter:
await endpointInstance.Send("ServerlessEndpoint", new TriggerMessage());
Console.WriteLine("Message sent to the serverless endpoint queue.");
break;
case ConsoleKey.Escape:
await endpointInstance.Stop();
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Messages\Messages.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NServiceBus.AmazonSQS" Version="9.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;

public class ResponseMessageHandler : IHandleMessages<ResponseMessage>
{
static readonly ILog Log = LogManager.GetLogger<ResponseMessageHandler>();

public Task Handle(ResponseMessage message, IMessageHandlerContext context)
{
Log.Info($"Handling {nameof(ResponseMessage)} in RegularEndpoint");
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Threading.Tasks;
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Annotations.APIGateway;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;
using NServiceBus;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly:LambdaSerializer(typeof(DefaultLambdaJsonSerializer))]

namespace LambdaFunctions;

public class HttpLambda(IAwsLambdaSQSEndpoint serverlessEndpoint)
{
#region HttpFunctionHandler
[LambdaFunction(Policies = "AWSLambda_FullAccess, AmazonSQSFullAccess")]
[HttpApi(LambdaHttpMethod.Get, "/")]
public async Task<string> HttpGetHandler(ILambdaContext context)
{
await serverlessEndpoint.Send(new TriggerMessage(), context);
return $"{nameof(TriggerMessage)} sent.";
}
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Mock Lambda Test Tool": {
"commandName": "Executable",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Messages\Messages.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Annotations" Version="1.*" />
<PackageReference Include="Amazon.Lambda.APIGatewayEvents" Version="2.*" />
<PackageReference Include="Amazon.Lambda.Core" Version="2.*" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.*" />
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.*" />
<PackageReference Include="NServiceBus.AmazonSQS" Version="9.0.0-alpha.1" />
<PackageReference Include="NServiceBus.AwsLambda.SQS" Version="4.0.0-alpha.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Annotations.SQS;
using Amazon.Lambda.Core;
using Amazon.Lambda.SQSEvents;
using NServiceBus;

namespace LambdaFunctions;

public class SqsLambda(IAwsLambdaSQSEndpoint serverlessEndpoint)
{
#region SqsFunctionHandler
[LambdaFunction]
[SQSEvent("arn:aws:sqs:region:account-id:ServerlessEndpoint")]
public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context)
{
using var cancellationTokenSource =
new CancellationTokenSource(context.RemainingTime.Subtract(DefaultRemainingTimeGracePeriod));

await serverlessEndpoint.Process(evnt, context, cancellationTokenSource.Token);
}
#endregion

static readonly TimeSpan DefaultRemainingTimeGracePeriod = TimeSpan.FromSeconds(10);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.Extensions.DependencyInjection;
using NServiceBus;
using NServiceBus.AwsLambda.SQS;

namespace LambdaFunctions;

#region EndpointSetup
[Amazon.Lambda.Annotations.LambdaStartup]
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddAwsLambdaSQSEndpoint("ServerlessEndpoint", (endpointConfiguration, _) =>
{
endpointConfiguration.UseSerialization<SystemJsonSerializer>();

var routing = endpointConfiguration.RoutingSettings;

routing.RouteToEndpoint(typeof(TriggerMessage), "ServerlessEndpoint");
routing.RouteToEndpoint(typeof(ResponseMessage), "RegularEndpoint");
});
}
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Threading.Tasks;
using NServiceBus;
using NServiceBus.Logging;

namespace LambdaFunctions;

#region TriggerMessageHandler
class TriggerMessageHandler : IHandleMessages<TriggerMessage>
{
static readonly ILog Log = LogManager.GetLogger<TriggerMessageHandler>();

public async Task Handle(TriggerMessage message, IMessageHandlerContext context)
{
Log.Info($"Handling {nameof(TriggerMessage)} in ServerlessEndpoint.");
await context.Send(new ResponseMessage());
}
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"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": "default",
"region": "ap-southeast-2",
"configuration": "Debug",
"framework": "net8.0",
"template": "serverless.template",
"template-parameters": "",
"stack-name": "nservicebus-aws-lambda-sample-jtd",
"s3-bucket": "nservicebus-aws-lambda-sample-jtd"
}
Loading