-
Notifications
You must be signed in to change notification settings - Fork 496
Application Load Balancer (ALB) #2318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
bed2729
feat: Add ALBApi attribute definition for Application Load Balancer s…
GarrettBeatty 28590ff
feat: Add ALB source generator model layer
GarrettBeatty 688daca
feat: Add ALB code generation support and dependency validation
GarrettBeatty a902df1
feat: Add ALB CloudFormation template generation
GarrettBeatty 64a05ba
test: Add ALB CloudFormation writer unit tests
GarrettBeatty d26a6d3
test: Add ALB source generator snapshot tests
GarrettBeatty c0cc155
test: Add ALB AWS integration test project
GarrettBeatty 7e84c5a
chore: Add ALB projects to solution and solution filter
GarrettBeatty 6ac3675
fix: replace invalid MultiValueHeadersEnabled with TargetGroupAttribu…
GarrettBeatty eb0a529
docs: add ALB feature documentation to Annotations README
GarrettBeatty 5184c5c
fix: rename DefaultAction to DefaultActions for ALB Listener
GarrettBeatty 2d57231
Add autover change file for ALB annotations feature
GarrettBeatty 818e53c
Address PR review comments from Copilot
GarrettBeatty de353af
test: add unit tests for ALB orphan cleanup and metadata tracking
GarrettBeatty cc1a75a
feat: add compile-time warning for unresolved @reference in ALB Liste…
GarrettBeatty 2deabb2
feat: make ALB @reference not-found diagnostic an error (not warning)
GarrettBeatty 58afd55
Add ALB diagnostic descriptors for FromRoute and unmapped parameters
GarrettBeatty c9f5316
Update ParameterListExtension to recognize ALB request types as pass-…
GarrettBeatty 9967f34
Update GeneratedMethodModelBuilder to support ALB parameter binding
GarrettBeatty 03f39e9
Create ALBSetupParameters template for ALB parameter binding
GarrettBeatty 38bc77b
Create ALBInvoke template for ALB method invocation and response wrap…
GarrettBeatty 3505bea
Update LambdaFunctionTemplate to dispatch ALB events to ALB templates
GarrettBeatty 025383b
Update LambdaFunctionValidator for ALB flexible parameters and diagno…
GarrettBeatty 3c2c696
Update test app and tests for ALB FromX parameter binding
GarrettBeatty d4d45db
Add http-header condition support to ALBApiAttribute
GarrettBeatty b297018
Add query-string and source-ip condition support to ALBApiAttribute
GarrettBeatty 311bcd6
Add ALB-specific FromHeader, FromQuery, FromBody attributes in ALB na…
GarrettBeatty a01ed96
Fix CloudFormation ALB condition format: use *Config sub-objects
GarrettBeatty 539f9bd
update README
GarrettBeatty 6d2d78b
add header
GarrettBeatty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Added [ALBApi] attribute for configuring Lambda functions as targets behind an Application Load Balancer" | ||
| ] | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...src/Amazon.Lambda.Annotations.SourceGenerator/Models/Attributes/ALBApiAttributeBuilder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using Amazon.Lambda.Annotations.ALB; | ||
| using Microsoft.CodeAnalysis; | ||
| using System; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes | ||
| { | ||
| /// <summary> | ||
| /// Builder for <see cref="ALBApiAttribute"/>. | ||
| /// </summary> | ||
| public class ALBApiAttributeBuilder | ||
| { | ||
| public static ALBApiAttribute Build(AttributeData att) | ||
| { | ||
| if (att.ConstructorArguments.Length != 3) | ||
| { | ||
| throw new NotSupportedException($"{TypeFullNames.ALBApiAttribute} must have constructor with 3 arguments."); | ||
| } | ||
|
|
||
| var listenerArn = att.ConstructorArguments[0].Value as string; | ||
| var pathPattern = att.ConstructorArguments[1].Value as string; | ||
| var priority = (int)att.ConstructorArguments[2].Value; | ||
|
|
||
| var data = new ALBApiAttribute(listenerArn, pathPattern, priority); | ||
|
|
||
| foreach (var pair in att.NamedArguments) | ||
| { | ||
| if (pair.Key == nameof(data.MultiValueHeaders) && pair.Value.Value is bool multiValueHeaders) | ||
| { | ||
| data.MultiValueHeaders = multiValueHeaders; | ||
| } | ||
| else if (pair.Key == nameof(data.HostHeader) && pair.Value.Value is string hostHeader) | ||
| { | ||
| data.HostHeader = hostHeader; | ||
| } | ||
| else if (pair.Key == nameof(data.HttpMethod) && pair.Value.Value is string httpMethod) | ||
| { | ||
| data.HttpMethod = httpMethod; | ||
| } | ||
| else if (pair.Key == nameof(data.ResourceName) && pair.Value.Value is string resourceName) | ||
| { | ||
| data.ResourceName = resourceName; | ||
| } | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ public enum EventType | |
| SQS, | ||
| DynamoDB, | ||
| Schedule, | ||
| Authorizer | ||
| Authorizer, | ||
| ALB | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using Amazon.Lambda.Annotations.SourceGenerator.Diagnostics; | ||
| using Amazon.Lambda.Annotations.ALB; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing the license header |
||
| using Amazon.Lambda.Annotations.SourceGenerator.Diagnostics; | ||
| using Amazon.Lambda.Annotations.SourceGenerator.Extensions; | ||
| using Amazon.Lambda.Annotations.SourceGenerator.Models; | ||
| using Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes; | ||
|
|
@@ -59,6 +60,7 @@ internal static bool ValidateFunction(GeneratorExecutionContext context, IMethod | |
| // Validate Events | ||
| ValidateApiGatewayEvents(lambdaFunctionModel, methodLocation, diagnostics); | ||
| ValidateSqsEvents(lambdaFunctionModel, methodLocation, diagnostics); | ||
| ValidateAlbEvents(lambdaFunctionModel, methodLocation, diagnostics); | ||
|
|
||
| return ReportDiagnostics(diagnosticReporter, diagnostics); | ||
| } | ||
|
|
@@ -86,6 +88,16 @@ internal static bool ValidateDependencies(GeneratorExecutionContext context, IMe | |
| } | ||
| } | ||
|
|
||
| // Check for references to "Amazon.Lambda.ApplicationLoadBalancerEvents" if the Lambda method is annotated with ALBApi attribute. | ||
| if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.ALBApiAttribute)) | ||
| { | ||
| if (context.Compilation.ReferencedAssemblyNames.FirstOrDefault(x => x.Name == "Amazon.Lambda.ApplicationLoadBalancerEvents") == null) | ||
| { | ||
| diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MissingDependencies, methodLocation, "Amazon.Lambda.ApplicationLoadBalancerEvents")); | ||
| return false; | ||
| } | ||
| } | ||
|
GarrettBeatty marked this conversation as resolved.
|
||
|
|
||
| return true; | ||
| } | ||
|
|
||
|
|
@@ -268,6 +280,48 @@ private static void ValidateSqsEvents(LambdaFunctionModel lambdaFunctionModel, L | |
| } | ||
| } | ||
|
|
||
| private static void ValidateAlbEvents(LambdaFunctionModel lambdaFunctionModel, Location methodLocation, List<Diagnostic> diagnostics) | ||
| { | ||
| // If the method does not contain any ALB events, then simply return early | ||
| if (!lambdaFunctionModel.LambdaMethod.Events.Contains(EventType.ALB)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| // Validate ALBApiAttributes | ||
| foreach (var att in lambdaFunctionModel.Attributes) | ||
| { | ||
| if (att.Type.FullName != TypeFullNames.ALBApiAttribute) | ||
| continue; | ||
|
|
||
| var albApiAttribute = ((AttributeModel<ALBApiAttribute>)att).Data; | ||
| var validationErrors = albApiAttribute.Validate(); | ||
| validationErrors.ForEach(errorMessage => diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidAlbApiAttribute, methodLocation, errorMessage))); | ||
| } | ||
|
|
||
| // Validate method parameters - When using ALBApiAttribute, the method signature must be | ||
| // (ApplicationLoadBalancerRequest request) or (ApplicationLoadBalancerRequest request, ILambdaContext context) | ||
| var parameters = lambdaFunctionModel.LambdaMethod.Parameters; | ||
|
GarrettBeatty marked this conversation as resolved.
|
||
| if (parameters.Count == 0 || | ||
| parameters.Count > 2 || | ||
| (parameters.Count == 1 && parameters[0].Type.FullName != TypeFullNames.ApplicationLoadBalancerRequest) || | ||
| (parameters.Count == 2 && (parameters[0].Type.FullName != TypeFullNames.ApplicationLoadBalancerRequest || parameters[1].Type.FullName != TypeFullNames.ILambdaContext))) | ||
| { | ||
| var errorMessage = $"When using the {nameof(ALBApiAttribute)}, the Lambda method can accept at most 2 parameters. " + | ||
| $"The first parameter is required and must be of type {TypeFullNames.ApplicationLoadBalancerRequest}. " + | ||
| $"The second parameter is optional and must be of type {TypeFullNames.ILambdaContext}."; | ||
| diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage)); | ||
| } | ||
|
|
||
| // Validate method return type - When using ALBApiAttribute, the return type must be | ||
| // ApplicationLoadBalancerResponse or Task<ApplicationLoadBalancerResponse> | ||
| if (!lambdaFunctionModel.LambdaMethod.ReturnsApplicationLoadBalancerResponse) | ||
| { | ||
| var errorMessage = $"When using the {nameof(ALBApiAttribute)}, the Lambda method must return {TypeFullNames.ApplicationLoadBalancerResponse} or Task<{TypeFullNames.ApplicationLoadBalancerResponse}>"; | ||
| diagnostics.Add(Diagnostic.Create(DiagnosticDescriptors.InvalidLambdaMethodSignature, methodLocation, errorMessage)); | ||
| } | ||
| } | ||
|
|
||
| private static bool ReportDiagnostics(DiagnosticReporter diagnosticReporter, List<Diagnostic> diagnostics) | ||
| { | ||
| var isValid = true; | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing the license header