forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb_event.py
45 lines (34 loc) · 1.58 KB
/
alb_event.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from typing import Dict, List, Optional
from aws_lambda_powertools.shared.headers_serializer import (
BaseHeadersSerializer,
MultiValueHeadersSerializer,
SingleValueHeadersSerializer,
)
from aws_lambda_powertools.utilities.data_classes.common import BaseProxyEvent, DictWrapper
class ALBEventRequestContext(DictWrapper):
@property
def elb_target_group_arn(self) -> str:
"""Target group arn for your Lambda function"""
return self["requestContext"]["elb"]["targetGroupArn"]
class ALBEvent(BaseProxyEvent):
"""Application load balancer event
Documentation:
--------------
- https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html
- https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
"""
@property
def request_context(self) -> ALBEventRequestContext:
return ALBEventRequestContext(self._data)
@property
def multi_value_query_string_parameters(self) -> Optional[Dict[str, List[str]]]:
return self.get("multiValueQueryStringParameters")
@property
def multi_value_headers(self) -> Optional[Dict[str, List[str]]]:
return self.get("multiValueHeaders")
def header_serializer(self) -> BaseHeadersSerializer:
# When using the ALB integration, the `multiValueHeaders` feature can be disabled (default) or enabled.
# We can determine if the feature is enabled by looking if the event has a `multiValueHeaders` key.
if self.multi_value_headers:
return MultiValueHeadersSerializer()
return SingleValueHeadersSerializer()