Skip to content

Commit 39d776c

Browse files
committed
Add [DynamoDBEvent] annotation attribute and source generator support
- DynamoDBEventAttribute with Stream, ResourceName, BatchSize, StartingPosition, MaximumBatchingWindowInSeconds, Filters, Enabled - DynamoDBEventAttributeBuilder for Roslyn AttributeData parsing - Source generator wiring (TypeFullNames, SyntaxReceiver, EventTypeBuilder, AttributeModelBuilder) - CloudFormationWriter ProcessDynamoDBAttribute (SAM DynamoDB event source mapping) - LambdaFunctionValidator ValidateDynamoDBEvents - DiagnosticDescriptors InvalidDynamoDBEventAttribute (AWSLambda0132) - DynamoDBEventAttributeTests (attribute unit tests) - DynamoDBEventsTests (CloudFormation writer tests) - E2E source generator snapshot tests - Integration test (DynamoDBEventSourceMapping) - Sample function (DynamoDbStreamProcessing) - .autover change file - README documentation
1 parent 78afc7a commit 39d776c

27 files changed

Lines changed: 1328 additions & 3 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.Annotations",
5+
"Type": "Minor",
6+
"ChangelogMessages": [
7+
"Added [DynamoDBEvent] annotation attribute for declaratively configuring DynamoDB stream-triggered Lambda functions with support for stream reference, batch size, starting position, batching window, filters, and enabled state."
8+
]
9+
}
10+
]
11+
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ AWSLambda0133 | AWSLambdaCSharpGenerator | Error | ALB Listener Reference Not Fo
2121
AWSLambda0134 | AWSLambdaCSharpGenerator | Error | FromRoute not supported on ALB functions
2222
AWSLambda0135 | AWSLambdaCSharpGenerator | Error | Unmapped parameter on ALB function
2323
AWSLambda0136 | AWSLambdaCSharpGenerator | Error | Invalid S3EventAttribute
24+
AWSLambda0137 | AWSLambdaCSharpGenerator | Error | Invalid DynamoDBEventAttribute

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,5 +281,12 @@ public static class DiagnosticDescriptors
281281
category: "AWSLambdaCSharpGenerator",
282282
DiagnosticSeverity.Error,
283283
isEnabledByDefault: true);
284+
285+
public static readonly DiagnosticDescriptor InvalidDynamoDBEventAttribute = new DiagnosticDescriptor(id: "AWSLambda0137",
286+
title: "Invalid DynamoDBEventAttribute",
287+
messageFormat: "Invalid DynamoDBEventAttribute encountered: {0}",
288+
category: "AWSLambdaCSharpGenerator",
289+
DiagnosticSeverity.Error,
290+
isEnabledByDefault: true);
284291
}
285292
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/AttributeModelBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Amazon.Lambda.Annotations.ALB;
3+
using Amazon.Lambda.Annotations.DynamoDB;
34
using Amazon.Lambda.Annotations.APIGateway;
45
using Amazon.Lambda.Annotations.S3;
56
using Amazon.Lambda.Annotations.SQS;
@@ -101,6 +102,15 @@ public static AttributeModel Build(AttributeData att, GeneratorExecutionContext
101102
Type = TypeModelBuilder.Build(att.AttributeClass, context)
102103
};
103104
}
105+
else if (att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(TypeFullNames.DynamoDBEventAttribute), SymbolEqualityComparer.Default))
106+
{
107+
var data = DynamoDBEventAttributeBuilder.Build(att);
108+
model = new AttributeModel<DynamoDBEventAttribute>
109+
{
110+
Data = data,
111+
Type = TypeModelBuilder.Build(att.AttributeClass, context)
112+
};
113+
}
104114
else if (att.AttributeClass.Equals(context.Compilation.GetTypeByMetadataName(TypeFullNames.HttpApiAuthorizerAttribute), SymbolEqualityComparer.Default))
105115
{
106116
var data = HttpApiAuthorizerAttributeBuilder.Build(att);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using Amazon.Lambda.Annotations.DynamoDB;
2+
using Microsoft.CodeAnalysis;
3+
using System;
4+
5+
namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes
6+
{
7+
/// <summary>
8+
/// Builder for <see cref="DynamoDBEventAttribute"/>.
9+
/// </summary>
10+
public class DynamoDBEventAttributeBuilder
11+
{
12+
public static DynamoDBEventAttribute Build(AttributeData att)
13+
{
14+
if (att.ConstructorArguments.Length != 1)
15+
{
16+
throw new NotSupportedException($"{TypeFullNames.DynamoDBEventAttribute} must have constructor with 1 argument.");
17+
}
18+
var stream = att.ConstructorArguments[0].Value as string;
19+
var data = new DynamoDBEventAttribute(stream);
20+
21+
foreach (var pair in att.NamedArguments)
22+
{
23+
if (pair.Key == nameof(data.ResourceName) && pair.Value.Value is string resourceName)
24+
{
25+
data.ResourceName = resourceName;
26+
}
27+
if (pair.Key == nameof(data.BatchSize) && pair.Value.Value is uint batchSize)
28+
{
29+
data.BatchSize = batchSize;
30+
}
31+
else if (pair.Key == nameof(data.StartingPosition) && pair.Value.Value is string startingPosition)
32+
{
33+
data.StartingPosition = startingPosition;
34+
}
35+
else if (pair.Key == nameof(data.Enabled) && pair.Value.Value is bool enabled)
36+
{
37+
data.Enabled = enabled;
38+
}
39+
else if (pair.Key == nameof(data.MaximumBatchingWindowInSeconds) && pair.Value.Value is uint maximumBatchingWindowInSeconds)
40+
{
41+
data.MaximumBatchingWindowInSeconds = maximumBatchingWindowInSeconds;
42+
}
43+
else if (pair.Key == nameof(data.Filters) && pair.Value.Value is string filters)
44+
{
45+
data.Filters = filters;
46+
}
47+
}
48+
49+
return data;
50+
}
51+
}
52+
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/EventTypeBuilder.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public static HashSet<EventType> Build(IMethodSymbol lambdaMethodSymbol,
3030
{
3131
events.Add(EventType.S3);
3232
}
33+
else if (attribute.AttributeClass.ToDisplayString() == TypeFullNames.DynamoDBEventAttribute)
34+
{
35+
events.Add(EventType.DynamoDB);
36+
}
3337
else if (attribute.AttributeClass.ToDisplayString() == TypeFullNames.HttpApiAuthorizerAttribute
3438
|| attribute.AttributeClass.ToDisplayString() == TypeFullNames.RestApiAuthorizerAttribute)
3539
{

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/SyntaxReceiver.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ internal class SyntaxReceiver : ISyntaxContextReceiver
2323
{ "RestApiAttribute", "RestApi" },
2424
{ "SQSEventAttribute", "SQSEvent" },
2525
{ "ALBApiAttribute", "ALBApi" },
26-
{ "S3EventAttribute", "S3Event" }
26+
{ "S3EventAttribute", "S3Event" },
27+
{ "DynamoDBEventAttribute", "DynamoDBEvent" }
2728
};
2829

2930
public List<MethodDeclarationSyntax> LambdaMethods { get; } = new List<MethodDeclarationSyntax>();

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/TypeFullNames.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public static class TypeFullNames
5656
public const string S3Event = "Amazon.Lambda.S3Events.S3Event";
5757
public const string S3EventAttribute = "Amazon.Lambda.Annotations.S3.S3EventAttribute";
5858

59+
public const string DynamoDBEvent = "Amazon.Lambda.DynamoDBEvents.DynamoDBEvent";
60+
public const string DynamoDBEventAttribute = "Amazon.Lambda.Annotations.DynamoDB.DynamoDBEventAttribute";
61+
5962
public const string LambdaSerializerAttribute = "Amazon.Lambda.Core.LambdaSerializerAttribute";
6063
public const string DefaultLambdaSerializer = "Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer";
6164

@@ -84,7 +87,8 @@ public static class TypeFullNames
8487
HttpApiAttribute,
8588
SQSEventAttribute,
8689
ALBApiAttribute,
87-
S3EventAttribute
90+
S3EventAttribute,
91+
DynamoDBEventAttribute
8892
};
8993
}
9094
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Validation/LambdaFunctionValidator.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Amazon.Lambda.Annotations.SourceGenerator.Extensions;
55
using Amazon.Lambda.Annotations.SourceGenerator.Models;
66
using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes;
7+
using Amazon.Lambda.Annotations.DynamoDB;
78
using Amazon.Lambda.Annotations.SQS;
89
using Microsoft.CodeAnalysis;
910
using System.Collections.Generic;
@@ -61,6 +62,7 @@ internal static bool ValidateFunction(GeneratorExecutionContext context, IMethod
6162
// Validate Events
6263
ValidateApiGatewayEvents(lambdaFunctionModel, methodLocation, diagnostics);
6364
ValidateSqsEvents(lambdaFunctionModel, methodLocation, diagnostics);
65+
ValidateDynamoDBEvents(lambdaFunctionModel, methodLocation, diagnostics);
6466
ValidateAlbEvents(lambdaFunctionModel, methodLocation, diagnostics);
6567
ValidateS3Events(lambdaFunctionModel, methodLocation, diagnostics);
6668

@@ -110,6 +112,16 @@ internal static bool ValidateDependencies(GeneratorExecutionContext context, IMe
110112
}
111113
}
112114

115+
// Check for references to "Amazon.Lambda.DynamoDBEvents" if the Lambda method is annotated with DynamoDBEvent attribute.
116+
if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.DynamoDBEventAttribute))
117+
{
118+
if (context.Compilation.ReferencedAssemblyNames.FirstOrDefault(x => x.Name == "Amazon.Lambda.DynamoDBEvents") == null)
119+
{
120+
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MissingDependencies, methodLocation, "Amazon.Lambda.DynamoDBEvents"));
121+
return false;
122+
}
123+
}
124+
113125
return true;
114126
}
115127

@@ -420,6 +432,45 @@ private static void ValidateS3Events(LambdaFunctionModel lambdaFunctionModel, Lo
420432
}
421433
}
422434

435+
private static void ValidateDynamoDBEvents(LambdaFunctionModel lambdaFunctionModel, Location methodLocation, List<Diagnostic> diagnostics)
436+
{
437+
if (!lambdaFunctionModel.LambdaMethod.Events.Contains(EventType.DynamoDB))
438+
{
439+
return;
440+
}
441+
442+
// Validate DynamoDBEventAttributes
443+
foreach (var att in lambdaFunctionModel.Attributes)
444+
{
445+
if (att.Type.FullName != TypeFullNames.DynamoDBEventAttribute)
446+
continue;
447+
448+
var dynamoDBEventAttribute = ((AttributeModel<DynamoDBEventAttribute>)att).Data;
449+
var validationErrors = dynamoDBEventAttribute.Validate();
450+
validationErrors.ForEach(errorMessage => diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidDynamoDBEventAttribute, methodLocation, errorMessage)));
451+
}
452+
453+
// Validate method parameters - When using DynamoDBEventAttribute, the method signature must be (DynamoDBEvent evnt) or (DynamoDBEvent evnt, ILambdaContext context)
454+
var parameters = lambdaFunctionModel.LambdaMethod.Parameters;
455+
if (parameters.Count == 0 ||
456+
parameters.Count > 2 ||
457+
(parameters.Count == 1 && parameters[0].Type.FullName != TypeFullNames.DynamoDBEvent) ||
458+
(parameters.Count == 2 && (parameters[0].Type.FullName != TypeFullNames.DynamoDBEvent || parameters[1].Type.FullName != TypeFullNames.ILambdaContext)))
459+
{
460+
var errorMessage = $"When using the {nameof(DynamoDBEventAttribute)}, the Lambda method can accept at most 2 parameters. " +
461+
$"The first parameter is required and must be of type {TypeFullNames.DynamoDBEvent}. " +
462+
$"The second parameter is optional and must be of type {TypeFullNames.ILambdaContext}.";
463+
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage));
464+
}
465+
466+
// Validate method return type - When using DynamoDBEventAttribute, the return type must be either void or Task
467+
if (!lambdaFunctionModel.LambdaMethod.ReturnsVoid && !lambdaFunctionModel.LambdaMethod.ReturnsVoidTask)
468+
{
469+
var errorMessage = $"When using the {nameof(DynamoDBEventAttribute)}, the Lambda method can return either void or {TypeFullNames.Task}";
470+
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage));
471+
}
472+
}
473+
423474
private static bool ReportDiagnostics(DiagnosticReporter diagnosticReporter, List<Diagnostic> diagnostics)
424475
{
425476
var isValid = true;

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Amazon.Lambda.Annotations.ALB;
2+
using Amazon.Lambda.Annotations.DynamoDB;
23
using Amazon.Lambda.Annotations.APIGateway;
34
using Amazon.Lambda.Annotations.SourceGenerator.Diagnostics;
45
using Amazon.Lambda.Annotations.SourceGenerator.FileIO;
@@ -232,6 +233,10 @@ private void ProcessLambdaFunctionEventAttributes(ILambdaFunctionSerializable la
232233
eventName = ProcessS3Attribute(lambdaFunction, s3AttributeModel.Data, currentSyncedEventProperties);
233234
currentSyncedEvents.Add(eventName);
234235
break;
236+
case AttributeModel<DynamoDBEventAttribute> dynamoDBAttributeModel:
237+
eventName = ProcessDynamoDBAttribute(lambdaFunction, dynamoDBAttributeModel.Data, currentSyncedEventProperties);
238+
currentSyncedEvents.Add(eventName);
239+
break;
235240
}
236241
}
237242

@@ -608,6 +613,68 @@ private string ProcessSqsAttribute(ILambdaFunctionSerializable lambdaFunction, S
608613
return att.ResourceName;
609614
}
610615

616+
/// <summary>
617+
/// Writes all properties associated with <see cref="DynamoDBEventAttribute"/> to the serverless template.
618+
/// </summary>
619+
private string ProcessDynamoDBAttribute(ILambdaFunctionSerializable lambdaFunction, DynamoDBEventAttribute att, Dictionary<string, List<string>> syncedEventProperties)
620+
{
621+
var eventName = att.ResourceName;
622+
var eventPath = $"Resources.{lambdaFunction.ResourceName}.Properties.Events.{eventName}";
623+
624+
_templateWriter.SetToken($"{eventPath}.Type", "DynamoDB");
625+
626+
// Stream
627+
_templateWriter.RemoveToken($"{eventPath}.Properties.Stream");
628+
if (!att.Stream.StartsWith("@"))
629+
{
630+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "Stream", att.Stream);
631+
}
632+
else
633+
{
634+
var resource = att.Stream.Substring(1);
635+
if (_templateWriter.Exists($"{PARAMETERS}.{resource}"))
636+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, $"Stream.{REF}", resource);
637+
else
638+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, $"Stream.{GET_ATTRIBUTE}", new List<string> { resource, "StreamArn" }, TokenType.List);
639+
}
640+
641+
// StartingPosition
642+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "StartingPosition", att.StartingPosition);
643+
644+
// BatchSize
645+
if (att.IsBatchSizeSet)
646+
{
647+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "BatchSize", att.BatchSize);
648+
}
649+
650+
// Enabled
651+
if (att.IsEnabledSet)
652+
{
653+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "Enabled", att.Enabled);
654+
}
655+
656+
// MaximumBatchingWindowInSeconds
657+
if (att.IsMaximumBatchingWindowInSecondsSet)
658+
{
659+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "MaximumBatchingWindowInSeconds", att.MaximumBatchingWindowInSeconds);
660+
}
661+
662+
// FilterCriteria
663+
if (att.IsFiltersSet)
664+
{
665+
const char SEPERATOR = ';';
666+
var filters = att.Filters.Split(SEPERATOR).Select(x => x.Trim()).ToList();
667+
var filterList = new List<Dictionary<string, string>>();
668+
foreach (var filter in filters)
669+
{
670+
filterList.Add(new Dictionary<string, string> { { "Pattern", filter } });
671+
}
672+
SetEventProperty(syncedEventProperties, lambdaFunction.ResourceName, eventName, "FilterCriteria.Filters", filterList, TokenType.List);
673+
}
674+
675+
return att.ResourceName;
676+
}
677+
611678
/// <summary>
612679
/// Writes all properties associated with <see cref="S3EventAttribute"/> to the serverless template.
613680
/// </summary>

0 commit comments

Comments
 (0)