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 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
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"
]
}
]
}
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*.userosscache
*.sln.docstates

playground/**/launchSettings.json

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

Expand Down Expand Up @@ -402,4 +404,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;
}
}
24 changes: 24 additions & 0 deletions playground/Lambda/SQSProcessorFunction/SQSProcessorFunction.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<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>
<ItemGroup>
<Folder Include="Properties\" />
</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"
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\Lambda.ServiceDefaults\Lambda.ServiceDefaults.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
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";
}
116 changes: 116 additions & 0 deletions src/Aspire.Hosting.AWS/Lambda/SQSEventSourceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

using Amazon.CDK.AWS.Events.Targets;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.AWS;
using Aspire.Hosting.AWS.CDK;
using Aspire.Hosting.AWS.CloudFormation;
using Aspire.Hosting.AWS.Lambda;
using Constructs;
using System.Runtime.Versioning;
using System.Text;

#pragma warning disable IDE0130
namespace Aspire.Hosting;

/// <summary>
/// Extension methods adding SQS event source for Lambda functions.
/// </summary>
[RequiresPreviewFeatures(Constants.LambdaPreviewMessage)]
public static class SQSEventSourceExtensions
{
/// <summary>
/// Add an SQS event source to a Lambda function. This feature emulates adding an SQS event source to a Lambda function when deployed to AWS.
/// A separate sub resource will be added to the .NET Aspire application that polls the SQS queue. As messages
/// are received from the queue the Lambda function will be invoked with the messages.
/// </summary>
/// <param name="lambdaFunction">The Lambda function to add the event source to.</param>
/// <param name="queueUrl">The queue url for an SQS queue that will be polled for messages.</param>
/// <param name="options">Optional configuration for the event source.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static IResourceBuilder<LambdaProjectResource> WithSQSEventSource(this IResourceBuilder<LambdaProjectResource> lambdaFunction, string queueUrl, SQSEventSourceOptions? options = null)
{
return WithSQSEventSource(lambdaFunction, () => ValueTask.FromResult(queueUrl), options);
}

/// <summary>
/// Add an SQS event source to a Lambda function. This feature emulates adding an SQS event source to a Lambda function when deployed to AWS.
/// A separate sub resource will be added to the .NET Aspire application that polls the SQS queue. As messages
/// are received from the queue the Lambda function will be invoked with the messages.
/// </summary>
/// <param name="lambdaFunction">The Lambda function to add the event source to.</param>
/// <param name="queue">CDK SQS queue construct that will be polled for messages.</param>
/// <param name="options">Optional configuration for the event source.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static IResourceBuilder<LambdaProjectResource> WithSQSEventSource(this IResourceBuilder<LambdaProjectResource> lambdaFunction, IResourceBuilder<IConstructResource<Amazon.CDK.AWS.SQS.Queue>> queue, SQSEventSourceOptions? options = null)
{
var queueOutputReference = queue.GetOutput("QueueUrl", queue => queue.QueueUrl);
return WithSQSEventSource(lambdaFunction, queueOutputReference, options);
}

/// <summary>
/// Add an SQS event source to a Lambda function. This feature emulates adding an SQS event source to a Lambda function when deployed to AWS.
/// A separate sub resource will be added to the .NET Aspire application that polls the SQS queue. As messages
/// are received from the queue the Lambda function will be invoked with the messages.
/// </summary>
/// <param name="lambdaFunction">The Lambda function to add the event source to.</param>
/// <param name="queueCfnOutputReference">CloudFormation StackOutputReference that should point to a SQS queue url output parameter in the CloudFormation stack.</param>
/// <param name="options">Optional configuration for the event source.</param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static IResourceBuilder<LambdaProjectResource> WithSQSEventSource(this IResourceBuilder<LambdaProjectResource> lambdaFunction, StackOutputReference queueCfnOutputReference, SQSEventSourceOptions? options = null)
{
Func<ValueTask<string>> resolver = async () =>
{
var queueUrl = await queueCfnOutputReference.GetValueAsync();
if (string.IsNullOrEmpty(queueUrl))
{
throw new InvalidOperationException("Output parameter for queue url failed to resolve");
}

if (!Uri.TryCreate(queueUrl, UriKind.Absolute, out _))
{
throw new InvalidOperationException($"Output parameter value {queueUrl} is not a SQS queue url.");
}

return queueUrl;
};

return WithSQSEventSource(lambdaFunction, resolver, options);
}

private static IResourceBuilder<LambdaProjectResource> WithSQSEventSource(IResourceBuilder<LambdaProjectResource> lambdaFunction, Func<ValueTask<string>> queueUrlResolver, SQSEventSourceOptions? options = null)
{
var sqsEventSourceResource = lambdaFunction.ApplicationBuilder.AddResource(new SQSEventSourceResource($"SQSEventSource-{lambdaFunction.Resource.Name}"))
.WithParentRelationship(lambdaFunction)
.ExcludeFromManifest();

sqsEventSourceResource.WithArgs(context =>
{
sqsEventSourceResource.Resource.AddCommandLineArguments(context.Args);
});

sqsEventSourceResource.WithEnvironment(async (context) =>
{
LambdaEmulatorAnnotation? lambdaEmulatorAnnotation = null;
if (lambdaFunction.ApplicationBuilder.Resources.FirstOrDefault(x => x.TryGetLastAnnotation<LambdaEmulatorAnnotation>(out lambdaEmulatorAnnotation)) == null ||
lambdaEmulatorAnnotation == null)
{
throw new InvalidOperationException("Lambda function is missing required annotations for Lambda emulator");
}

var queueUrl = await queueUrlResolver();

// Look to see if the Lambda function has been configured with an AWS SDK config. If so then
// configure the SQS event source with the same config to access the SQS queue.
var awsSdkConfig = lambdaFunction.Resource.Annotations.OfType<SDKResourceAnnotation>().FirstOrDefault()?.SdkConfig;

var sqsEventConfig = SQSEventSourceResource.CreateSQSEventConfig(queueUrl, lambdaFunction.Resource.Name, lambdaEmulatorAnnotation.Endpoint.Url, options, awsSdkConfig);
context.EnvironmentVariables[SQSEventSourceResource.SQS_EVENT_CONFIG_ENV_VAR] = sqsEventConfig;
});

return lambdaFunction;
}
}
Loading
Loading