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: Input transformer for AWS::Serverless::Function.EventBridgeRule #3283

Merged
merged 10 commits into from
Aug 2, 2023
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved
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] = eventbridgeruleeventproperties("InputTransformer")
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved


class EventBridgeRuleEvent(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion samtranslator/internal/schema_source/sam-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@
"RetryPolicy": "A `RetryPolicy` object that includes information about the retry policy settings\\. For more information, see [Event retry policy and using dead\\-letter queues](https://docs.aws.amazon.com/eventbridge/latest/userguide/rule-dlq.html) in the *Amazon EventBridge User Guide*\\. \n*Type*: [RetryPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html#cfn-events-rule-target-retrypolicy) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`RetryPolicy`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html#cfn-events-rule-target-retrypolicy) property of the `AWS::Events::Rule` `Target` data type\\.",
"RuleName": "The name of the rule\\. \n*Type*: String \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the [`Name`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-name) property of an `AWS::Events::Rule` resource\\.",
"State": "The state of the rule\\. \n*Accepted values*: `DISABLED | ENABLED` \n*Type*: String \n*Required*: No \n*AWS CloudFormation compatibility*: This property is passed directly to the `[State](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-state) ` property of an `AWS::Events::Rule` resource\\.",
"Target": "The AWS resource that EventBridge invokes when a rule is triggered\\. You can use this property to specify the logical ID of the target\\. If this property is not specified, then AWS SAM generates the logical ID of the target\\. \n*Type*: [Target](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-target.html) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is similar to the [`Targets`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-targets) property of an `AWS::Events::Rule` resource\\. The AWS SAM version of this property only allows you to specify the logical ID of a single target\\."
"Target": "The AWS resource that EventBridge invokes when a rule is triggered\\. You can use this property to specify the logical ID of the target\\. If this property is not specified, then AWS SAM generates the logical ID of the target\\. \n*Type*: [Target](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-target.html) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is similar to the [`Targets`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-events-rule.html#cfn-events-rule-targets) property of an `AWS::Events::Rule` resource\\. The AWS SAM version of this property only allows you to specify the logical ID of a single target\\.",
"InputTransformer": ""
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved
ConnorRobertson marked this conversation as resolved.
Show resolved Hide resolved
},
"sam-property-function-eventinvokeconfiguration": {
"DestinationConfig": "A configuration object that specifies the destination of an event after Lambda processes it\\. \n*Type*: [EventInvokeDestinationConfiguration](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-eventinvokedestinationconfiguration.html) \n*Required*: No \n*AWS CloudFormation compatibility*: This property is similar to the [`DestinationConfig`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-eventinvokeconfig-destinationconfig.html) property of an `AWS::Lambda::EventInvokeConfig` resource\\. SAM requires an extra parameter, \"Type\", that does not exist in CloudFormation\\.",
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
9 changes: 9 additions & 0 deletions samtranslator/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -246854,6 +246854,15 @@
"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": {
"allOf": [
{
"$ref": "#/definitions/PassThroughProp"
}
],
"markdownDescription": "",
"title": "InputTransformer"
},
"Pattern": {
"allOf": [
{
Expand Down
9 changes: 9 additions & 0 deletions schema_source/sam.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5086,6 +5086,15 @@
"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": {
"allOf": [
{
"$ref": "#/definitions/PassThroughProp"
}
],
"markdownDescription": "",
"title": "InputTransformer"
},
"Pattern": {
"allOf": [
{
Expand Down
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