You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -852,6 +853,226 @@ The following SQS event source mapping will be generated for the `SQSMessageHand
852
853
}
853
854
```
854
855
856
+
## Application Load Balancer (ALB) Example
857
+
858
+
This example shows how to use the `ALBApi` attribute to configure a Lambda function as a target behind an [Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html). Unlike API Gateway event attributes that map to SAM event types, the ALB integration generates standalone CloudFormation resources — a `TargetGroup`, a `ListenerRule`, and a `Lambda::Permission` — to wire the Lambda function to an existing ALB listener.
859
+
860
+
The `ALBApi` attribute contains the following properties:
|`ListenerArn`|`string`| Yes | — | The ARN of the existing ALB listener, or a `@ResourceName` reference to a listener resource defined in the CloudFormation template. |
865
+
|`PathPattern`|`string`| Yes | — | The path pattern condition for the listener rule (e.g., `"/api/orders/*"`). Supports wildcard characters `*` and `?`. |
866
+
|`Priority`|`int`| Yes | — | The listener rule priority (1–50000). Lower numbers are evaluated first. Must be unique per listener. |
867
+
|`MultiValueHeaders`|`bool`| No |`false`| When `true`, enables multi-value headers on the target group. The function should then use `MultiValueHeaders` and `MultiValueQueryStringParameters` on request/response objects. |
868
+
|`HostHeader`|`string`| No |`null`| Optional host header condition (e.g., `"api.example.com"`). |
869
+
|`HttpMethod`|`string`| No |`null`| Optional HTTP method condition (e.g., `"GET"`, `"POST"`). Leave null to match all methods. |
870
+
|`ResourceName`|`string`| No |`"{LambdaResourceName}ALB"`| Custom CloudFormation resource name prefix for the generated resources. Must be alphanumeric. |
871
+
872
+
The `ALBApi` attribute must be applied to a Lambda method along with the `LambdaFunction` attribute.
873
+
874
+
The Lambda method must conform to the following rules when tagged with the `ALBApi` attribute:
875
+
876
+
1. It must have at least 1 argument and can have at most 2 arguments.
877
+
- The first argument is required and must be of type `ApplicationLoadBalancerRequest` defined in the [Amazon.Lambda.ApplicationLoadBalancerEvents](https://github.com/aws/aws-lambda-dotnet/tree/master/Libraries/src/Amazon.Lambda.ApplicationLoadBalancerEvents) package.
878
+
- The second argument is optional and must be of type `ILambdaContext`.
879
+
2. The method return type must be `ApplicationLoadBalancerResponse` or `Task<ApplicationLoadBalancerResponse>`.
880
+
881
+
### Prerequisites
882
+
883
+
Your CloudFormation template must include an existing ALB and listener. The `ALBApi` attribute references the listener — it does **not** create the ALB or listener for you. You can define them in the same template or reference one that already exists via its ARN.
884
+
885
+
### Basic Example
886
+
887
+
This example creates a simple hello endpoint behind an ALB listener that is defined elsewhere in the template:
Body=$"{{\"message\": \"Hello from ALB Lambda!\", \"path\": \"{request.Path}\"}}"
914
+
};
915
+
}
916
+
}
917
+
```
918
+
919
+
In the example above, `@ALBTestListener` references a listener resource called `ALBTestListener` defined in the same CloudFormation template. The `@` prefix tells the source generator to use a `Ref` intrinsic function instead of a literal ARN string.
920
+
921
+
### Using a Literal Listener ARN
922
+
923
+
If you want to reference an ALB listener in a different stack or one that was created outside of CloudFormation, use the full ARN:
For each `ALBApi` attribute, the source generator creates three CloudFormation resources. Here is an example of the generated template for the basic hello endpoint:
The `ALBApi` attribute requires an existing ALB listener. Here is a minimal example of the infrastructure resources you would add to your `serverless.template`:
Then your Lambda function references `@MyListener` in the `ALBApi` attribute.
1075
+
855
1076
## Custom Lambda Authorizer Example
856
1077
857
1078
Lambda Annotations supports defining custom Lambda authorizers using attributes. Custom authorizers let you control access to your API Gateway endpoints by running a Lambda function that validates tokens or request parameters before the target function is invoked. The source generator automatically wires up the authorizer resources and references in the CloudFormation template.
@@ -1198,7 +1419,9 @@ parameter to the `LambdaFunction` must be the event object and the event source
1198
1419
* RestApiAuthorizer
1199
1420
* Marks a Lambda function as a REST API (API Gateway V1) custom authorizer. The authorizer name is automatically derived from the method name. Other functions reference it via `RestApi.Authorizer` using `nameof()`. Use the `Type` property to choose between `Token` and `Request` authorizer types.
1200
1421
* SQSEvent
1201
-
* Sets up event source mapping between the Lambda function and SQS queues. The SQS queue ARN is required to be set on the attribute. If users want to pass a reference to an existing SQS queue resource defined in their CloudFormation template, they can pass the SQS queue resource name prefixed with the '@' symbol.
1422
+
* Sets up event source mapping between the Lambda function and SQS queues. The SQS queue ARN is required to be set on the attribute. If users want to pass a reference to an existing SQS queue resource defined in their CloudFormation template, they can pass the SQS queue resource name prefixed with the '@' symbol.
1423
+
* ALBApi
1424
+
* Configures the Lambda function to be called from an Application Load Balancer. The listener ARN (or `@ResourceName` template reference), path pattern, and priority are required. The source generator creates standalone CloudFormation resources (TargetGroup, ListenerRule, Lambda Permission) rather than SAM event types.
1202
1425
1203
1426
### Parameter Attributes
1204
1427
@@ -1277,3 +1500,5 @@ The content type is determined using the following rules.
1277
1500
## Project References
1278
1501
1279
1502
If API Gateway event attributes, such as `RestAPI` or `HttpAPI`, are being used then a package reference to `Amazon.Lambda.APIGatewayEvents` must be added to the project, otherwise the project will not compile. We do not include it by default in order to keep the `Amazon.Lambda.Annotations` library lightweight.
1503
+
1504
+
Similarly, if the `ALBApi` attribute is being used then a package reference to `Amazon.Lambda.ApplicationLoadBalancerEvents` must be added to the project. This provides the `ApplicationLoadBalancerRequest` and `ApplicationLoadBalancerResponse` types used by ALB Lambda functions.
0 commit comments