-
Notifications
You must be signed in to change notification settings - Fork 436
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
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
389e63b
Merge branch 'develop' into custom
ran-isenberg 68c75ea
fix pipeline for older python
54b2297
Merge branch 'custom' of ssh://github.com/ran-isenberg/aws-lambda-pow…
34adb1a
Merge branch 'develop' into custom
leandrodamascena d9d5de4
rename to cloudformation
6b7f6c2
Merge branch 'develop' into custom
leandrodamascena 175b141
Update aws_lambda_powertools/utilities/parser/models/cloudformation_c…
ran-isenberg bcf6ff8
Update aws_lambda_powertools/utilities/parser/models/cloudformation_c…
ran-isenberg 5dc7b1c
cr fixes
a6f3c62
Merge branch 'develop' into custom
ran-isenberg 6b4ea78
optional old resource props
71a65d7
Merge branch 'custom' of ssh://github.com/ran-isenberg/aws-lambda-pow…
51fc1ef
revert: union type due to python bug
heitorlessa e58c403
docs: improve description for cfn custom resource model
heitorlessa 0b09a6d
chore: rename test event
heitorlessa 045f97c
chore: rename test and move to unit
heitorlessa caf218a
chore: refactor create test as unit test
heitorlessa 926be8f
fix: resource_properties type to allow override
heitorlessa bd96a04
chore: refactor delete test as unit test
heitorlessa 4f8b774
chore: refactor update test as unit test
heitorlessa 6be00f2
chore: cleanup any leftovers, remove duplicate logic
heitorlessa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
aws_lambda_powertools/utilities/parser/models/cloudformation_custom_resource.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
ran-isenberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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") | ||
heitorlessa marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.