Skip to content
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

feat: Add additional properties for Cloudwatch Schedule - Enabled, Name, Description #1006

Merged
merged 4 commits into from
Jul 16, 2019
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
3 changes: 3 additions & 0 deletions docs/cloudformation_compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ Schedule
======================== ================================== ========================
Schedule All
Input All
Name All
Description All
Enabled All
======================== ================================== ========================

CloudWatchEvent
Expand Down
9 changes: 8 additions & 1 deletion samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ class Schedule(PushEventSource):
principal = 'events.amazonaws.com'
property_types = {
'Schedule': PropertyType(True, is_str()),
'Input': PropertyType(False, is_str())
'Input': PropertyType(False, is_str()),
'Enabled': PropertyType(False, is_type(bool)),
'Name': PropertyType(False, is_str()),
'Description': PropertyType(False, is_str())
}

def to_cloudformation(self, **kwargs):
Expand All @@ -93,6 +96,10 @@ def to_cloudformation(self, **kwargs):
resources.append(events_rule)

events_rule.ScheduleExpression = self.Schedule
if self.Enabled is not None:
events_rule.State = "ENABLED" if self.Enabled else "DISABLED"
events_rule.Name = self.Name
events_rule.Description = self.Description
events_rule.Targets = [self._construct_target(function)]

source_arn = events_rule.get_runtime_attr("arn")
Expand Down
11 changes: 10 additions & 1 deletion samtranslator/validator/sam_schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,16 @@
},
"Schedule": {
"type": "string"
}
},
"Name": {
"type": "string"
},
"Description": {
"type": "string"
},
"Enabled": {
"type": "boolean"
}
},
"required": [
"Schedule"
Expand Down
30 changes: 30 additions & 0 deletions tests/translator/input/cloudwatchevent_schedule_properties.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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)'
Name: test-schedule
Description: Test Schedule
Enabled: True

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: CloudWatchEvent
Properties:
Pattern:
detail:
state:
- terminated
4 changes: 2 additions & 2 deletions tests/translator/output/aws-cn/cloudwatchevent.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"ScheduledFunctionSchedule": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"ScheduleExpression": "rate(1 minute)",
"Targets": [
{
"Id": "ScheduledFunctionScheduleLambdaTarget",
Expand All @@ -169,4 +169,4 @@
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"Resources": {
"ScheduledFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}
}
},
"TriggeredFunctionOnTerminatePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "events.amazonaws.com",
"FunctionName": {
"Ref": "TriggeredFunction"
},
"SourceArn": {
"Fn::GetAtt": [
"TriggeredFunctionOnTerminate",
"Arn"
]
}
}
},
"ScheduledFunctionSchedulePermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:invokeFunction",
"Principal": "events.amazonaws.com",
"FunctionName": {
"Ref": "ScheduledFunction"
},
"SourceArn": {
"Fn::GetAtt": [
"ScheduledFunctionSchedule",
"Arn"
]
}
}
},
"ScheduledFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip",
"S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"ScheduledFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"TriggeredFunctionOnTerminate": {
"Type": "AWS::Events::Rule",
"Properties": {
"EventPattern": {
"detail": {
"state": [
"terminated"
]
}
},
"Targets": [
{
"Id": "TriggeredFunctionOnTerminateLambdaTarget",
"Arn": {
"Fn::GetAtt": [
"TriggeredFunction",
"Arn"
]
}
}
]
}
},
"TriggeredFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip",
"S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"TriggeredFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
]
}
},
"TriggeredFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
]
}
}
},
"ScheduledFunctionSchedule": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"Name": "test-schedule",
"Description": "Test Schedule",
"State": "ENABLED",
"Targets": [
{
"Id": "ScheduledFunctionScheduleLambdaTarget",
"Arn": {
"Fn::GetAtt": [
"ScheduledFunction",
"Arn"
]
}
}
]
}
}
}
}
4 changes: 2 additions & 2 deletions tests/translator/output/aws-us-gov/cloudwatchevent.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
"ScheduledFunctionSchedule": {
"Type": "AWS::Events::Rule",
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"ScheduleExpression": "rate(1 minute)",
"Targets": [
{
"Id": "ScheduledFunctionScheduleLambdaTarget",
Expand All @@ -169,4 +169,4 @@
}
}
}
}
}
Loading