Conversation
There was a problem hiding this comment.
Pull request overview
Adds declarative Lambda Authorizer support to the Amazon.Lambda.Annotations framework by introducing new authorizer attributes and extending the source generator to emit corresponding SAM Auth configuration for protected API events.
Changes:
- Introduces
[HttpApiAuthorizer]/[RestApiAuthorizer]attributes plus supporting models/builders in the source generator. - Extends
[HttpApi]/[RestApi]with anAuthorizerproperty and updates CloudFormation template generation accordingly. - Updates/introduces test applications and baseline templates to exercise authorizer scenarios.
Reviewed changes
Copilot reviewed 26 out of 26 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| Libraries/src/Amazon.Lambda.Annotations/APIGateway/HttpApiAuthorizerAttribute.cs | New attribute surface for HTTP API Lambda authorizers. |
| Libraries/src/Amazon.Lambda.Annotations/APIGateway/RestApiAuthorizerAttribute.cs | New attribute surface for REST API Lambda authorizers. |
| Libraries/src/Amazon.Lambda.Annotations/APIGateway/HttpApiAttribute.cs | Adds Authorizer property for protecting HTTP API routes. |
| Libraries/src/Amazon.Lambda.Annotations/APIGateway/RestApiAttribute.cs | Adds Authorizer property for protecting REST API routes. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs | Detects authorizer attributes and adds authorizer data into the report. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs | Writes authorizer definitions and route-level Auth config into templates; adds orphan cleanup. |
| Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Models/* | Adds authorizer models and wiring through report + lambda function model. |
| Libraries/test/TestServerlessApp/AuthorizerFunctions.cs | New test functions demonstrating protected/public endpoints and authorizers. |
| Libraries/test/TestServerlessApp/serverless.template | Updated expected baseline template for the new authorizer scenarios. |
| Libraries/test/TestCustomAuthorizerApp/* | Updates sample app + template to use new Authorizer property. |
| Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/CloudFormationWriterTests.cs | Adjusts test model to include new Authorizer property. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
aead1d4 to
663a5b0
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 34 out of 34 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Outdated
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
...st/Amazon.Lambda.Annotations.SourceGenerators.Tests/WriterTests/CloudFormationWriterTests.cs
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 48 out of 48 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| Without this, the generator fails with FileNotFoundException in Release builds (CS8785). | ||
| Use absolute path via $(MSBuildProjectDirectory) so the path resolves correctly when | ||
| propagated to consuming projects through the project reference chain. --> | ||
| <TargetPathWithTargetPlatformMoniker Include="$(MSBuildProjectDirectory)\$(OutputPath)Amazon.Lambda.Annotations.dll" IncludeRuntimeDependency="false" /> |
There was a problem hiding this comment.
2026-02-26T12:06:37.456-05:00
REPORT RequestId: 49ccb130-93d1-4020-b13f-2046990f5e67 Duration: 985.80 ms Billed Duration: 986 ms Memory Size: 512 MB Max Memory Used: 28 MB
REPORT RequestId: 49ccb130-93d1-4020-b13f-2046990f5e67 Duration: 985.80 ms Billed Duration: 986 ms Memory Size: 512 MB Max Memory Used: 28 MB
2026-02-26T12:06:37.579-05:00
2026-02-26T17:06:37.557Z fail Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load type 'TestServerlessApp.ComplexCalculator_Add_Generated' from assembly 'TestServerlessApp'.
2026-02-26T17:06:37.557Z fail Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load type 'TestServerlessApp.ComplexCalculator_Add_Generated' from assembly 'TestServerlessApp'.
2026-02-26T12:06:37.579-05:00
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 115
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 115
2026-02-26T12:06:37.579-05:00
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
2026-02-26T12:06:37.579-05:00
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 210
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 210
2026-02-26T12:06:37.742-05:00
Unhandled exception. Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load type 'TestServerlessApp.ComplexCalculator_Add_Generated' from assembly 'TestServerlessApp'.
Unhandled exception. Amazon.Lambda.RuntimeSupport.ExceptionHandling.LambdaValidationException: Unable to load type 'TestServerlessApp.ComplexCalculator_Add_Generated' from assembly 'TestServerlessApp'.
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 115
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeLoader.Init(Action`1 customerLoggingAction) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeLoader.cs:line 115
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
at Amazon.Lambda.RuntimeSupport.Bootstrap.UserCodeInitializer.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/UserCodeInitializer.cs:line 46
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 210
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 210
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 222
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.InitializeAsync() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 222
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.RunAsync(CancellationToken cancellationToken) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 156
at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.RunAsync(CancellationToken cancellationToken) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Bootstrap/LambdaBootstrap.cs:line 156
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.RuntimeSupportInitializer.RunLambdaBootstrap() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/RuntimeSupportInitializer.cs:line 63
at Amazon.Lambda.RuntimeSupport.RuntimeSupportInitializer.RunLambdaBootstrap() in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/RuntimeSupportInitializer.cs:line 63
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.Program.Main(String[] args) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Program.cs:line 43
at Amazon.Lambda.RuntimeSupport.Program.Main(String[] args) in /src/Repo/Libraries/src/Amazon.Lambda.RuntimeSupport/Program.cs:line 43
2026-02-26T12:06:37.742-05:00
at Amazon.Lambda.RuntimeSupport.Program.<Main>(String[] args)
@normj have you seen this issue at all when working with the annotations libraries integration test project. without this change i was seeing this problem where sometimes the release build wouldnt include the generated files (it would sometimes but not all the time). adding this line to here seemed to fix the issue but it doesnt feel right
|
|
||
| ### New Rules | ||
|
|
||
| Rule ID | Category | Severity | Notes |
There was a problem hiding this comment.
i added these to this file manually is that correct to do?
|
When a user adds an authorizer and we add an RestApi or HttpApi resource the the CF template we need to make sure all API Gateway based Lambda functions are referencing the Api resource even if they aren't using the authorizer. We should only be creating one Api endpoint. |
Libraries/src/Amazon.Lambda.Annotations/APIGateway/HttpApiAuthorizerAttribute.cs
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations/APIGateway/RestApiAuthorizerAttribute.cs
Outdated
Show resolved
Hide resolved
|
Nice to have would be to fix the compiler error when the |
|
The |
|
To improve the user experience what do you think of the authorizer method signature supporting the public IAuthorizeResult Get([FromHeader("Authorization"] string authorization, ILambdaContext)
{
if (IsValid(authorization)
{
return AuthorizeResult.Allow();
}
return AuthorizeResult.Deny();
} |
i have updated to fix this. |
i have added this new diagnostic too |
theres actually two things.
so now user needs to make sure both of these versions match. I have added a diagnostic warning to also detect if there is a mismatch. |
i will add this in a follow up PR after this one gets merged |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 53 out of 53 changed files in this pull request and generated 9 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs
Outdated
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Writers/CloudFormationWriter.cs
Outdated
Show resolved
Hide resolved
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs
Show resolved
Hide resolved
Libraries/test/TestCustomAuthorizerApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
Libraries/test/TestServerlessApp.IntegrationTests/IntegrationTestContextFixture.cs
Show resolved
Hide resolved
| /// Authorizer payload format version. Defaults to <see cref="APIGateway.AuthorizerPayloadFormatVersion.V2"/>. | ||
| /// Maps to the <c>AuthorizerPayloadFormatVersion</c> property in the SAM template. | ||
| /// </summary> | ||
| public AuthorizerPayloadFormatVersion AuthorizerPayloadFormatVersion { get; set; } = AuthorizerPayloadFormatVersion.V2; |
There was a problem hiding this comment.
@normj updated this to be an enum similar to Version in HttpApi
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Patch", | ||
| "ChangelogMessages": [ | ||
| "Added Authorizers annotation" |
There was a problem hiding this comment.
will update this to be clearer and i also need to create a feature branch for this
| AWSLambda0124 | AWSLambdaCSharpGenerator | Error | Authorizer Type Mismatch | ||
| AWSLambda0125 | AWSLambdaCSharpGenerator | Error | Duplicate Authorizer Name | ||
| AWSLambda0127 | AWSLambdaCSharpGenerator | Error | Invalid Result TTL | ||
| AWSLambda0128 | AWSLambdaCSharpGenerator | Warning | Authorizer Payload Version Mismatch |
There was a problem hiding this comment.
im thinking this should be error instead of warning
Pull Request: Lambda Authorizer Annotations Support
Description
This PR adds declarative Lambda Authorizer support to the AWS Lambda Annotations framework. Developers can now define Lambda Authorizers and protect API endpoints entirely through C# attributes, eliminating the need for manual CloudFormation configuration. The source generator automatically creates all necessary CloudFormation resources including authorizer definitions, Lambda permissions, and route-level authorizer references.
New Attributes
[HttpApiAuthorizer]— For HTTP API (API Gateway V2)Marks a Lambda function as an HTTP API custom authorizer. The authorizer name is automatically derived from the method name, allowing other functions to reference it via
nameof()for compile-time safety.IdentityHeaderstring"Authorization"$request.header.{value}in CloudFormation.EnableSimpleResponsesbooltruetrue, use simple responses (IsAuthorized: true/false). Whenfalse, use IAM policy responses.AuthorizerPayloadFormatVersionAuthorizerPayloadFormatVersionV2V1("1.0") orV2("2.0").ResultTtlInSecondsint00= no caching. Max =3600.[RestApiAuthorizer]— For REST API (API Gateway V1)Marks a Lambda function as a REST API custom authorizer. The authorizer name is automatically derived from the method name.
IdentityHeaderstring"Authorization"method.request.header.{value}in CloudFormation.TypeRestApiAuthorizerTypeTokenToken(receives just the token viarequest.AuthorizationToken) orRequest(receives full request context).ResultTtlInSecondsint00= no caching. Max =3600.Updated Attributes
[HttpApi]— AddedAuthorizerproperty to reference an HTTP API authorizer by method name (usenameof()for compile-time safety)[RestApi]— AddedAuthorizerproperty to reference a REST API authorizer by method name (usenameof()for compile-time safety)New Parameter Attribute
[FromCustomAuthorizer]— Maps a method parameter to a value from the custom authorizer context. Use theNameproperty to specify the key. Returns HTTP 401 if the key is not found or type conversion fails.Basic Usage
HTTP API Authorizer Example
REST API Authorizer Example
Authorizer with Custom Header and Caching
HTTP API Authorizer with IAM Policy Responses (Payload Format 1.0)
Non-String Type Extraction from Authorizer Context
Key Design Decisions
Nameproperty on the attributes. This enablesnameof()references for compile-time safety. If the authorizer method is renamed, all references update automatically.[FromCustomAuthorizer]returns 401 on missing keys — If an expected authorizer context key is not found or type conversion fails, the generated handler returns HTTP 401 Unauthorized rather than a 500 error.[HttpApi]or[RestApi]endpoints can reference the same authorizer method.Authorizerremain public — Only endpoints that explicitly setAuthorizer = nameof(...)are protected.What Gets Generated
The source generator automatically creates all necessary CloudFormation resources:
Authconfiguration on the API Gateway resources (AnnotationsHttpApi/ServerlessRestApi) with authorizer definitionsAWS::Lambda::Permissionresources for API Gateway to invoke authorizer functions