Skip to content

Commit

Permalink
(core) adding escape hatches for sqs_lambda stage (#47)
Browse files Browse the repository at this point in the history
* adding escape hatches for sqs_lambda stage

* setting default for 'handler'

* flake8

* updating conditional checking of optional args

* formatting
  • Loading branch information
malachi-constant committed Mar 10, 2022
1 parent 7e9ebfb commit 6c046c2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 18 deletions.
47 changes: 30 additions & 17 deletions core/aws_ddk_core/stages/sqs_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def __init__(
scope: Construct,
id: str,
environment_id: str,
code: Code,
handler: str,
code: Optional[Code] = None,
handler: Optional[str] = None,
runtime: Runtime = Runtime.PYTHON_3_9,
role: Optional[IRole] = None,
memory_size: Optional[int] = None,
Expand All @@ -46,6 +46,8 @@ def __init__(
dead_letter_queue_enabled: bool = False,
max_receive_count: int = 1,
batch_size: Optional[int] = None,
lambda_function: Optional[IFunction] = None,
sqs_queue: Optional[IQueue] = None,
) -> None:
"""
DDK SQS to Lambda stage.
Expand All @@ -60,10 +62,12 @@ def __init__(
Identifier of the stage
environment_id : str
Identifier of the environment
code : Code
code : Optional[Code]
The source code of the Lambda function
handler : str
The name of the method within the code that Lambda calls to execute the function
Must be set if `lambda_function` is not.
handler : Optional[str]
The name of the method within the code that Lambda calls to execute the function.
Must be set if `lambda_function` is not.
runtime : Runtime
The runtime environment for the Lambda function. `PYTHON_3_9` by default
role : Optional[IRole]
Expand All @@ -83,22 +87,31 @@ def __init__(
batch_size : Optional[int]
The maximum number of records retrieved from the event source at the function invocation time.
`10` by default
lambda_function: Optional[IFunction]
Preexisting Lambda Function to use in stage. `None` by default
sqs_queue: Optional[IQueue]
Preexisting SQS Queue to use in stage. `None` by default
"""
super().__init__(scope, id)

self._event_detail_type: str = f"{id}-event-type"

self._function = LambdaFactory.function(
self,
id=f"{id}-function",
environment_id=environment_id,
code=code,
handler=handler,
runtime=runtime,
role=role,
memory_size=memory_size,
timeout=timeout,
)
if lambda_function:
self._function = lambda_function
elif code and handler:
self._function = LambdaFactory.function(
self,
id=f"{id}-function",
environment_id=environment_id,
code=code,
handler=handler,
runtime=runtime,
role=role,
memory_size=memory_size,
timeout=timeout,
)
else:
raise ValueError("'code' and 'handler' or 'lambda_function' must be set to instanstiate this stage")

self._dlq: Optional[DeadLetterQueue] = None
if dead_letter_queue_enabled:
Expand All @@ -111,7 +124,7 @@ def __init__(
),
)

self._queue = SQSFactory.queue(
self._queue = sqs_queue or SQSFactory.queue(
self,
id=f"{id}-queue",
environment_id=environment_id,
Expand Down
58 changes: 57 additions & 1 deletion core/tests/unit/test_sqs_lambda_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
from pathlib import Path

from aws_cdk.assertions import Template
from aws_cdk.aws_lambda import Code
from aws_cdk.aws_lambda import Code, Function, Runtime
from aws_cdk.aws_sqs import Queue
from aws_ddk_core.base import BaseStack
from aws_ddk_core.stages.sqs_lambda import SqsToLambdaStage

Expand Down Expand Up @@ -69,3 +70,58 @@ def test_sqs_lambda_event_source(test_stack: BaseStack) -> None:
"DelaySeconds": 15,
},
)


def test_sqs_lambda_with_existing_queue(test_stack: BaseStack) -> None:
SqsToLambdaStage(
scope=test_stack,
id="dummy-sqs-lambda",
environment_id="dev",
code=Code.from_asset(f"{Path(__file__).parents[2]}"),
handler="commons.handlers.lambda_handler",
sqs_queue=Queue(test_stack, "custom-queue", queue_name="custom-queue"),
)

template = Template.from_stack(test_stack)
template.has_resource_properties(
"AWS::Lambda::Function",
props={
"Runtime": "python3.9",
"MemorySize": 512,
},
)
template.has_resource_properties(
"AWS::SQS::Queue",
props={
"QueueName": "custom-queue",
},
)


def test_sqs_lambda_with_existing_function(test_stack: BaseStack) -> None:
SqsToLambdaStage(
scope=test_stack,
id="dummy-sqs-lambda",
environment_id="dev",
lambda_function=Function(
test_stack,
"custom-function",
runtime=Runtime.PYTHON_3_8,
handler="commons.handlers.lambda_handler",
code=Code.from_asset(f"{Path(__file__).parents[2]}"),
),
)

template = Template.from_stack(test_stack)
template.has_resource_properties(
"AWS::Lambda::Function",
props={
"Runtime": "python3.8",
},
)
template.has_resource_properties(
"AWS::SQS::Queue",
props={
"VisibilityTimeout": 120,
},
)

0 comments on commit 6c046c2

Please sign in to comment.