Skip to content

Commit

Permalink
feat: Input transformer for AWS::Serverless::Function.EventBridgeRule (
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorRobertson committed Aug 2, 2023
1 parent cb20917 commit a040f3c
Show file tree
Hide file tree
Showing 8 changed files with 627 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ class EventBridgeRuleEventProperties(BaseModel):
Pattern: PassThroughProp = eventbridgeruleeventproperties("Pattern")
RetryPolicy: Optional[PassThroughProp] = eventbridgeruleeventproperties("RetryPolicy")
Target: Optional[EventBridgeRuleTarget] = eventbridgeruleeventproperties("Target")
InputTransformer: Optional[PassThroughProp] # TODO: add docs


class EventBridgeRuleEvent(BaseModel):
Expand Down
5 changes: 5 additions & 0 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ class CloudWatchEvent(PushEventSource):
"Target": PropertyType(False, IS_DICT),
"Enabled": PropertyType(False, IS_BOOL),
"State": PropertyType(False, IS_STR),
"InputTransformer": PropertyType(False, IS_DICT),
}

EventBusName: Optional[PassThrough]
Expand All @@ -219,6 +220,7 @@ class CloudWatchEvent(PushEventSource):
Target: Optional[PassThrough]
Enabled: Optional[bool]
State: Optional[PassThrough]
InputTransformer: Optional[PassThrough]

@cw_timer(prefix=FUNCTION_EVETSOURCE_METRIC_PREFIX)
def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
Expand Down Expand Up @@ -291,6 +293,9 @@ def _construct_target(self, function, dead_letter_queue_arn=None): # type: igno
if self.RetryPolicy is not None:
target["RetryPolicy"] = self.RetryPolicy

if self.InputTransformer is not None:
target["InputTransformer"] = self.InputTransformer

return target


Expand Down
3 changes: 3 additions & 0 deletions samtranslator/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -246854,6 +246854,9 @@
"markdownDescription": "When you don't want to pass the entire matched event to the target, use the `InputPath` property to describe which part of the event to pass\\. \n*Type*: String \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`InputPath`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html#cfn-events-rule-target-inputpath) property of an `AWS::Events::Rule Target` resource\\.",
"title": "InputPath"
},
"InputTransformer": {
"$ref": "#/definitions/PassThroughProp"
},
"Pattern": {
"allOf": [
{
Expand Down
3 changes: 3 additions & 0 deletions schema_source/sam.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5086,6 +5086,9 @@
"markdownDescription": "When you don't want to pass the entire matched event to the target, use the `InputPath` property to describe which part of the event to pass\\. \n*Type*: String \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`InputPath`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html#cfn-events-rule-target-inputpath) property of an `AWS::Events::Rule Target` resource\\.",
"title": "InputPath"
},
"InputTransformer": {
"$ref": "#/definitions/PassThroughProp"
},
"Pattern": {
"allOf": [
{
Expand Down
39 changes: 39 additions & 0 deletions tests/translator/input/eventbridgerule_with_input_transformer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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
Target:
Id: hello345
InputTransformer:
InputPathsMap:
instance: $.detail.instance-id
state: $.detail.state
InputTemplate: |
{
"instance" : <instance>,
"state" : <state>,
"instanceStatus": "instance \"<instance>\" is in <state>"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
{
"Resources": {
"ScheduledFunction": {
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip",
"S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"ScheduledFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"ScheduledFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
},
"ScheduledFunctionSchedule": {
"Properties": {
"ScheduleExpression": "rate(1 minute)",
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"ScheduledFunction",
"Arn"
]
},
"Id": "ScheduledFunctionScheduleLambdaTarget"
}
]
},
"Type": "AWS::Events::Rule"
},
"ScheduledFunctionSchedulePermission": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ScheduledFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"ScheduledFunctionSchedule",
"Arn"
]
}
},
"Type": "AWS::Lambda::Permission"
},
"TriggeredFunction": {
"Properties": {
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Key": "hello.zip",
"S3ObjectVersion": "3Tcgv52_0GaDvhDva4YciYeqRyPnpIcO"
},
"Handler": "hello.handler",
"Role": {
"Fn::GetAtt": [
"TriggeredFunctionRole",
"Arn"
]
},
"Runtime": "python2.7",
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::Lambda::Function"
},
"TriggeredFunctionOnTerminate": {
"Properties": {
"EventBusName": "ExternalEventBridge",
"EventPattern": {
"detail": {
"state": [
"terminated"
]
}
},
"Targets": [
{
"Arn": {
"Fn::GetAtt": [
"TriggeredFunction",
"Arn"
]
},
"Id": "hello345",
"InputTransformer": {
"InputPathsMap": {
"instance": "$.detail.instance-id",
"state": "$.detail.state"
},
"InputTemplate": "{\n\"instance\" : <instance>,\n\"state\" : <state>,\n\"instanceStatus\": \"instance \\\"<instance>\\\" is in <state>\"\n}\n"
}
}
]
},
"Type": "AWS::Events::Rule"
},
"TriggeredFunctionOnTerminatePermission": {
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "TriggeredFunction"
},
"Principal": "events.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"TriggeredFunctionOnTerminate",
"Arn"
]
}
},
"Type": "AWS::Lambda::Permission"
},
"TriggeredFunctionRole": {
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": [
"sts:AssumeRole"
],
"Effect": "Allow",
"Principal": {
"Service": [
"lambda.amazonaws.com"
]
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
},
"Type": "AWS::IAM::Role"
}
}
}
Loading

0 comments on commit a040f3c

Please sign in to comment.