Skip to content

Add integration for setting up SQS event source for a Lambda function. #47

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

Merged
merged 5 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions .autover/changes/d530dfa0-e51a-4bcb-8061-835237914aee.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Projects": [
{
"Name": "Aspire.Hosting.AWS",
"Type": "Patch",
"ChangelogMessages": [
"Add support for configuring SQS event source for a Lambda function",
"Update version of Amazon.Lambda.TestTool to install to version 0.10.0"
]
}
]
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,7 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml

.idea/
.idea/

# CDK temp files
**/cdk.out/**
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<PackageVersion Include="Amazon.Lambda.Core" Version="2.5.0" />
<PackageVersion Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.4.4" />
<PackageVersion Include="Amazon.Lambda.APIGatewayEvents" Version="2.7.1" />
<PackageVersion Include="Amazon.Lambda.SQSEvents" Version="2.2.0" />
<!-- AWS CDK dependencies -->
<PackageVersion Include="Amazon.CDK.Lib" Version="2.180.0" />
<!-- Open Telemetry -->
Expand Down
7 changes: 7 additions & 0 deletions integrations-on-dotnet-aspire-for-aws.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAWSCallsLambdaFunction",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebCalculatorFunctions", "playground\Lambda\WebCalculatorFunctions\WebCalculatorFunctions.csproj", "{4CE69424-50B4-1D2F-EA87-E9FC81C4BEA6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SQSProcessorFunction", "playground\Lambda\SQSProcessorFunction\SQSProcessorFunction.csproj", "{FC2267CD-9769-68B1-A271-774F98326A43}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -102,6 +104,10 @@ Global
{4CE69424-50B4-1D2F-EA87-E9FC81C4BEA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4CE69424-50B4-1D2F-EA87-E9FC81C4BEA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4CE69424-50B4-1D2F-EA87-E9FC81C4BEA6}.Release|Any CPU.Build.0 = Release|Any CPU
{FC2267CD-9769-68B1-A271-774F98326A43}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC2267CD-9769-68B1-A271-774F98326A43}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC2267CD-9769-68B1-A271-774F98326A43}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC2267CD-9769-68B1-A271-774F98326A43}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -122,6 +128,7 @@ Global
{A2750C2D-1F82-47CB-9EAB-B819E3EBDD74} = {C3C0B096-DB7C-4D1B-B8A0-91631B32B4DE}
{96396A08-6FB9-49C2-A923-1AF1C97087EF} = {C3C0B096-DB7C-4D1B-B8A0-91631B32B4DE}
{4CE69424-50B4-1D2F-EA87-E9FC81C4BEA6} = {C3C0B096-DB7C-4D1B-B8A0-91631B32B4DE}
{FC2267CD-9769-68B1-A271-774F98326A43} = {C3C0B096-DB7C-4D1B-B8A0-91631B32B4DE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBA55172-92F1-4495-A082-E0ABE4F4AF09}
Expand Down
3 changes: 3 additions & 0 deletions playground/Lambda/Lambda.AppHost/Lambda.AppHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
<OutputType>Exe</OutputType>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<IsAspireHost>true</IsAspireHost>
<NoWarn>$(NoWarn);CS8002</NoWarn> <!-- AWS CDK packages are not signed -->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.SQS" />
<ProjectReference Include="..\..\..\src\Aspire.Hosting.AWS\Aspire.Hosting.AWS.csproj" IsAspireProjectResource="false" />
<PackageReference Include="Aspire.Hosting.AppHost" />
<ProjectReference Include="..\SQSProcessorFunction\SQSProcessorFunction.csproj" />
<ProjectReference Include="..\ToUpperLambdaFunctionExecutable\ToUpperLambdaFunctionExecutable.csproj" />
<ProjectReference Include="..\WebAWSCallsLambdaFunction\WebAWSCallsLambdaFunction.csproj" />
<ProjectReference Include="..\WebCalculatorFunctions\WebCalculatorFunctions.csproj" />
Expand Down
10 changes: 10 additions & 0 deletions playground/Lambda/Lambda.AppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
using Amazon.Lambda;
using Aspire.Hosting;
using Aspire.Hosting.AWS.Lambda;

#pragma warning disable CA2252 // This API requires opting into preview features
Expand All @@ -8,6 +9,9 @@

var awsSdkConfig = builder.AddAWSSDKConfig().WithRegion(Amazon.RegionEndpoint.USWest2);

var cdkStackResource = builder.AddAWSCDKStack("AWSLambdaPlaygroundResources");
var sqsDemoQueue = cdkStackResource.AddSQSQueue("DemoQueue");

builder.AddAWSLambdaFunction<Projects.ToUpperLambdaFunctionExecutable>("ToUpperFunction", lambdaHandler: "ToUpperLambdaFunctionExecutable", new LambdaFunctionOptions { ApplicationLogLevel = ApplicationLogLevel.DEBUG, LogFormat = LogFormat.JSON});

var defaultRouteLambda = builder.AddAWSLambdaFunction<Projects.WebDefaultLambdaFunction>("LambdaDefaultRoute", lambdaHandler: "WebDefaultLambdaFunction");
Expand All @@ -30,5 +34,11 @@
.WithReference(multiplyFunction, Method.Get, "/multiply/{x}/{y}")
.WithReference(divideFunction, Method.Get, "/divide/{x}/{y}");


builder.AddAWSLambdaFunction<Projects.SQSProcessorFunction>("SQSProcessorFunction", "SQSProcessorFunction::SQSProcessorFunction.Function::FunctionHandler")
.WithReference(awsSdkConfig)
.WithSQSEventSource(sqsDemoQueue);


builder.Build().Run();

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17295;http://localhost:15136",
"applicationUrl": "https://localhost:17296;http://localhost:15137",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
Expand Down
44 changes: 44 additions & 0 deletions playground/Lambda/SQSProcessorFunction/Function.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this line?

Copy link
Member Author

@normj normj Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted. I also added a gitignore entry for playground launchsettings.json.

},
"Aspire_ProcessorFunction": {
"commandName": "Executable",
"executablePath": "dotnet",
"commandLineArgs": "exec --depsfile ./SQSProcessorFunction.deps.json --runtimeconfig ./SQSProcessorFunction.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.9.999\\amazon.lambda.testtool\\0.9.999\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll SQSProcessorFunction::SQSProcessorFunction.Function::FunctionHandler",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is here by accident right? you only want the one below - the 0.10.0 version

"workingDirectory": ".\\bin\\$(Configuration)\\net8.0"
},
"Aspire_SQSProcessorFunction": {
"commandName": "Executable",
"executablePath": "dotnet",
"commandLineArgs": "exec --depsfile ./SQSProcessorFunction.deps.json --runtimeconfig ./SQSProcessorFunction.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.10.0\\amazon.lambda.testtool\\0.10.0\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll SQSProcessorFunction::SQSProcessorFunction.Function::FunctionHandler",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0"
}
}
}
21 changes: 21 additions & 0 deletions playground/Lambda/SQSProcessorFunction/SQSProcessorFunction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
<!-- This property makes the build directory similar to a publish directory and helps the AWS .NET Lambda Mock Test Tool find project dependencies. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<!-- Generate ready to run images during publishing to improve cold start time. -->
<PublishReadyToRun>true</PublishReadyToRun>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Amazon.Lambda.Core" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" />
<PackageReference Include="Amazon.Lambda.SQSEvents" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Lambda.ServiceDefaults\Lambda.ServiceDefaults.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"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": "us-west-2",
"configuration": "Release",
"function-runtime": "dotnet8",
"function-memory-size": 512,
"function-timeout": 30,
"function-handler": "SQSProcessorFunction::SQSProcessorFunction.Function::FunctionHandler"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@
"profiles": {
"Aspire_AddFunction": {
"commandName": "Executable",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.0.2-preview\\amazon.lambda.testtool\\0.0.2-preview\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::AddFunctionHandler",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.10.0\\amazon.lambda.testtool\\0.10.0\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::AddFunctionHandler",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "dotnet"
},
"Aspire_MinusFunction": {
"commandName": "Executable",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.0.2-preview\\amazon.lambda.testtool\\0.0.2-preview\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::MinusFunctionHandler",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.10.0\\amazon.lambda.testtool\\0.10.0\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::MinusFunctionHandler",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "dotnet"
},
"Aspire_MultiplyFunction": {
"commandName": "Executable",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.0.2-preview\\amazon.lambda.testtool\\0.0.2-preview\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::MultiplyFunctionHandler",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.10.0\\amazon.lambda.testtool\\0.10.0\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::MultiplyFunctionHandler",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "dotnet"
},
"Aspire_DivideFunction": {
"commandName": "Executable",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.0.2-preview\\amazon.lambda.testtool\\0.0.2-preview\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::DivideFunctionHandler",
"commandLineArgs": "exec --depsfile ./WebCalculatorFunctions.deps.json --runtimeconfig ./WebCalculatorFunctions.runtimeconfig.json %USERPROFILE%\\.dotnet\\tools\\.store\\amazon.lambda.testtool\\0.10.0\\amazon.lambda.testtool\\0.10.0\\content\\Amazon.Lambda.RuntimeSupport\\net8.0\\Amazon.Lambda.RuntimeSupport.dll WebCalculatorFunctions::WebCalculatorFunctions.Functions::DivideFunctionHandler",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "dotnet"
},
"Mock Lambda Test Tool": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor

@GarrettBeatty GarrettBeatty Apr 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this auto generated one from the aws toolkit will end up confusing customers - we should probably see about getting rid of this eventually sooner than later

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

"commandName": "Executable",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\\bin\\$(Configuration)\\net8.0",
"executablePath": "%USERPROFILE%\\.dotnet\\tools\\dotnet-lambda-test-tool-8.0.exe"
}
}
}
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.AWS/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ internal static class Constants
/// <summary>
/// The default version of Amazon.Lambda.TestTool that will be automatically installed
/// </summary>
internal const string DefaultLambdaTestToolVersion = "0.9.1";
internal const string DefaultLambdaTestToolVersion = "0.10.0";
}
Loading
Loading