Skip to content

feat(parser): support for CloudFormation Custom Resources #2335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
b88c545
feat(parser): Parser support for custom resource
May 27, 2023
389e63b
Merge branch 'develop' into custom
ran-isenberg May 27, 2023
68c75ea
fix pipeline for older python
May 29, 2023
54b2297
Merge branch 'custom' of ssh://github.com/ran-isenberg/aws-lambda-pow…
May 29, 2023
34adb1a
Merge branch 'develop' into custom
leandrodamascena May 30, 2023
d9d5de4
rename to cloudformation
Jun 8, 2023
6b7f6c2
Merge branch 'develop' into custom
leandrodamascena Jun 8, 2023
175b141
Update aws_lambda_powertools/utilities/parser/models/cloudformation_c…
ran-isenberg Jun 9, 2023
bcf6ff8
Update aws_lambda_powertools/utilities/parser/models/cloudformation_c…
ran-isenberg Jun 9, 2023
5dc7b1c
cr fixes
Jun 9, 2023
a6f3c62
Merge branch 'develop' into custom
ran-isenberg Jun 9, 2023
6b4ea78
optional old resource props
Jun 9, 2023
71a65d7
Merge branch 'custom' of ssh://github.com/ran-isenberg/aws-lambda-pow…
Jun 9, 2023
51fc1ef
revert: union type due to python bug
heitorlessa Jun 9, 2023
e58c403
docs: improve description for cfn custom resource model
heitorlessa Jun 9, 2023
0b09a6d
chore: rename test event
heitorlessa Jun 9, 2023
045f97c
chore: rename test and move to unit
heitorlessa Jun 9, 2023
caf218a
chore: refactor create test as unit test
heitorlessa Jun 9, 2023
926be8f
fix: resource_properties type to allow override
heitorlessa Jun 9, 2023
bd96a04
chore: refactor delete test as unit test
heitorlessa Jun 9, 2023
4f8b774
chore: refactor update test as unit test
heitorlessa Jun 9, 2023
6be00f2
chore: cleanup any leftovers, remove duplicate logic
heitorlessa Jun 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
RequestContextV2AuthorizerJwt,
RequestContextV2Http,
)
from .cloudformation_custom_resource import (
CloudFormationCustomResourceBaseModel,
CloudFormationCustomResourceCreateModel,
CloudFormationCustomResourceDeleteModel,
CloudFormationCustomResourceUpdateModel,
)
from .cloudwatch import (
CloudWatchLogsData,
CloudWatchLogsDecode,
Expand Down Expand Up @@ -147,4 +153,8 @@
"KafkaBaseEventModel",
"KinesisFirehoseSqsModel",
"KinesisFirehoseSqsRecord",
"CloudFormationCustomResourceUpdateModel",
"CloudFormationCustomResourceDeleteModel",
"CloudFormationCustomResourceCreateModel",
"CloudFormationCustomResourceBaseModel",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from typing import Any, Dict, Type, Union

from pydantic import BaseModel, Field, HttpUrl

from aws_lambda_powertools.utilities.parser.types import Literal


class CloudFormationCustomResourceBaseModel(BaseModel):
request_type: str = Field(..., alias="RequestType")
service_token: str = Field(..., alias="ServiceToken")
response_url: HttpUrl = Field(..., alias="ResponseURL")
stack_id: str = Field(..., alias="StackId")
request_id: str = Field(..., alias="RequestId")
logical_resource_id: str = Field(..., alias="LogicalResourceId")
resource_type: str = Field(..., alias="ResourceType")
resource_properties: Union[Dict[str, Any], Type[BaseModel]] = Field(..., alias="ResourceProperties")


class CloudFormationCustomResourceCreateModel(CloudFormationCustomResourceBaseModel):
request_type: Literal["Create"] = Field(..., alias="RequestType")


class CloudFormationCustomResourceDeleteModel(CloudFormationCustomResourceBaseModel):
request_type: Literal["Delete"] = Field(..., alias="RequestType")


class CloudFormationCustomResourceUpdateModel(CloudFormationCustomResourceBaseModel):
request_type: Literal["Update"] = Field(..., alias="RequestType")
old_resource_properties: Union[Dict[str, Any], Type[BaseModel]] = Field(..., alias="OldResourceProperties")
45 changes: 24 additions & 21 deletions docs/utilities/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,30 @@ def my_function():

Parser comes with the following built-in models:

| Model name | Description |
| --------------------------------------- | ------------------------------------------------------------------------------------- |
| **AlbModel** | Lambda Event Source payload for Amazon Application Load Balancer |
| **APIGatewayProxyEventModel** | Lambda Event Source payload for Amazon API Gateway |
| **APIGatewayProxyEventV2Model** | Lambda Event Source payload for Amazon API Gateway v2 payload |
| **CloudwatchLogsModel** | Lambda Event Source payload for Amazon CloudWatch Logs |
| **DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams |
| **EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge |
| **KafkaMskEventModel** | Lambda Event Source payload for AWS MSK payload |
| **KafkaSelfManagedEventModel** | Lambda Event Source payload for self managed Kafka payload |
| **KinesisDataStreamModel** | Lambda Event Source payload for Amazon Kinesis Data Streams |
| **KinesisFirehoseModel** | Lambda Event Source payload for Amazon Kinesis Firehose |
| **KinesisFirehoseSqsModel** | Lambda Event Source payload for SQS messages wrapped in Kinesis Firehose records |
| **LambdaFunctionUrlModel** | Lambda Event Source payload for Lambda Function URL payload |
| **S3EventNotificationEventBridgeModel** | Lambda Event Source payload for Amazon S3 Event Notification to EventBridge. |
| **S3Model** | Lambda Event Source payload for Amazon S3 |
| **S3ObjectLambdaEvent** | Lambda Event Source payload for Amazon S3 Object Lambda |
| **S3SqsEventNotificationModel** | Lambda Event Source payload for S3 event notifications wrapped in SQS event (S3->SQS) |
| **SesModel** | Lambda Event Source payload for Amazon Simple Email Service |
| **SnsModel** | Lambda Event Source payload for Amazon Simple Notification Service |
| **SqsModel** | Lambda Event Source payload for Amazon SQS |
| Model name | Description |
| ------------------------------------------- | ------------------------------------------------------------------------------------- |
| **AlbModel** | Lambda Event Source payload for Amazon Application Load Balancer |
| **APIGatewayProxyEventModel** | Lambda Event Source payload for Amazon API Gateway |
| **APIGatewayProxyEventV2Model** | Lambda Event Source payload for Amazon API Gateway v2 payload |
| **CloudwatchLogsModel** | Lambda Event Source payload for Amazon CloudWatch Logs |
| **DynamoDBStreamModel** | Lambda Event Source payload for Amazon DynamoDB Streams |
| **EventBridgeModel** | Lambda Event Source payload for Amazon EventBridge |
| **KafkaMskEventModel** | Lambda Event Source payload for AWS MSK payload |
| **KafkaSelfManagedEventModel** | Lambda Event Source payload for self managed Kafka payload |
| **KinesisDataStreamModel** | Lambda Event Source payload for Amazon Kinesis Data Streams |
| **KinesisFirehoseModel** | Lambda Event Source payload for Amazon Kinesis Firehose |
| **KinesisFirehoseSqsModel** | Lambda Event Source payload for SQS messages wrapped in Kinesis Firehose records |
| **LambdaFunctionUrlModel** | Lambda Event Source payload for Lambda Function URL payload |
| **S3EventNotificationEventBridgeModel** | Lambda Event Source payload for Amazon S3 Event Notification to EventBridge. |
| **S3Model** | Lambda Event Source payload for Amazon S3 |
| **S3ObjectLambdaEvent** | Lambda Event Source payload for Amazon S3 Object Lambda |
| **S3SqsEventNotificationModel** | Lambda Event Source payload for S3 event notifications wrapped in SQS event (S3->SQS) |
| **SesModel** | Lambda Event Source payload for Amazon Simple Email Service |
| **SnsModel** | Lambda Event Source payload for Amazon Simple Notification Service |
| **SqsModel** | Lambda Event Source payload for Amazon SQS |
| **CloudFormationCustomResourceCreateModel** | Lambda Event Source payload for AWS CloudFormation 'create' custom resource |
| **CloudFormationCustomResourceUpdateModel** | Lambda Event Source payload for AWS CloudFormation 'update' custom resource |
| **CloudFormationCustomResourceDeleteModel** | Lambda Event Source payload for AWS CloudFormation 'delete' custom resource |

#### Extending built-in models

Expand Down
13 changes: 13 additions & 0 deletions tests/events/customResourceCreate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"RequestType": "Create",
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe",
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b",
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21",
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx",
"LogicalResourceId": "xxxxxxxxx",
"ResourceType": "Custom::MyType",
"ResourceProperties": {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "ss"
}
}
13 changes: 13 additions & 0 deletions tests/events/customResourceDelete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"RequestType": "Delete",
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe",
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b",
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21",
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx",
"LogicalResourceId": "xxxxxxxxx",
"ResourceType": "Custom::MyType",
"ResourceProperties": {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "ss"
}
}
17 changes: 17 additions & 0 deletions tests/events/customResourceUpdate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"RequestType": "Update",
"ServiceToken": "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe",
"ResponseURL": "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b",
"StackId": "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21",
"RequestId": "xxxxx-d2a0-4dfb-ab1f-xxxxxx",
"LogicalResourceId": "xxxxxxxxx",
"ResourceType": "Custom::MyType",
"ResourceProperties": {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "new"
},
"OldResourceProperties": {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx-xxxx-xxx",
"MyProps": "old"
}
}
130 changes: 130 additions & 0 deletions tests/functional/parser/test_custom_resource.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import pytest
from pydantic import BaseModel, Field

from aws_lambda_powertools.utilities.parser import ValidationError, event_parser
from aws_lambda_powertools.utilities.parser.models import (
CloudFormationCustomResourceCreateModel,
CloudFormationCustomResourceDeleteModel,
CloudFormationCustomResourceUpdateModel,
)
from aws_lambda_powertools.utilities.typing import LambdaContext
from tests.functional.utils import load_event


@event_parser(model=CloudFormationCustomResourceCreateModel)
def handle_create_custom_resource(event: CloudFormationCustomResourceCreateModel, _: LambdaContext):
assert event.request_type == "Create"
assert event.request_id == "xxxxx-d2a0-4dfb-ab1f-xxxxxx"
assert event.service_token == "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe"
assert (
str(event.response_url)
== "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b"
)
assert event.stack_id == "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21"
assert event.logical_resource_id == "xxxxxxxxx"
assert event.resource_type == "Custom::MyType"
assert event.resource_properties == {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "ss",
}


@event_parser(model=CloudFormationCustomResourceUpdateModel)
def handle_update_custom_resource(event: CloudFormationCustomResourceUpdateModel, _: LambdaContext):
assert event.request_type == "Update"
assert event.request_id == "xxxxx-d2a0-4dfb-ab1f-xxxxxx"
assert event.service_token == "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe"
assert (
str(event.response_url)
== "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b"
)
assert event.stack_id == "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21"
assert event.logical_resource_id == "xxxxxxxxx"
assert event.resource_type == "Custom::MyType"
assert event.resource_properties == {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "new",
}
assert event.old_resource_properties == {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx-xxxx-xxx",
"MyProps": "old",
}


@event_parser(model=CloudFormationCustomResourceDeleteModel)
def handle_delete_custom_resource(event: CloudFormationCustomResourceDeleteModel, _: LambdaContext):
assert event.request_type == "Delete"
assert event.request_id == "xxxxx-d2a0-4dfb-ab1f-xxxxxx"
assert event.service_token == "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe"
assert (
str(event.response_url)
== "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b"
)
assert event.stack_id == "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21"
assert event.logical_resource_id == "xxxxxxxxx"
assert event.resource_type == "Custom::MyType"
assert event.resource_properties == {
"ServiceToken": "arn:aws:lambda:us-east-1:xxxxx:function:xxxxx",
"MyProps": "ss",
}


def test_create_trigger_event():
event_dict = load_event("customResourceCreate.json")
handle_create_custom_resource(event_dict, LambdaContext())


def test_update_trigger_event():
event_dict = load_event("customResourceUpdate.json")
handle_update_custom_resource(event_dict, LambdaContext())


def test_delete_trigger_event():
event_dict = load_event("customResourceDelete.json")
handle_delete_custom_resource(event_dict, LambdaContext())


def test_validate_create_event_does_not_conform_with_model():
event = {"invalid": "event"}
with pytest.raises(ValidationError):
handle_create_custom_resource(event, LambdaContext())


def test_validate_update_event_does_not_conform_with_model():
event = {"invalid": "event"}
with pytest.raises(ValidationError):
handle_update_custom_resource(event, LambdaContext())


def test_validate_delete_event_does_not_conform_with_model():
event = {"invalid": "event"}
with pytest.raises(ValidationError):
handle_delete_custom_resource(event, LambdaContext())


class MyModel(BaseModel):
MyProps: str


class MyCustomResource(CloudFormationCustomResourceCreateModel):
resource_properties: MyModel = Field(..., alias="ResourceProperties")


@event_parser(model=MyCustomResource)
def handle_create_custom_resource_extended_model(event: MyCustomResource, _: LambdaContext):
assert event.request_type == "Create"
assert event.request_id == "xxxxx-d2a0-4dfb-ab1f-xxxxxx"
assert event.service_token == "arn:aws:lambda:us-east-1:xxx:function:xxxx-CrbuiltinfunctionidProvi-2vKAalSppmKe"
assert (
str(event.response_url)
== "https://cloudformation-custom-resource-response-useast1.s3.amazonaws.com/7F%7Cb1f50fdfc25f3b"
)
assert event.stack_id == "arn:aws:cloudformation:us-east-1:xxxx:stack/xxxx/271845b0-f2e8-11ed-90ac-0eeb25b8ae21"
assert event.logical_resource_id == "xxxxxxxxx"
assert event.resource_type == "Custom::MyType"
assert event.resource_properties.MyProps == "ss"


def test_create_trigger_event_custom_model():
event_dict = load_event("customResourceCreate.json")
handle_create_custom_resource_extended_model(event_dict, LambdaContext())