Skip to content

Commit 63152f3

Browse files
authored
fix: Impose a 127 character limit on the Lambda function handler when the package type is set to zip (#1812)
Addresses #1778
1 parent a3a8d44 commit 63152f3

File tree

9 files changed

+95
-26
lines changed

9 files changed

+95
-26
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
; Shipped analyzer releases
22
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
33

4+
## Release 1.5.1
5+
### New Rules
6+
7+
Rule ID | Category | Severity | Notes
8+
--------|----------|----------|-------
9+
AWSLambda0118 | AWSLambdaCSharpGenerator | Error | Maximum Handler Length Exceeded
10+
411
## Release 1.5.0
512
### New Rules
613

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,12 @@ public static class DiagnosticDescriptors
138138
category: "AWSLambdaCSharpGenerator",
139139
DiagnosticSeverity.Error,
140140
isEnabledByDefault: true);
141+
142+
public static readonly DiagnosticDescriptor MaximumHandlerLengthExceeded = new DiagnosticDescriptor(id: "AWSLambda0118",
143+
title: "Maximum Handler Length Exceeded",
144+
messageFormat: "The handler string '{0}' exceeds the maximum length of 127 characters. Please trim down your project namespace, class name or method name to stay within the character limit.",
145+
category: "AWSLambdaCSharpGenerator",
146+
DiagnosticSeverity.Error,
147+
isEnabledByDefault: true);
141148
}
142149
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ internal static bool ValidateFunction(GeneratorExecutionContext context, IMethod
2828
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidResourceName, methodLocation));
2929
}
3030

31+
// Check the handler length does not exceed 127 characters when the package type is set to zip
32+
// The official AWS docs state a 128 character limit on the Lambda handler. However, there is an open issue where the last character is stripped off
33+
// when the handler is exactly 128 characters long. Hence, we are enforcing a 127 character limit.
34+
// https://github.com/aws/aws-lambda-dotnet/issues/1642
35+
if (lambdaFunctionModel.PackageType == LambdaPackageType.Zip && lambdaFunctionModel.Handler.Length > 127)
36+
{
37+
diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.MaximumHandlerLengthExceeded, methodLocation, lambdaFunctionModel.Handler));
38+
}
39+
3140
// Check for Serializer attribute
3241
if (!lambdaMethodSymbol.ContainingAssembly.HasAttribute(context, TypeFullNames.LambdaSerializerAttribute))
3342
{

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/sqsEvents.template

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@
3535
}
3636
},
3737
"Properties": {
38-
"Runtime": "dotnet6",
39-
"CodeUri": ".",
4038
"MemorySize": 512,
4139
"Timeout": 30,
4240
"Policies": [
4341
"AWSLambdaBasicExecutionRole"
4442
],
45-
"PackageType": "Zip",
46-
"Handler": "TestProject::TestServerlessApp.SQSEventExamples.ValidSQSEvents_ProcessMessages_Generated::ProcessMessages",
43+
"PackageType": "Image",
44+
"ImageUri": ".",
45+
"ImageConfig": {
46+
"Command": [
47+
"TestProject::TestServerlessApp.SQSEventExamples.ValidSQSEvents_ProcessMessages_Generated::ProcessMessages"
48+
]
49+
},
4750
"Events": {
4851
"queue1": {
4952
"Type": "SQS",
@@ -109,15 +112,18 @@
109112
}
110113
},
111114
"Properties": {
112-
"Runtime": "dotnet6",
113-
"CodeUri": ".",
114115
"MemorySize": 512,
115116
"Timeout": 30,
116117
"Policies": [
117118
"AWSLambdaBasicExecutionRole"
118119
],
119-
"PackageType": "Zip",
120-
"Handler": "TestProject::TestServerlessApp.SQSEventExamples.ValidSQSEvents_ProcessMessagesWithBatchFailureReporting_Generated::ProcessMessagesWithBatchFailureReporting",
120+
"PackageType": "Image",
121+
"ImageUri": ".",
122+
"ImageConfig": {
123+
"Command": [
124+
"TestProject::TestServerlessApp.SQSEventExamples.ValidSQSEvents_ProcessMessagesWithBatchFailureReporting_Generated::ProcessMessagesWithBatchFailureReporting"
125+
]
126+
},
121127
"Events": {
122128
"queue3": {
123129
"Type": "SQS",

Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,31 @@ public async Task VerifyValidSQSEvents()
13301330
}.RunAsync();
13311331
}
13321332

1333+
[Fact]
1334+
public async Task ExceededMaximumHandlerLength()
1335+
{
1336+
await new VerifyCS.Test
1337+
{
1338+
TestState =
1339+
{
1340+
Sources =
1341+
{
1342+
(Path.Combine("TestServerlessApp", "PlaceholderClass.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "PlaceholderClass.cs"))),
1343+
(Path.Combine("TestServerlessApp", "ExceededMaximumHandlerLength.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "ExceededMaximumHandlerLength.cs.error"))),
1344+
(Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"), await File.ReadAllTextAsync(Path.Combine("Amazon.Lambda.Annotations", "LambdaFunctionAttribute.cs"))),
1345+
(Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"), await File.ReadAllTextAsync(Path.Combine("TestServerlessApp", "AssemblyAttributes.cs"))),
1346+
},
1347+
ExpectedDiagnostics =
1348+
{
1349+
DiagnosticResult
1350+
.CompilerError("AWSLambda0118")
1351+
.WithSpan($"TestServerlessApp{Path.DirectorySeparatorChar}ExceededMaximumHandlerLength.cs", 9, 9, 13, 10)
1352+
.WithArguments("TestProject::TestServerlessApp.ExceededMaximumHandlerLength_SayHelloXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX_Generated::SayHelloXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
1353+
},
1354+
}
1355+
}.RunAsync();
1356+
}
1357+
13331358
public void Dispose()
13341359
{
13351360
File.Delete(Path.Combine("TestServerlessApp", "serverless.template"));
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Amazon.Lambda.Annotations;
3+
4+
namespace TestServerlessApp
5+
{
6+
public class ExceededMaximumHandlerLength
7+
{
8+
// This fails because generated handler is longer than 127 characters
9+
[LambdaFunction]
10+
public string SayHelloXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX()
11+
{
12+
return "Hello, World!";
13+
}
14+
}
15+
}

Libraries/test/TestServerlessApp/SQSEventExamples/InvalidSQSEvents.cs.error

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,72 @@ namespace TestServerlessApp.SQSEventExamples
1212

1313
public class InvalidSQSEvents
1414
{
15-
[LambdaFunction]
15+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
1616
[SQSEvent("@testQueue", BatchSize = 0, MaximumBatchingWindowInSeconds = 302, MaximumConcurrency = 1)]
1717
public void ProcessMessageWithInvalidSQSEventAttributes(SQSEvent evnt)
1818
{
1919
Console.WriteLine($"Event processed: {evnt}");
2020
}
2121

22-
[LambdaFunction]
22+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
2323
[SQSEvent("@testQueue")]
2424
public void ProcessMessageWithInvalidParameters(SQSEvent evnt, bool invalidParameter1, int invalidParameter2)
2525
{
2626
Console.WriteLine($"Event processed: {evnt}");
2727
}
2828

29-
[LambdaFunction]
29+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
3030
[SQSEvent("@testQueue")]
3131
public bool ProcessMessageWithInvalidReturnType(SQSEvent evnt)
3232
{
3333
Console.WriteLine($"Event processed: {evnt}");
3434
return true;
3535
}
3636

37-
[LambdaFunction]
37+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
3838
[RestApi(LambdaHttpMethod.Get, "/")]
3939
[SQSEvent("@testQueue")]
4040
public void ProcessMessageWithMultipleEventTypes(SQSEvent evnt)
4141
{
4242
Console.WriteLine($"Event processed: {evnt}");
4343
}
4444

45-
[LambdaFunction]
45+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
4646
[SQSEvent("test-queue")]
4747
public void ProcessMessageWithInvalidQueueArn(SQSEvent evnt)
4848
{
4949
Console.WriteLine($"Event processed: {evnt}");
5050
}
5151

52-
[LambdaFunction]
52+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
5353
[SQSEvent("@testQueue", ResourceName = "sqs-event-source")]
5454
public void ProcessMessageWithInvalidResourceName(SQSEvent evnt)
5555
{
5656
Console.WriteLine($"Event processed: {evnt}");
5757
}
5858

59-
[LambdaFunction]
59+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
6060
[SQSEvent("@testQueue", ResourceName = "")]
6161
public void ProcessMessageWithEmptyResourceName(SQSEvent evnt)
6262
{
6363
Console.WriteLine($"Event processed: {evnt}");
6464
}
6565

66-
[LambdaFunction]
66+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
6767
[SQSEvent("@testQueue", BatchSize = 100)]
6868
public void ProcessMessageWithMaximumBatchingWindowInSecondsNotSpecified(SQSEvent evnt)
6969
{
7070
Console.WriteLine($"Event processed: {evnt}");
7171
}
7272

73-
[LambdaFunction]
73+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
7474
[SQSEvent("@testQueue", BatchSize = 100, MaximumBatchingWindowInSeconds = 0)]
7575
public void ProcessMessageWithMaximumBatchingWindowInSecondsLessThanOne(SQSEvent evnt)
7676
{
7777
Console.WriteLine($"Event processed: {evnt}");
7878
}
7979

80-
[LambdaFunction]
80+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
8181
[SQSEvent("arn:aws:sqs:us-east-2:444455556666:test-queue.fifo", BatchSize = 100, MaximumBatchingWindowInSeconds = 5)]
8282
public void ProcessMessageWithFifoQueue(SQSEvent evnt)
8383
{

Libraries/test/TestServerlessApp/SQSEventExamples/ValidSQSEvents.cs.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace TestServerlessApp.SQSEventExamples
1212

1313
public class ValidSQSEvents
1414
{
15-
[LambdaFunction]
15+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
1616
[SQSEvent("arn:aws:sqs:us-east-2:444455556666:queue1", BatchSize = 50, MaximumBatchingWindowInSeconds = 2, MaximumConcurrency = 30, Filters = "My-Filter-1; My-Filter-2")]
1717
[SQSEvent("arn:aws:sqs:us-east-2:444455556666:queue2", MaximumBatchingWindowInSeconds = 5, Enabled = false)]
1818
[SQSEvent("arn:aws:sqs:us-east-2:444455556666:my-queue")]
@@ -22,7 +22,7 @@ namespace TestServerlessApp.SQSEventExamples
2222
Console.WriteLine($"Event processed: {evnt}");
2323
}
2424

25-
[LambdaFunction]
25+
[LambdaFunction(PackageType = LambdaPackageType.Image)]
2626
[SQSEvent("arn:aws:sqs:us-east-2:444455556666:queue3")]
2727
public async Task<SQSBatchResponse> ProcessMessagesWithBatchFailureReporting(SQSEvent evnt)
2828
{

Libraries/test/TestServerlessApp/serverless.template

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,12 +913,6 @@
913913
"TestQueueEvent": {
914914
"Type": "SQS",
915915
"Properties": {
916-
"Queue": {
917-
"Fn::GetAtt": [
918-
"TestQueue",
919-
"Arn"
920-
]
921-
},
922916
"BatchSize": 50,
923917
"FilterCriteria": {
924918
"Filters": [
@@ -933,6 +927,12 @@
933927
"MaximumBatchingWindowInSeconds": 5,
934928
"ScalingConfig": {
935929
"MaximumConcurrency": 5
930+
},
931+
"Queue": {
932+
"Fn::GetAtt": [
933+
"TestQueue",
934+
"Arn"
935+
]
936936
}
937937
}
938938
}

0 commit comments

Comments
 (0)