Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 11 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class CloudWatchEvent(PushEventSource):
"Input": PropertyType(False, is_str()),
"InputPath": PropertyType(False, is_str()),
"Target": PropertyType(False, is_type(dict)),
"Enabled": PropertyType(False, is_type(bool)),
"State": PropertyType(False, is_str()),
}

@cw_timer(prefix=FUNCTION_EVETSOURCE_METRIC_PREFIX)
Expand Down Expand Up @@ -218,6 +220,15 @@ def to_cloudformation(self, **kwargs):
)
resources.extend(dlq_resources)

if self.State and self.Enabled is not None:
raise InvalidEventException(self.relative_id, "State and Enabled Properties cannot both be specified.")

if self.State:
events_rule.State = self.State

if self.Enabled is not None:
events_rule.State = "ENABLED" if self.Enabled else "DISABLED"

events_rule.Targets = [self._construct_target(function, dlq_queue_arn)]

resources.append(events_rule)
Expand Down
31 changes: 31 additions & 0 deletions tests/model/eventsources/test_eventbridge_rule_source.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from unittest.mock import Mock, patch
from unittest import TestCase
from parameterized import parameterized

from samtranslator.model.eventsources.push import EventBridgeRule
from samtranslator.model.lambda_ import LambdaFunction
Expand Down Expand Up @@ -82,3 +83,33 @@ def test_to_cloudformation_with_dlq_generated_with_intrinsic_function_custom_log
self.eb_event_source.DeadLetterConfig = dead_letter_config
with self.assertRaises(InvalidEventException):
self.eb_event_source.to_cloudformation(function=self.func)

def test_to_cloudformation_transforms_enabled_boolean_to_state(self):
self.eb_event_source.Enabled = True
resources = self.eb_event_source.to_cloudformation(function=self.func)
self.assertEqual(len(resources), 2)
event_rule = resources[0]
self.assertEqual(event_rule.State, "ENABLED")

self.eb_event_source.Enabled = False
resources = self.eb_event_source.to_cloudformation(function=self.func)
self.assertEqual(len(resources), 2)
event_rule = resources[0]
self.assertEqual(event_rule.State, "DISABLED")

@parameterized.expand(
[
(True, "ENABLED"),
(True, "DISABLED"),
(True, {"FN:FakeIntrinsic": "something"}),
(False, "ENABLED"),
(False, "DISABLED"),
(False, {"FN:FakeIntrinsic": "something"}),
]
)
def test_to_cloudformation_invalid_defined_both_enabled_and_state_provided(self, enabled_value, state_value):
self.maxDiff = None
self.eb_event_source.Enabled = enabled_value
self.eb_event_source.State = state_value
with self.assertRaises(InvalidEventException):
self.eb_event_source.to_cloudformation(function=self.func)
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Transform: "AWS::Serverless-2016-10-31"

Resources:
TestBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: test-bucket
TestFunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: test-function
InlineCode: |
exports.handler = async (event) => {
return 'Hello from Lambda!';
};
Handler: index.handler
Runtime: nodejs16.x
Events:
TestEventBridgeRule:
Type: EventBridgeRule
Properties:
State: ENABLED
Enabled: true
Pattern:
source:
- aws.s3
detail-type:
- Object Created
detail:
bucket:
name:
- "test-bucket"
object:
key:
- prefix: "/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Transform: "AWS::Serverless-2016-10-31"

Resources:
TestBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: test-bucket
TestFunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: test-function
InlineCode: |
exports.handler = async (event) => {
return 'Hello from Lambda!';
};
Handler: index.handler
Runtime: nodejs16.x
Events:
TestEventBridgeRule:
Type: EventBridgeRule
Properties:
State: ENABLED
Pattern:
source:
- aws.s3
detail-type:
- Object Created
detail:
bucket:
name:
- "test-bucket"
object:
key:
- prefix: "/"
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"Resources": {
"TestBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "test-bucket"
}
},
"TestFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async (event) => {\n return 'Hello from Lambda!';\n};\n"
},
"FunctionName": "test-function",
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"TestFunctionRole",
"Arn"
]
},
"Runtime": "nodejs16.x",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"TestFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"TestFunctionTestEventBridgeRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"EventPattern": {
"source": [
"aws.s3"
],
"detail-type": [
"Object Created"
],
"detail": {
"bucket": {
"name": [
"test-bucket"
]
},
"object": {
"key": [
{
"prefix": "/"
}
]
}
}
},
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"TestFunction",
"Arn"
]
},
"Id": "TestFunctionTestEventBridgeRuleLambdaTarget"
}
]
}
},
"TestFunctionTestEventBridgeRulePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "TestFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"TestFunctionTestEventBridgeRule",
"Arn"
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
{
"Resources": {
"TestBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "test-bucket"
}
},
"TestFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = async (event) => {\n return 'Hello from Lambda!';\n};\n"
},
"FunctionName": "test-function",
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"TestFunctionRole",
"Arn"
]
},
"Runtime": "nodejs16.x",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"TestFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"TestFunctionTestEventBridgeRule": {
"Type": "AWS::Events::Rule",
"Properties": {
"EventPattern": {
"source": [
"aws.s3"
],
"detail-type": [
"Object Created"
],
"detail": {
"bucket": {
"name": [
"test-bucket"
]
},
"object": {
"key": [
{
"prefix": "/"
}
]
}
}
},
"State": "ENABLED",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"TestFunction",
"Arn"
]
},
"Id": "TestFunctionTestEventBridgeRuleLambdaTarget"
}
]
}
},
"TestFunctionTestEventBridgeRulePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "TestFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"TestFunctionTestEventBridgeRule",
"Arn"
]
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [TestFunction] is invalid. Event with id [TestEventBridgeRule] is invalid. State and Enabled Properties cannot both be specified."}
Loading