Skip to content

Add [SNSEvent] annotation attribute and source generator support#2322

Open
GarrettBeatty wants to merge 6 commits intodevfrom
feature/sns-annotations
Open

Add [SNSEvent] annotation attribute and source generator support#2322
GarrettBeatty wants to merge 6 commits intodevfrom
feature/sns-annotations

Conversation

@GarrettBeatty
Copy link
Copy Markdown
Contributor

@GarrettBeatty GarrettBeatty commented Apr 3, 2026

Summary

Adds [SNSEvent] annotation attribute support to the Lambda Annotations framework, enabling developers to declaratively configure SNS topic-triggered Lambda functions directly in C# code. The source generator automatically produces the corresponding SAM/CloudFormation template configuration at build time.

User Experience

With this change, developers can write SNS-triggered Lambda functions like this:

[LambdaFunction]
[SNSEvent("@MyTopic")]
public async Task ProcessMessages(SNSEvent evnt)
{
    foreach (var record in evnt.Records)
    {
        // Handle SNS messages
    }
}

The source generator will automatically generate the SAM template entry:

ProcessMessages:
  Type: AWS::Serverless::Function
  Properties:
    Events:
      MyTopic:
        Type: SNS
        Properties:
          Topic: !Ref MyTopic

Attribute Properties

Property Required Description Default
Topic Yes SNS topic reference (@TopicName for CloudFormation Ref, or topic ARN) -
ResourceName No CloudFormation event resource name Derived from topic name
FilterPolicy No JSON filter policy for the subscription Not set
Enabled No Whether the event source is enabled true

The implementation is pretty similar to sqs event

Related: DOTNET-8573

@GarrettBeatty GarrettBeatty force-pushed the feature/sns-annotations branch 3 times, most recently from acfc470 to 53e14ec Compare April 13, 2026 18:09
@GarrettBeatty GarrettBeatty requested a review from Copilot April 13, 2026 18:38
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds first-class SNS event support to the Lambda Annotations framework by introducing a new [SNSEvent] attribute, validating its usage at compile time, and emitting the corresponding SAM/CloudFormation SNS event configuration via the source generator.

Changes:

  • Introduces SNSEventAttribute (with Topic/ResourceName/FilterPolicy/Enabled) and updates docs/examples.
  • Extends the source generator pipeline (syntax discovery, modeling, validation, template writing) to support SNS events and diagnostics.
  • Adds unit tests, snapshot tests, and an integration test to validate generated templates and real SNS subscription configuration.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
Libraries/test/TestServerlessApp/TestServerlessApp.csproj Adds SNSEvents project reference for test app compilation.
Libraries/test/TestServerlessApp/SnsMessageProcessing.cs Adds an SNS-triggered Lambda handler using [SNSEvent].
Libraries/test/TestServerlessApp/SNSEventExamples/ValidSNSEvents.cs.txt Adds valid SNS examples used as generator test input (not deployed).
Libraries/test/TestServerlessApp.IntegrationTests/SNSEventSubscription.cs Adds integration test to verify SNS subscription + filter policy.
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs Captures SNS Topic ARN from the deployed stack; updates expected Lambda count.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/SNSEventsTests.cs Adds CloudFormation writer tests for SNS event emission and syncing behavior.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs Adds end-to-end snapshot-based generator test for SNS events.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/snsEvents.template Adds expected generated serverless template snapshot for SNS events.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/SNS/ValidSNSEvents_ProcessMessages_Generated.g.cs Adds expected generated handler snapshot (sync).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/SNS/ValidSNSEvents_ProcessMessagesAsync_Generated.g.cs Adds expected generated handler snapshot (async).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SNSEventAttributeTests.cs Adds unit tests for SNSEventAttribute defaults and validation.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs Adds SNSEvents metadata reference for compilation in generator tests.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Amazon.Lambda.Annotations.SourceGenerators.Tests.csproj Adds SNSEvents project reference for test project.
Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs Introduces the public [SNSEvent] attribute and internal validation helpers.
Libraries/src/Amazon.Lambda.Annotations/README.md Documents SNS event usage and adds an SNS example section.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs Emits Type: SNS event definitions and associated properties into templates.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs Adds SNS signature/return-type/attribute validation and dependency check.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs Adds SNS type/attribute full names and registers SNS attribute in known list.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs Recognizes SNSEventAttribute during syntax collection.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs Maps SNSEventAttribute to EventType.SNS.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventType.cs Adds the SNS enum value.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/SNSEventAttributeBuilder.cs Builds SNSEventAttribute models from Roslyn attribute data.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs Routes SNSEventAttribute into the attribute model pipeline.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs Adds a diagnostic descriptor for invalid SNSEventAttribute usage.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md Registers the new SNS diagnostic in analyzer releases.
.autover/changes/add-snsevent-annotation.json Adds a minor-version changelog entry for the new SNS annotation support.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs
Comment thread Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs
Comment thread Libraries/test/TestServerlessApp.IntegrationTests/SNSEventSubscription.cs Outdated
Comment thread Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs Outdated
- SNSEventAttribute with Topic, ResourceName, FilterPolicy, Enabled
- SNSEventAttributeBuilder for Roslyn AttributeData parsing
- Source generator wiring (TypeFullNames, SyntaxReceiver, EventTypeBuilder, AttributeModelBuilder)
- CloudFormationWriter ProcessSNSAttribute (SAM SNS event subscription)
- LambdaFunctionValidator ValidateSNSEvents
- DiagnosticDescriptors InvalidSNSEventAttribute
- SNSEventAttributeTests (attribute unit tests)
- SNSEventsTests (CloudFormation writer tests)
- E2E source generator snapshot tests
- Integration test (SNSEventSubscription)
- Sample function (SnsMessageProcessing)
- .autover change file
- README documentation
public bool Enabled
{
get => enabled.GetValueOrDefault();
get => enabled.GetValueOrDefault(true);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

i think this was a bug. default should be true as per the docs. right now it returns false by default

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Call that out in the changelog

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds first-class SNS event support to the Lambda Annotations framework by introducing a new [SNSEvent] attribute and extending the source generator to validate SNS handlers and emit the corresponding SAM Events configuration.

Changes:

  • Introduces Amazon.Lambda.Annotations.SNS.SNSEventAttribute with optional ResourceName, FilterPolicy, and Enabled.
  • Extends the source generator to recognize/validate [SNSEvent] and write Type: SNS events into generated templates.
  • Adds unit, snapshot, writer, and integration tests plus documentation updates for SNS usage.

Reviewed changes

Copilot reviewed 27 out of 27 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Libraries/test/TestServerlessApp/TestServerlessApp.csproj Adds SNSEvents project reference for test app compilation.
Libraries/test/TestServerlessApp/SnsMessageProcessing.cs Adds a deployable SNS-triggered handler example for integration testing.
Libraries/test/TestServerlessApp/SNSEventExamples/ValidSNSEvents.cs.txt Adds source input for SNS-related generator snapshot tests (not deployed).
Libraries/test/TestServerlessApp.IntegrationTests/SNSEventSubscription.cs Adds integration test verifying SNS subscription + filter policy.
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs Captures SNS topic ARN from the deployed stack for tests.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/SNSEventsTests.cs Adds CloudFormation writer tests for SNS event serialization + syncing.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs Adds snapshot-based generator test coverage for valid SNS handlers.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/snsEvents.template Adds expected serverless template snapshot for SNS events.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/SNS/ValidSNSEvents_ProcessMessages_Generated.g.cs Adds expected generated handler snapshot (sync).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/SNS/ValidSNSEvents_ProcessMessagesAsync_Generated.g.cs Adds expected generated handler snapshot (async).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SNSEventAttributeTests.cs Adds unit tests for SNSEventAttribute defaults/validation.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs Adds SNSEvent assembly reference for Roslyn test compilation.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Amazon.Lambda.Annotations.SourceGenerators.Tests.csproj Adds SNSEvents project reference for test project.
Libraries/src/Amazon.Lambda.Annotations/SQS/SQSEventAttribute.cs Fixes SQS Enabled default to true.
Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs Adds the new SNSEventAttribute API and validation helpers.
Libraries/src/Amazon.Lambda.Annotations/README.md Documents SNS event usage and adds SNSEvent to attribute list.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs Emits SNS SAM event entries and tracks synced properties.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs Adds SNS validation (dependencies, signature, return type, attribute validation).
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs Registers SNSEvent and SNSEventAttribute full names.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs Recognizes SNSEventAttribute as a “secondary” attribute requiring [LambdaFunction].
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs Maps SNSEventAttribute to EventType.SNS.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventType.cs Adds SNS to the event type enum.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/SNSEventAttributeBuilder.cs Builds SNSEventAttribute instances from Roslyn AttributeData.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs Routes SNSEventAttribute through the model builder pipeline.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs Adds Invalid SNSEventAttribute diagnostic descriptor.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md Registers the new SNS diagnostic in unshipped analyzer releases.
.autover/changes/add-snsevent-annotation.json Adds autover changelog entry for the new SNSEvent feature.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs
Comment thread Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs
@GarrettBeatty GarrettBeatty marked this pull request as ready for review April 15, 2026 14:51
@GarrettBeatty GarrettBeatty requested a review from a team as a code owner April 15, 2026 14:51
@@ -0,0 +1,40 @@
using Amazon.Lambda.Annotations.SNS;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add license header

@@ -0,0 +1,113 @@
using System;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add license header

public bool Enabled
{
get => enabled.GetValueOrDefault();
get => enabled.GetValueOrDefault(true);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Call that out in the changelog

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants