|
1 | 1 | import base64
|
2 | 2 | import json
|
3 | 3 | import zlib
|
4 |
| -from typing import Dict, List, Optional |
| 4 | +from typing import Any, Dict, List, Optional |
5 | 5 |
|
6 | 6 | from aws_lambda_powertools.utilities.trigger.common import DictWrapper
|
7 | 7 |
|
@@ -78,17 +78,26 @@ class CloudWatchLogsEvent(dict):
|
78 | 78 | - https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchlogs.html
|
79 | 79 | """
|
80 | 80 |
|
| 81 | + def __init__(self, event: Dict[str, Any]): |
| 82 | + super().__init__(event) |
| 83 | + self._decompressed_logs_data = None |
| 84 | + self._json_logs_data = None |
| 85 | + |
81 | 86 | @property
|
82 |
| - def aws_logs_data(self) -> str: |
| 87 | + def raw_logs_data(self) -> str: |
83 | 88 | """The value of the `data` field is a Base64 encoded ZIP archive."""
|
84 | 89 | return self["awslogs"]["data"]
|
85 | 90 |
|
86 | 91 | @property
|
87 | 92 | def decompress_logs_data(self) -> bytes:
|
88 | 93 | """Decode and decompress log data"""
|
89 |
| - payload = base64.b64decode(self.aws_logs_data) |
90 |
| - return zlib.decompress(payload, zlib.MAX_WBITS | 32) |
| 94 | + if self._decompressed_logs_data is None: |
| 95 | + payload = base64.b64decode(self.raw_logs_data) |
| 96 | + self._decompressed_logs_data = zlib.decompress(payload, zlib.MAX_WBITS | 32) |
| 97 | + return self._decompressed_logs_data |
91 | 98 |
|
92 | 99 | def parse_logs_data(self) -> CloudWatchLogsDecodedData:
|
93 | 100 | """Decode, decompress and parse json data as CloudWatchLogsDecodedData"""
|
94 |
| - return CloudWatchLogsDecodedData(json.loads(self.decompress_logs_data.decode("UTF-8"))) |
| 101 | + if self._json_logs_data is None: |
| 102 | + self._json_logs_data = json.loads(self.decompress_logs_data.decode("UTF-8")) |
| 103 | + return CloudWatchLogsDecodedData(self._json_logs_data) |
0 commit comments