|
| 1 | +--- |
| 2 | +name: new-event-source |
| 3 | +description: Add a new AWS event source attribute (e.g., Kinesis, Kafka, MQ) to the Lambda .NET Annotations framework, including the attribute class, source generator integration, CloudFormation writer, unit tests, writer tests, source generator tests, and integration tests |
| 4 | +--- |
| 5 | + |
| 6 | +# Adding a New Event Source to Lambda Annotations |
| 7 | + |
| 8 | +This skill guides you through adding a complete new event source attribute to the AWS Lambda .NET Annotations framework. Use this when a user asks to add support for a new AWS event source like Kinesis, Kafka, MQ, etc. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before starting, gather from the user: |
| 13 | +1. **Service name** (e.g., "Kinesis", "Kafka", "MQ") |
| 14 | +2. **Primary resource identifier** (e.g., stream ARN, topic ARN, broker ARN) |
| 15 | +3. **CloudFormation event type string** (e.g., "Kinesis", "MSK", "MQ") |
| 16 | +4. **Event class name** from the corresponding `Amazon.Lambda.*Events` NuGet package (e.g., `KinesisEvent`) |
| 17 | +5. **Optional properties** the attribute should support (e.g., BatchSize, StartingPosition, Filters) |
| 18 | +6. **Whether `@` references use `Fn::GetAtt` or `Ref`** — event source mappings use `Fn::GetAtt`, subscriptions use `Ref` |
| 19 | + |
| 20 | +## Reference Examples |
| 21 | + |
| 22 | +Read these files to understand existing patterns before creating new ones: |
| 23 | +- **SNS (simplest, subscription-based)**: `Libraries/src/Amazon.Lambda.Annotations/SNS/SNSEventAttribute.cs` |
| 24 | +- **SQS (event source mapping with batching)**: `Libraries/src/Amazon.Lambda.Annotations/SQS/SQSEventAttribute.cs` |
| 25 | +- **DynamoDB (stream-based)**: `Libraries/src/Amazon.Lambda.Annotations/DynamoDB/DynamoDBEventAttribute.cs` |
| 26 | +- **S3 (notification-based)**: `Libraries/src/Amazon.Lambda.Annotations/S3/S3EventAttribute.cs` |
| 27 | + |
| 28 | +Also see `Libraries/src/Amazon.Lambda.Annotations/ADDING_NEW_EVENT_SOURCE.md` for the full detailed developer guide. |
| 29 | + |
| 30 | +## Steps |
| 31 | + |
| 32 | +### Step 1: Create the Event Attribute Class |
| 33 | + |
| 34 | +**Create**: `Libraries/src/Amazon.Lambda.Annotations/{ServiceName}/{ServiceName}EventAttribute.cs` |
| 35 | + |
| 36 | +Key patterns: |
| 37 | +- Add copyright header: `// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.` + `// SPDX-License-Identifier: Apache-2.0` |
| 38 | +- Inherit from `Attribute` with `[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]` |
| 39 | +- Constructor takes the primary resource identifier as a required `string` parameter |
| 40 | +- All optional properties use nullable backing fields with `Is<PropertyName>Set` internal properties |
| 41 | +- Include auto-derived `ResourceName` property (strips `@` prefix or extracts name from ARN) |
| 42 | +- Include `internal List<string> Validate()` method with all validation rules |
| 43 | +- Use `Regex("^[a-zA-Z0-9]+$")` for ResourceName validation |
| 44 | + |
| 45 | +### Step 2: Register Type Full Names |
| 46 | + |
| 47 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs` |
| 48 | + |
| 49 | +Add constants: |
| 50 | +```csharp |
| 51 | +public const string {ServiceName}EventAttribute = "Amazon.Lambda.Annotations.{ServiceName}.{ServiceName}EventAttribute"; |
| 52 | +public const string {ServiceName}Event = "Amazon.Lambda.{ServiceName}Events.{ServiceName}Event"; |
| 53 | +``` |
| 54 | + |
| 55 | +Also add to `EventType` enum if needed in `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventType.cs`. |
| 56 | + |
| 57 | +### Step 3: Create the Attribute Builder |
| 58 | + |
| 59 | +**Create**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/{ServiceName}EventAttributeBuilder.cs` |
| 60 | + |
| 61 | +Extracts attribute data from Roslyn `AttributeData`. Use consistent `else if` chaining. Reference: `SNSEventAttributeBuilder.cs`. |
| 62 | + |
| 63 | +### Step 4: Register in AttributeModelBuilder |
| 64 | + |
| 65 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs` |
| 66 | + |
| 67 | +Add `else if` block for the new attribute type after the existing event attribute blocks. |
| 68 | + |
| 69 | +### Step 5: Register in EventTypeBuilder |
| 70 | + |
| 71 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs` |
| 72 | + |
| 73 | +Add `else if` block mapping the attribute to the `EventType` enum value. |
| 74 | + |
| 75 | +### Step 6: Add DiagnosticDescriptor |
| 76 | + |
| 77 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs` |
| 78 | + |
| 79 | +Add descriptor with the next available `AWSLambda0XXX` ID for invalid attribute validation errors. |
| 80 | + |
| 81 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md` — add the new diagnostic ID. |
| 82 | + |
| 83 | +### Step 7: Add Validation in LambdaFunctionValidator |
| 84 | + |
| 85 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs` |
| 86 | + |
| 87 | +1. Add `Validate{ServiceName}Events()` call in `ValidateFunction` method |
| 88 | +2. Create private `Validate{ServiceName}Events()` method that validates: |
| 89 | + - Attribute properties via `Validate()` method |
| 90 | + - Method parameters (first must be event type, optional second is `ILambdaContext`) |
| 91 | + - Return type (usually `void` or `Task`) |
| 92 | + |
| 93 | +### Step 8: Add Dependency Check |
| 94 | + |
| 95 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs` |
| 96 | + |
| 97 | +In `ValidateDependencies`, add check for `Amazon.Lambda.{ServiceName}Events` NuGet package. |
| 98 | + |
| 99 | +### Step 9: Check SyntaxReceiver |
| 100 | + |
| 101 | +**Check**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs` |
| 102 | + |
| 103 | +Add the new attribute name if the SyntaxReceiver filters by attribute name strings. |
| 104 | + |
| 105 | +### Step 10: Add CloudFormation Writer Logic |
| 106 | + |
| 107 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs` |
| 108 | + |
| 109 | +1. Add `case AttributeModel<{ServiceName}EventAttribute>` in the event processing switch |
| 110 | +2. Create `Process{ServiceName}Attribute()` method that writes CF template properties |
| 111 | + - Event source mappings (SQS, DynamoDB, Kinesis): use `Fn::GetAtt` for `@` references |
| 112 | + - Subscription events (SNS): use `Ref` for `@` references |
| 113 | + - Track synced properties in metadata |
| 114 | + |
| 115 | +### Step 11: Create Attribute Unit Tests |
| 116 | + |
| 117 | +**Create**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/{ServiceName}EventAttributeTests.cs` |
| 118 | + |
| 119 | +Cover: constructor, defaults, property tracking, ResourceName derivation, all validation paths. Reference: `SQSEventAttributeTests.cs`, `DynamoDBEventAttributeTests.cs`, `SNSEventAttributeTests.cs`. |
| 120 | + |
| 121 | +### Step 12: Create CloudFormation Writer Tests |
| 122 | + |
| 123 | +**Create**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/{ServiceName}EventsTests.cs` |
| 124 | + |
| 125 | +This is a `partial class CloudFormationWriterTests`. Include tests for: |
| 126 | +1. `Verify{ServiceName}EventAttributes_AreCorrectlyApplied` — Theory with JSON/YAML and property combinations |
| 127 | +2. `Verify{ServiceName}EventProperties_AreSyncedCorrectly` — Synced properties update when attributes change |
| 128 | +3. `SwitchBetweenArnAndRef_For{Resource}` — ARN to `@` reference switching |
| 129 | +4. `Verify{Resource}CanBeSet_FromCloudFormationParameter` — CF Parameters handling |
| 130 | +5. `VerifyManuallySet{ServiceName}EventProperties_ArePreserved` — Hand-edited template preservation |
| 131 | + |
| 132 | +Reference: `SQSEventsTests.cs`, `DynamoDBEventsTests.cs`, `SNSEventsTests.cs`. |
| 133 | + |
| 134 | +### Step 13: Create Valid Event Examples + Source Generator Test |
| 135 | + |
| 136 | +**Create**: `Libraries/test/TestServerlessApp/{ServiceName}EventExamples/Valid{ServiceName}Events.cs.txt` |
| 137 | +**Create**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/{ServiceName}/` (generated handler snapshots) |
| 138 | +**Create**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/{serviceName}Events.template` |
| 139 | +**Modify**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs` — add `VerifyValid{ServiceName}Events()` test |
| 140 | + |
| 141 | +### Step 14: Create Invalid Event Examples + Source Generator Test |
| 142 | + |
| 143 | +**Create**: `Libraries/test/TestServerlessApp/{ServiceName}EventExamples/Invalid{ServiceName}Events.cs.error` |
| 144 | + |
| 145 | +Cover: invalid property values, invalid params, invalid return type, multiple events, invalid ARN, invalid resource name. |
| 146 | + |
| 147 | +**Modify**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs` — add `VerifyInvalid{ServiceName}Events_ThrowsCompilationErrors()` test with diagnostic assertions including line spans. |
| 148 | + |
| 149 | +### Step 15: Create Generated Code Snapshots |
| 150 | + |
| 151 | +**Create**: `Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/{ServiceName}/` |
| 152 | + |
| 153 | +Tip: Run the source generator once to get actual output, then use as snapshot. |
| 154 | + |
| 155 | +### Step 16: Create Integration Test |
| 156 | + |
| 157 | +**Create**: `Libraries/test/TestServerlessApp.IntegrationTests/{ServiceName}EventSourceMapping.cs` |
| 158 | +**Modify**: `Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs` — resource lookup |
| 159 | +**Modify**: `Libraries/test/TestServerlessApp.IntegrationTests/DeploymentScript.ps1` — if needed |
| 160 | + |
| 161 | +### Step 17: Update AnalyzerReleases.Unshipped.md |
| 162 | + |
| 163 | +**Modify**: `Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md` |
| 164 | + |
| 165 | +## File Map Summary |
| 166 | + |
| 167 | +| Action | File Path | |
| 168 | +|--------|-----------| |
| 169 | +| Create | `src/Amazon.Lambda.Annotations/{ServiceName}/{ServiceName}EventAttribute.cs` | |
| 170 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs` | |
| 171 | +| Create | `src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/{ServiceName}EventAttributeBuilder.cs` | |
| 172 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs` | |
| 173 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs` | |
| 174 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs` | |
| 175 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs` | |
| 176 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs` | |
| 177 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs` | |
| 178 | +| Modify | `src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md` | |
| 179 | +| Create | `test/Amazon.Lambda.Annotations.SourceGenerators.Tests/{ServiceName}EventAttributeTests.cs` | |
| 180 | +| Create | `test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/{ServiceName}EventsTests.cs` | |
| 181 | +| Create | `test/TestServerlessApp/{ServiceName}EventExamples/Valid{ServiceName}Events.cs.txt` | |
| 182 | +| Create | `test/TestServerlessApp/{ServiceName}EventExamples/Invalid{ServiceName}Events.cs.error` | |
| 183 | +| Create | `test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/{ServiceName}/` | |
| 184 | +| Create | `test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/{serviceName}Events.template` | |
| 185 | +| Modify | `test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs` | |
| 186 | +| Create | `test/TestServerlessApp.IntegrationTests/{ServiceName}EventSourceMapping.cs` | |
| 187 | +| Modify | `test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs` | |
| 188 | + |
| 189 | +## Important Conventions |
| 190 | + |
| 191 | +- **Copyright header** on every new `.cs` file |
| 192 | +- **Consistent `else if` chaining** in attribute builders (never `if` then `if` for the same loop) |
| 193 | +- **Both JSON and YAML** template formats must be tested in writer tests |
| 194 | +- **Invalid event test spans** must reference exact line numbers in the `.cs.error` file |
| 195 | +- **`.cs.txt` extension** for valid test files (prevents deployment) |
| 196 | +- **`.cs.error` extension** for invalid test files (prevents compilation) |
0 commit comments