Skip to content

Commit

Permalink
Add new EventBridgeRule SAM Function EventSource superseding existing…
Browse files Browse the repository at this point in the history
… CloudWatchEvent
  • Loading branch information
nikp authored and keetonian committed Dec 6, 2019
1 parent bbccae9 commit b31e4b9
Show file tree
Hide file tree
Showing 37 changed files with 1,027 additions and 101 deletions.
12 changes: 11 additions & 1 deletion docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,17 @@ Description All
Enabled All
======================== ================================== ========================

CloudWatchEvent
CloudWatchEvent (superseded by EventBridgeRule, see below)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
======================== ================================== ========================
Property Name Intrinsic(s) Supported Reasons
======================== ================================== ========================
Pattern All
Input All
InputPath All
======================== ================================== ========================

EventBridgeRule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
======================== ================================== ========================
Property Name Intrinsic(s) Supported Reasons
Expand Down
41 changes: 39 additions & 2 deletions docs/internals/generated_resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ AWS::Lambda::Permission MyFunction\ **MyTimer**\ Permission
AWS::Events::Rule MyFunction\ **MyTimer**
================================== ================================

CloudWatchEvent
CloudWatchEvent (superseded by EventBridgeRule, see below)
^^^^^^^^^^^^^^^

Example:
Expand All @@ -401,9 +401,13 @@ Example:
Type: CloudWatchEvent
Properties:
Pattern:
source:
- aws.ec2
detail-type:
- EC2 Instance State-change Notification
detail:
state:
- terminated
- terminated
...
Additional generated resources:
Expand All @@ -415,6 +419,39 @@ AWS::Lambda::Permission MyFunction\ **OnTerminate**\ Permission
AWS::Events::Rule MyFunction\ **OnTerminate**
================================== ================================

EventBridgeRule
^^^^^^^^^^^^^^^

Example:

.. code:: yaml
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
OnTerminate:
Type: EventBridgeRule
Properties:
Pattern:
source:
- aws.ec2
detail-type:
- EC2 Instance State-change Notification
detail:
state:
- terminated
...
Additional generated resources:

================================== ================================
CloudFormation Resource Type Logical ID
================================== ================================
AWS::Lambda::Permission MyFunction\ **OnTerminate**\ Permission
AWS::Events::Rule MyFunction\ **OnTerminate**
================================== ================================

AWS::Serverless::Api
--------------------
Expand Down
3 changes: 3 additions & 0 deletions examples/2016-10-31/cloudwatch-event-to-msteams/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CloudWatch Events has been re-launched as Amazon EventBridge with full backwards compatibility

Please see ../eventbridge-event-to-msteams/ for the migrated code
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Resources:
Resource: '*'
Events:
WatchSecurityGroupRule:
Type: CloudWatchEvent
Type: EventBridgeRule
Description: Detects EC2 Security Group Events to Send to Teams
Properties:
EventBusName: event-bus-name
Expand Down
2 changes: 1 addition & 1 deletion examples/2016-10-31/schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The `ScheduledFunction` is of type `AWS::Serverless::Function`. This is a Server

`AWS::Lambda::Function` is the top-level CloudFormation resource to define an Amazon Lambda function. Because we want to schedule the function’s periodic execution, we include an `Events` property on our `AWS::Serverless::Function` resource. This allows us to define the function execution schedule *within* the context of the function’s properties. Behind-the-scenes, the `Events` property expands into a `AWS::Events::Rule` resource with an invocation rate of once every 5 minutes.

Lastly, in order for the CloudWatch Events API to invoke our function, it needs permissions to do so. `AWS::Lambda::Permission` grants CloudWatch Events the permission to invoke our function.
Lastly, in order for the EventBridge API to invoke our function, it needs permissions to do so. `AWS::Lambda::Permission` grants CloudWatch Events the permission to invoke our function.

### Deployment

Expand Down
39 changes: 2 additions & 37 deletions examples/apps/logicmonitor-send-cloudwatch-events/readme
Original file line number Diff line number Diff line change
@@ -1,38 +1,3 @@
'''
This function handles a Slack slash command and echoes the details back to the user.
CloudWatch Events has been re-launched as Amazon EventBridge with full backwards compatibility

Follow these steps to configure the slash command in Slack:

1. Navigate to https://<your-team-domain>.slack.com/services/new

2. Search for and select "Slash Commands".

3. Enter a name for your command and click "Add Slash Command Integration".

4. Copy the token string from the integration settings and use it in the next section.

5. After you complete this blueprint, enter the provided API endpoint URL in the URL field.


To encrypt your secrets use the following steps:

1. Create or use an existing KMS Key - http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html

2. Click the "Enable Encryption Helpers" checkbox

3. Paste <COMMAND_TOKEN> into the kmsEncryptedToken environment variable and click encrypt


Follow these steps to complete the configuration of your command API endpoint

1. When completing the blueprint configuration select "Open" for security
on the "Configure triggers" page.

2. Enter a name for your execution role in the "Role name" field.
Your function's execution role needs kms:Decrypt permissions. We have
pre-selected the "KMS decryption permissions" policy template that will
automatically add these permissions.

3. Update the URL for your Slack slash command with the invocation URL for the
created API resource in the prod stage.
'''
Please see ../logicmonitor-send-eventbridge-events/ for the migrated code
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''
This function uses the LogicMonitor REST API to create LogicMonitor OpsNotes
for CloudWatch Events.
for EventBridge Events.
Once the Ops Notes have been created for monitored resources that have
associated events, you'll see them on all graphs for those resources.
Assuming you already have monitored AWS resources in LogicMonitor, you'll
Expand Down Expand Up @@ -40,13 +40,12 @@
ACCOUNT_NAME environment variable and click encrypt.
Follow these steps to configure CloudWatch Events Rules that will trigger
Follow these steps to configure EventBridge Rules that will trigger
the Lambda Function:
1. Create a CloudWatch Event Rule that matches the events you'd like
1. Create an EventBridge Rule that matches the events you'd like
to show up in your LogicMonitor account
https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/
WhatIsCloudWatchEvents.html
https://docs.aws.amazon.com/eventbridge/latest/userguide/what-is-amazon-eventbridge.html
2. Configure the rule to trigger this Lambda Function.
Expand Down
38 changes: 38 additions & 0 deletions examples/apps/logicmonitor-send-eventbridge-events/readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'''
This function handles a Slack slash command and echoes the details back to the user.

Follow these steps to configure the slash command in Slack:

1. Navigate to https://<your-team-domain>.slack.com/services/new

2. Search for and select "Slash Commands".

3. Enter a name for your command and click "Add Slash Command Integration".

4. Copy the token string from the integration settings and use it in the next section.

5. After you complete this blueprint, enter the provided API endpoint URL in the URL field.


To encrypt your secrets use the following steps:

1. Create or use an existing KMS Key - http://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html

2. Click the "Enable Encryption Helpers" checkbox

3. Paste <COMMAND_TOKEN> into the kmsEncryptedToken environment variable and click encrypt


Follow these steps to complete the configuration of your command API endpoint

1. When completing the blueprint configuration select "Open" for security
on the "Configure triggers" page.

2. Enter a name for your execution role in the "Role name" field.
Your function's execution role needs kms:Decrypt permissions. We have
pre-selected the "KMS decryption permissions" policy template that will
automatically add these permissions.

3. Update the URL for your Slack slash command with the invocation URL for the
created API resource in the prod stage.
'''
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: >-
Creates LogicMonitor OpsNotes for CloudWatch Events, thereby enabling
Creates LogicMonitor OpsNotes for EventBridge Events, thereby enabling
correlation between events and performance data.
Parameters:
KeyIdParameter:
Type: String
Resources:
logicmonitorsendcloudwatchevents:
logicmonitorsendeventbridgeevents:
Type: 'AWS::Serverless::Function'
Properties:
Handler: lambda_function.lambda_handler
Runtime: python2.7
CodeUri: .
Description: >-
Creates LogicMonitor OpsNotes for CloudWatch Events, thereby enabling
Creates LogicMonitor OpsNotes for EventBridge Events, thereby enabling
correlation between events and performance data.
MemorySize: 128
Timeout: 30
Expand Down
19 changes: 12 additions & 7 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Schedule(PushEventSource):
}

def to_cloudformation(self, **kwargs):
"""Returns the CloudWatch Events Rule and Lambda Permission to which this Schedule event source corresponds.
"""Returns the EventBridge Rule and Lambda Permission to which this Schedule event source corresponds.
:param dict kwargs: no existing resources need to be modified
:returns: a list of vanilla CloudFormation Resources, to which this Schedule event expands
Expand Down Expand Up @@ -124,7 +124,7 @@ def to_cloudformation(self, **kwargs):
return resources

def _construct_target(self, function):
"""Constructs the Target property for the CloudWatch Events Rule.
"""Constructs the Target property for the EventBridge Rule.
:returns: the Target property
:rtype: dict
Expand All @@ -140,7 +140,7 @@ def _construct_target(self, function):


class CloudWatchEvent(PushEventSource):
"""CloudWatch Events event source for SAM Functions."""
"""CloudWatch Events/EventBridge event source for SAM Functions."""
resource_type = 'CloudWatchEvent'
principal = 'events.amazonaws.com'
property_types = {
Expand All @@ -151,11 +151,11 @@ class CloudWatchEvent(PushEventSource):
}

def to_cloudformation(self, **kwargs):
"""Returns the CloudWatch Events Rule and Lambda Permission to which this CloudWatch Events event source
corresponds.
"""Returns the CloudWatch Events/EventBridge Rule and Lambda Permission to which
this CloudWatch Events/EventBridge event source corresponds.
:param dict kwargs: no existing resources need to be modified
:returns: a list of vanilla CloudFormation Resources, to which this CloudWatch Events event expands
:returns: a list of vanilla CloudFormation Resources, to which this CloudWatch Events/EventBridge event expands
:rtype: list
"""
function = kwargs.get('function')
Expand All @@ -180,7 +180,7 @@ def to_cloudformation(self, **kwargs):
return resources

def _construct_target(self, function):
"""Constructs the Target property for the CloudWatch Events Rule.
"""Constructs the Target property for the CloudWatch Events/EventBridge Rule.
:returns: the Target property
:rtype: dict
Expand All @@ -197,6 +197,11 @@ def _construct_target(self, function):
return target


class EventBridgeRule(CloudWatchEvent):
"""EventBridge Rule event source for SAM Functions."""
resource_type = 'EventBridgeRule'


class S3(PushEventSource):
"""S3 bucket event source for SAM Functions."""
resource_type = 'S3'
Expand Down
21 changes: 21 additions & 0 deletions samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,24 @@
],
"type": "object"
},
"AWS::Serverless::Function.EventBridgeRule": {
"additionalProperties": false,
"properties": {
"Input": {
"type": "string"
},
"InputPath": {
"type": "string"
},
"Pattern": {
"type": "object"
}
},
"required": [
"Pattern"
],
"type": "object"
},
"AWS::Serverless::Function.LogEvent": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -471,6 +489,9 @@
{
"$ref": "#/definitions/AWS::Serverless::Function.CloudWatchEventEvent"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.EventBridgeRule"
},
{
"$ref": "#/definitions/AWS::Serverless::Function.LogEvent"
},
Expand Down
27 changes: 27 additions & 0 deletions tests/translator/input/eventbridgerule.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Resources:
ScheduledFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip?versionId=3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO
Handler: hello.handler
Runtime: python2.7
Events:
Schedule:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
TriggeredFunction:
Type: 'AWS::Serverless::Function'
Properties:
CodeUri: s3://sam-demo-bucket/hello.zip?versionId=3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO
Handler: hello.handler
Runtime: python2.7
Events:
OnTerminate:
Type: EventBridgeRule
Properties:
EventBusName: ExternalEventBridge
Pattern:
detail:
state:
- terminated
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Resources:
Runtime: python2.7
Events:
OnTerminate:
Type: CloudWatchEvent
Type: EventBridgeRule
Properties:
Pattern:
detail:
Expand Down
10 changes: 9 additions & 1 deletion tests/translator/input/function_event_conditions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Resources:
AutoPublishAlias: Live

Events:
CWSchedule:
EBSchedule:
Type: Schedule
Properties:
Schedule: 'rate(1 minute)'
Expand All @@ -46,6 +46,14 @@ Resources:
state:
- terminated

EBRule:
Type: EventBridgeRule
Properties:
Pattern:
detail:
state:
- launched

CWLog:
Type: CloudWatchLogs
Properties:
Expand Down
Loading

0 comments on commit b31e4b9

Please sign in to comment.