Skip to content

Add [ScheduleEvent] annotation attribute and source generator support#2323

Open
GarrettBeatty wants to merge 1 commit intofeature/dynamodb-annotationsfrom
feature/schedule-annotations
Open

Add [ScheduleEvent] annotation attribute and source generator support#2323
GarrettBeatty wants to merge 1 commit intofeature/dynamodb-annotationsfrom
feature/schedule-annotations

Conversation

@GarrettBeatty
Copy link
Copy Markdown
Contributor

@GarrettBeatty GarrettBeatty commented Apr 3, 2026

Summary

Adds [ScheduleEvent] annotation attribute support to the Lambda Annotations framework, enabling developers to declaratively configure schedule-triggered Lambda functions directly in C# code using rate or cron expressions. The source generator automatically produces the corresponding SAM/CloudFormation template configuration at build time.

User Experience

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

[LambdaFunction]
[ScheduleEvent("rate(5 minutes)")]
public async Task ProcessScheduledEvent(ScheduledEvent evnt)
{
    // Handle scheduled invocation
}

The source generator will automatically generate the SAM template entry:

ProcessScheduledEvent:
  Type: AWS::Serverless::Function
  Properties:
    Events:
      rate5minutes:
        Type: Schedule
        Properties:
          Schedule: rate(5 minutes)

Attribute Properties

Property Required Description Default
Schedule Yes Schedule expression (rate(...) or cron(...)) -
ResourceName No CloudFormation event resource name Derived from schedule expression
Description No Description for the schedule rule Not set
Input No JSON string to pass as input to the Lambda function Not set
Enabled No Whether the event source is enabled true

Compile-Time Validation

The source generator validates at build time:

  • Schedule expression: Must start with rate( or cron(
  • Method signature: First parameter must be ScheduledEvent, optional second parameter must be ILambdaContext
  • Return type: Must be void or Task
  • Dependencies: Project must reference Amazon.Lambda.CloudWatchEvents NuGet package
  • Resource name: Must be alphanumeric if explicitly set

Example with all properties

[LambdaFunction]
[ScheduleEvent("cron(0 12 * * ? *)",
    ResourceName = "DailyCleanup",
    Description = "Runs daily at noon UTC",
    Input = "{\"action\": \"cleanup\"}",
    Enabled = true)]
public async Task ProcessScheduledEvent(ScheduledEvent evnt, ILambdaContext context)
{
    context.Logger.LogLine("Running scheduled cleanup");
}

What Changed

Annotation Attribute (Amazon.Lambda.Annotations)

  • New ScheduleEventAttribute class in Amazon.Lambda.Annotations.Schedule namespace with configurable properties and built-in validation

Source Generator (Amazon.Lambda.Annotations.SourceGenerator)

  • ScheduleEventAttributeBuilder — extracts attribute data from Roslyn syntax tree
  • AttributeModelBuilder — recognizes and routes ScheduleEvent attributes
  • EventTypeBuilder — maps to EventType.Schedule
  • SyntaxReceiver — registers ScheduleEvent as a recognized attribute
  • TypeFullNames — adds Schedule type constants
  • LambdaFunctionValidator — validates method signatures, return types, dependencies, and attribute properties
  • CloudFormationWriter.ProcessScheduleAttribute() — generates SAM template with Schedule expression, Description, Input, and Enabled
  • New diagnostic AWSLambda0132 for invalid ScheduleEventAttribute errors

Tests

  • Attribute unit tests covering constructor, defaults, property tracking, and validation
  • CloudFormation writer tests covering attribute configurations and template formats
  • E2E source generator snapshot tests (sync + async)
  • Integration test deploying a real schedule-triggered Lambda and verifying EventBridge rule

Related: DOTNET-8574

@GarrettBeatty GarrettBeatty force-pushed the feature/schedule-annotations branch 3 times, most recently from b1c1d26 to 53f2edd 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 [ScheduleEvent] support to the Lambda Annotations framework so schedule-triggered Lambdas (rate/cron) can be declared in C# and emitted into the generated SAM/CloudFormation template.

Changes:

  • Introduces ScheduleEventAttribute (schedule expression + optional ResourceName/Description/Input/Enabled).
  • Extends the source generator to recognize/validate schedule events and emit Type: Schedule event configuration in the template.
  • Adds unit, snapshot, and integration tests plus docs/examples for schedule events.

Reviewed changes

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

Show a summary per file
File Description
Libraries/test/TestServerlessApp/TestServerlessApp.csproj Adds CloudWatchEvents project reference for schedule event sample app.
Libraries/test/TestServerlessApp/ScheduledProcessing.cs Adds an example scheduled handler Lambda using [ScheduleEvent].
Libraries/test/TestServerlessApp/ScheduleEventExamples/ValidScheduleEvents.cs.txt Adds valid schedule event usages for generator snapshot tests (kept as .txt).
Libraries/test/TestServerlessApp.IntegrationTests/TestServerlessApp.IntegrationTests.csproj Adds AWSSDK.CloudWatchEvents dependency for schedule integration testing.
Libraries/test/TestServerlessApp.IntegrationTests/ScheduleEventRule.cs Adds integration test to validate the deployed EventBridge rule configuration.
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs Updates expected Lambda function count to include the new scheduled handler.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/ScheduleEventsTests.cs Adds CloudFormation writer tests for schedule event emission and property syncing.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs Adds snapshot-based source generator test for valid schedule events.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/scheduleEvents.template Adds expected generated serverless template snapshot for schedule events.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/Schedule/ValidScheduleEvents_ProcessScheduledEvent_Generated.g.cs Adds expected generated handler snapshot (sync).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/Schedule/ValidScheduleEvents_ProcessScheduledEventAsync_Generated.g.cs Adds expected generated handler snapshot (async).
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/ScheduleEventAttributeTests.cs Adds unit tests for ScheduleEventAttribute defaults/derivations/validation.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs Adds metadata reference for ScheduledEvent type in the Roslyn test harness.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Amazon.Lambda.Annotations.SourceGenerators.Tests.csproj Adds CloudWatchEvents project reference for tests.
Libraries/src/Amazon.Lambda.Annotations/Schedule/ScheduleEventAttribute.cs Introduces the new ScheduleEventAttribute implementation + validation.
Libraries/src/Amazon.Lambda.Annotations/README.md Documents ScheduleEvent usage and updates supported-attributes list.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs Emits Schedule SAM event entries for [ScheduleEvent].
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs Adds dependency checks + signature/return/attribute validation for schedule handlers.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs Adds full-name constants for ScheduleEventAttribute and ScheduledEvent.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs Recognizes ScheduleEventAttribute during syntax collection.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs Maps ScheduleEventAttribute to EventType.Schedule.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/ScheduleEventAttributeBuilder.cs Builds ScheduleEventAttribute models from Roslyn attribute data.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs Routes ScheduleEventAttribute to the new builder/model type.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs Adds a new diagnostic descriptor for invalid ScheduleEventAttribute usage.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md Records the new diagnostic ID in analyzer release notes.
.autover/changes/add-scheduleevent-annotation.json Adds versioning/changelog entry for the new feature.

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

Comment thread Libraries/test/TestServerlessApp.IntegrationTests/ScheduleEventRule.cs Outdated
Comment thread Libraries/src/Amazon.Lambda.Annotations/Schedule/ScheduleEventAttribute.cs Outdated
@GarrettBeatty GarrettBeatty changed the base branch from dev to feature/dynamodb-annotations April 17, 2026 16:11
@GarrettBeatty GarrettBeatty force-pushed the feature/schedule-annotations branch from 53f2edd to ae98bbc Compare April 17, 2026 16:11
@GarrettBeatty GarrettBeatty requested a review from Copilot April 17, 2026 18:03
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

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


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

- ScheduleEventAttribute with Schedule (rate/cron), ResourceName, Description, Input, Enabled
- ScheduleEventAttributeBuilder for Roslyn AttributeData parsing
- Source generator wiring (TypeFullNames, SyntaxReceiver, EventTypeBuilder, AttributeModelBuilder)
- CloudFormationWriter ProcessScheduleAttribute (SAM Schedule event rule)
- LambdaFunctionValidator ValidateScheduleEvents
- DiagnosticDescriptors InvalidScheduleEventAttribute
- ScheduleEventAttributeTests (attribute unit tests)
- ScheduleEventsTests (CloudFormation writer tests)
- E2E source generator snapshot tests
- Integration test (ScheduleEventRule)
- Sample function (ScheduledProcessing)
- .autover change file
- README documentation

update

pr comments

update test

update test
@GarrettBeatty GarrettBeatty force-pushed the feature/schedule-annotations branch from c528158 to 05e42e2 Compare April 17, 2026 18:16
@GarrettBeatty GarrettBeatty marked this pull request as ready for review April 17, 2026 18:37
@GarrettBeatty GarrettBeatty requested a review from a team as a code owner April 17, 2026 18:37
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.

2 participants