Skip to content

Commit

Permalink
cloud watch event support, address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kapilt committed Jul 26, 2019
1 parent d042989 commit e8f03e7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 44 deletions.
2 changes: 1 addition & 1 deletion chalice/deploy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def dependencies(self):

@attrs
class CloudWatchEvent(FunctionEventSubscriber):
resource_type = 'cw_event'
resource_type = 'cloud_watch_event'
rule_name = attrib() # type: str
event_pattern = attrib() # type: str

Expand Down
55 changes: 15 additions & 40 deletions chalice/deploy/planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,18 +562,23 @@ def _plan_s3bucketnotification(self, resource):
),
]

def _plan_cloudwatchevent(self, resource):
# type: (models.CloudWatchEvent) -> Sequence[InstructionMsg]
def _create_cloudwatchevent(self, resource):
# type: (Union[models.ScheduledEvent, models.CloudWatchEvent]) -> Sequence[InstructionMsg] # noqa

function_arn = Variable(
'%s_lambda_arn' % resource.lambda_function.resource_name
)

params = {'rule_name': resource.rule_name}
if isinstance(resource, models.ScheduledEvent):
params['schedule_expression'] = resource.schedule_expression
else:
params['event_pattern'] = resource.event_pattern

plan = [
models.APICall(
method_name='get_or_create_rule_arn',
params={'rule_name': resource.rule_name,
'event_pattern': resource.event_pattern},
params=params,
output_var='rule-arn',
),
models.APICall(
Expand All @@ -589,51 +594,21 @@ def _plan_cloudwatchevent(self, resource):
# You need to remove targets (which have IDs)
# before you can delete a rule.
models.RecordResourceValue(
resource_type='cloudwatch_event',
resource_type=resource.resource_type,
resource_name=resource.resource_name,
name='rule_name',
value=resource.rule_name,
)
]
return plan

def _plan_cloudwatchevent(self, resource):
# type: (models.CloudWatchEvent) -> Sequence[InstructionMsg]
return self._create_cloudwatchevent(resource)

def _plan_scheduledevent(self, resource):
# type: (models.ScheduledEvent) -> Sequence[InstructionMsg]
function_arn = Variable(
'%s_lambda_arn' % resource.lambda_function.resource_name
)
# Because the underlying API calls have PUT semantics,
# we don't have to check if the resource exists and have
# a separate code path for updates. We could however
# check if the resource exists to avoid unnecessary API
# calls, but that's a later optimization.
plan = [
models.APICall(
method_name='get_or_create_rule_arn',
params={'rule_name': resource.rule_name,
'schedule_expression': resource.schedule_expression},
output_var='rule-arn',
),
models.APICall(
method_name='connect_rule_to_lambda',
params={'rule_name': resource.rule_name,
'function_arn': function_arn}
),
models.APICall(
method_name='add_permission_for_cloud_watch_event',
params={'rule_arn': Variable('rule-arn'),
'function_arn': function_arn},
),
# You need to remove targets (which have IDs)
# before you can delete a rule.
models.RecordResourceValue(
resource_type='cloudwatch_event',
resource_name=resource.resource_name,
name='rule_name',
value=resource.rule_name,
)
]
return plan
return self._create_cloudwatchevent(resource)

def _create_websocket_function_configs(self, resource):
# type: (models.WebsocketAPI) -> Dict[str, Dict[str, Any]]
Expand Down
17 changes: 16 additions & 1 deletion docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@ Chalice
entire lambda function name. This parameter is optional. If it is
not provided, the name of the python function will be used.

.. method:: on_cw_event(pattern, name=None)

Create a lambda function and configure it to be invoked whenever
an event that matches the given pattern flows through Cloud Watch Events
or Event Bridge.

:param pattern: The event pattern to use to filter subscribed events.
See the Cloud Watch Events docs for examples https://amzn.to/2OlqZso

:param name: The name of the function to create. This name is combined
with the chalice app name as well as the stage name to create the
entire lambda function name. This parameter is optional. If it is
not provided, the name of the python function will be used.

.. method:: on_s3_event(bucket, events=None, prefix=None, suffix=None, name=None)

Create a lambda function and configure it to be automatically invoked
Expand Down Expand Up @@ -865,7 +879,7 @@ Event Sources
.. class:: CloudWatchEvent()

This is the input argument for a scheduled event.
This is the input argument for a scheduled or cloud watch events.

.. code-block:: python
Expand All @@ -891,6 +905,7 @@ Event Sources

.. attribute:: detail

For cloud watch events this will be the event payload.
For scheduled events, this will be an empty dictionary.

.. attribute:: detail_type
Expand Down
3 changes: 3 additions & 0 deletions docs/source/topics/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ In this example, we have a single lambda function that we subscribe to all
events from the AWS Code Commit service. The first parameter to the decorator
is the event pattern that will be used to filter the events sent to the function.

See the Cloud Watch Event pattern docs for additional syntax and examples
https://amzn.to/2OlqZso

The function you decorate must accept a single argument,
which will be of type :class:`CloudWatchEvent`.

Expand Down
4 changes: 2 additions & 2 deletions tests/unit/deploy/test_planner.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ def test_can_plan_cloud_watch_event(self):
)
)
assert plan[3] == models.RecordResourceValue(
resource_type='cloudwatch_event',
resource_type='cloud_watch_event',
resource_name='bar',
name='rule_name',
value='myrulename',
Expand Down Expand Up @@ -533,7 +533,7 @@ def test_can_plan_scheduled_event(self):
)
)
assert plan[3] == models.RecordResourceValue(
resource_type='cloudwatch_event',
resource_type='scheduled_event',
resource_name='bar',
name='rule_name',
value='myrulename',
Expand Down

0 comments on commit e8f03e7

Please sign in to comment.