Skip to content

Commit 8329bf3

Browse files
feat(event_source): add CodeDeploy Lifecycle Hook event (#5219)
* feat: Add CodeDeploy Lifecycle Hook event definition * Add tests * Update aws_lambda_powertools/utilities/data_classes/code_deploy_lifecycle_hook_event.py Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Update aws_lambda_powertools/utilities/data_classes/__init__.py Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Update tests/unit/data_classes/required_dependencies/test_code_deploy_lifecycle_hook_event.py Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Update tests/unit/data_classes/required_dependencies/test_code_deploy_lifecycle_hook_event.py Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Update aws_lambda_powertools/utilities/data_classes/__init__.py Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Add docs * Update docs/utilities/data_classes.md Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> * Update docs/utilities/data_classes.md Co-authored-by: Leandro Damascena <[email protected]> Signed-off-by: Mike W <[email protected]> --------- Signed-off-by: Mike W <[email protected]> Co-authored-by: Leandro Damascena <[email protected]>
1 parent 4104f8b commit 8329bf3

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

Diff for: aws_lambda_powertools/utilities/data_classes/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
from .cloud_watch_custom_widget_event import CloudWatchDashboardCustomWidgetEvent
1919
from .cloud_watch_logs_event import CloudWatchLogsEvent
2020
from .cloudformation_custom_resource_event import CloudFormationCustomResourceEvent
21+
from .code_deploy_lifecycle_hook_event import (
22+
CodeDeployLifecycleHookEvent,
23+
)
2124
from .code_pipeline_job_event import CodePipelineJobEvent
2225
from .connect_contact_flow_event import ConnectContactFlowEvent
2326
from .dynamo_db_stream_event import DynamoDBStreamEvent
@@ -59,6 +62,7 @@
5962
"CloudWatchAlarmMetricStat",
6063
"CloudWatchDashboardCustomWidgetEvent",
6164
"CloudWatchLogsEvent",
65+
"CodeDeployLifecycleHookEvent",
6266
"CodePipelineJobEvent",
6367
"ConnectContactFlowEvent",
6468
"DynamoDBStreamEvent",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
2+
3+
4+
class CodeDeployLifecycleHookEvent(DictWrapper):
5+
@property
6+
def deployment_id(self) -> str:
7+
"""The unique ID of the calling CodeDeploy Deployment."""
8+
return self["DeploymentId"]
9+
10+
@property
11+
def lifecycle_event_hook_execution_id(self) -> str:
12+
"""The unique ID of a deployments lifecycle hook."""
13+
return self["LifecycleEventHookExecutionId"]

Diff for: docs/utilities/data_classes.md

+25
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Log Data Event for Troubleshooting
9090
| [CloudWatch Alarm State Change Action](#cloudwatch-alarm-state-change-action) | `CloudWatchAlarmEvent` |
9191
| [CloudWatch Dashboard Custom Widget](#cloudwatch-dashboard-custom-widget) | `CloudWatchDashboardCustomWidgetEvent` |
9292
| [CloudWatch Logs](#cloudwatch-logs) | `CloudWatchLogsEvent` |
93+
| [CodeDeploy Lifecycle Hook](#codedeploy-lifecycle-hook) | `CodeDeployLifecycleHookEvent` |
9394
| [CodePipeline Job Event](#codepipeline-job) | `CodePipelineJobEvent` |
9495
| [Cognito User Pool](#cognito-user-pool) | Multiple available under `cognito_user_pool_event` |
9596
| [Connect Contact Flow](#connect-contact-flow) | `ConnectContactFlowEvent` |
@@ -615,6 +616,30 @@ Alternatively, you can use `extract_cloudwatch_logs_from_record` to seamless int
615616
return processor.response()
616617
```
617618

619+
### CodeDeploy LifeCycle Hook
620+
621+
CodeDeploy triggers Lambdas with this event when defined in
622+
[AppSpec definitions](https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html)
623+
to test applications at different stages of deployment.
624+
625+
626+
=== "app.py"
627+
```python
628+
from aws_lambda_powertools import Logger
629+
from aws_lambda_powertools.utilities.data_classes import (
630+
event_source,
631+
CodeDeployLifecycleHookEvent,
632+
)
633+
634+
logger = Logger()
635+
636+
def lambda_handler(
637+
event: CodeDeployLifecycleHookEvent, context: LambdaContext
638+
) -> None:
639+
deployment_id = event.deployment_id
640+
lifecycle_event_hook_execution_id = event.lifecycle_event_hook_execution_id
641+
```
642+
618643
### CodePipeline Job
619644

620645
Data classes and utility functions to help create continuous delivery pipelines tasks with AWS Lambda

Diff for: tests/events/codeDeployLifecycleHookEvent.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"DeploymentId": "d-ABCDEF",
3+
"LifecycleEventHookExecutionId": "xxxxxxxxxxxxxxxxxxxxxxxx"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pytest
2+
3+
from aws_lambda_powertools.utilities.data_classes import (
4+
CodeDeployLifecycleHookEvent,
5+
)
6+
from tests.functional.utils import load_event
7+
8+
9+
@pytest.mark.parametrize(
10+
"event_file",
11+
[
12+
"codeDeployLifecycleHookEvent.json",
13+
],
14+
)
15+
def test_code_deploy_lifecycle_hook_event(event_file):
16+
raw_event = load_event(event_file)
17+
parsed_event = CodeDeployLifecycleHookEvent(raw_event)
18+
19+
assert parsed_event.deployment_id == raw_event["DeploymentId"]
20+
assert parsed_event.lifecycle_event_hook_execution_id == raw_event["LifecycleEventHookExecutionId"]

0 commit comments

Comments
 (0)