Skip to content

Commit 1174f0f

Browse files
authored
Fix DestinationConfig in streaming event sources (#2215)
1 parent cc28051 commit 1174f0f

13 files changed

+287
-4
lines changed

samtranslator/model/eventsources/pull.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ def to_cloudformation(self, **kwargs):
110110

111111
destination_config_policy = None
112112
if self.DestinationConfig:
113+
if self.DestinationConfig.get("OnFailure") is None:
114+
raise InvalidEventException(self.logical_id, "'OnFailure' is a required field for 'DestinationConfig'")
115+
113116
# `Type` property is for sam to attach the right policies
114117
destination_type = self.DestinationConfig.get("OnFailure").get("Type")
115118

@@ -120,10 +123,6 @@ def to_cloudformation(self, **kwargs):
120123
# the values 'SQS' and 'SNS' are allowed. No intrinsics are allowed
121124
if destination_type not in ["SQS", "SNS"]:
122125
raise InvalidEventException(self.logical_id, "The only valid values for 'Type' are 'SQS' and 'SNS'")
123-
if self.DestinationConfig.get("OnFailure") is None:
124-
raise InvalidEventException(
125-
self.logical_id, "'OnFailure' is a required field for " "'DestinationConfig'"
126-
)
127126
if destination_type == "SQS":
128127
queue_arn = self.DestinationConfig.get("OnFailure").get("Destination")
129128
destination_config_policy = IAMRolePolicies().sqs_send_message_role_policy(
@@ -134,6 +133,7 @@ def to_cloudformation(self, **kwargs):
134133
destination_config_policy = IAMRolePolicies().sns_publish_role_policy(
135134
sns_topic_arn, self.logical_id
136135
)
136+
137137
lambda_eventsourcemapping.DestinationConfig = self.DestinationConfig
138138

139139
if "role" in kwargs:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Parameters:
5+
MyBatchingWindowParam:
6+
Type: Number
7+
Default: 45
8+
Description: parameter for batching window in seconds
9+
10+
Resources:
11+
MyFunction:
12+
Type: AWS::Serverless::Function
13+
Properties:
14+
Handler: index.handler
15+
InlineCode: |
16+
exports.handler = async (event) => {
17+
return {
18+
statusCode: 200,
19+
body: JSON.stringify(event),
20+
headers: {}
21+
}
22+
}
23+
Runtime: nodejs12.x
24+
Policies:
25+
- SQSSendMessagePolicy:
26+
QueueName: !GetAtt MySqsQueue.QueueName
27+
Events:
28+
StreamEvent:
29+
Type: Kinesis
30+
Properties:
31+
Stream: !GetAtt KinesisStream.Arn
32+
MaximumBatchingWindowInSeconds: !Ref MyBatchingWindowParam
33+
StartingPosition: LATEST
34+
DestinationConfig:
35+
OnFailure:
36+
Type: INVALID_VALID
37+
Destination: !Ref MySnsTopic
38+
39+
KinesisStream:
40+
Type: AWS::Kinesis::Stream
41+
Properties:
42+
ShardCount: 1
43+
44+
MySnsTopic:
45+
Type: AWS::SNS::Topic
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
4+
Parameters:
5+
MyBatchingWindowParam:
6+
Type: Number
7+
Default: 45
8+
Description: parameter for batching window in seconds
9+
10+
Resources:
11+
MyFunction:
12+
Type: AWS::Serverless::Function
13+
Properties:
14+
Handler: index.handler
15+
InlineCode: |
16+
exports.handler = async (event) => {
17+
return {
18+
statusCode: 200,
19+
body: JSON.stringify(event),
20+
headers: {}
21+
}
22+
}
23+
Runtime: nodejs12.x
24+
Policies:
25+
- SQSSendMessagePolicy:
26+
QueueName: !GetAtt MySqsQueue.QueueName
27+
Events:
28+
StreamEvent:
29+
Type: Kinesis
30+
Properties:
31+
Stream: !GetAtt KinesisStream.Arn
32+
MaximumBatchingWindowInSeconds: !Ref MyBatchingWindowParam
33+
StartingPosition: LATEST
34+
DestinationConfig:
35+
InvalidConfig:
36+
Type: SNS
37+
Destination: !Ref MySnsTopic
38+
39+
KinesisStream:
40+
Type: AWS::Kinesis::Stream
41+
Properties:
42+
ShardCount: 1
43+
44+
MySnsTopic:
45+
Type: AWS::SNS::Topic

tests/translator/input/function_with_event_source_mapping.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ Resources:
5959
OnFailure:
6060
Type: SQS
6161
Destination: !GetAtt MySqsQueue.Arn
62+
StreamEventWithoutDestinationConfigType:
63+
Type: Kinesis
64+
Properties:
65+
Stream: !GetAtt KinesisStream1.Arn
66+
MaximumBatchingWindowInSeconds: !Ref MyBatchingWindowParam
67+
StartingPosition: LATEST
68+
DestinationConfig:
69+
OnFailure:
70+
Destination: !Ref MySnsTopic
71+
StreamEventWithEmptyDestinationConfig:
72+
Type: Kinesis
73+
Properties:
74+
Stream: !GetAtt KinesisStream1.Arn
75+
MaximumBatchingWindowInSeconds: !Ref MyBatchingWindowParam
76+
StartingPosition: LATEST
77+
DestinationConfig:
6278

6379
KinesisStream:
6480
Type: AWS::Kinesis::Stream
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. The only valid values for 'Type' are 'SQS' and 'SNS'"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. The only valid values for 'Type' are 'SQS' and 'SNS'"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. 'OnFailure' is a required field for 'DestinationConfig'"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. 'OnFailure' is a required field for 'DestinationConfig'"
8+
}

tests/translator/output/aws-cn/function_with_event_source_mapping.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,49 @@
233233
}
234234
}
235235
},
236+
"MyFunctionForBatchingExampleStreamEventWithoutDestinationConfigType": {
237+
"Type": "AWS::Lambda::EventSourceMapping",
238+
"Properties": {
239+
"MaximumBatchingWindowInSeconds": {
240+
"Ref": "MyBatchingWindowParam"
241+
},
242+
"EventSourceArn": {
243+
"Fn::GetAtt": [
244+
"KinesisStream1",
245+
"Arn"
246+
]
247+
},
248+
"FunctionName": {
249+
"Ref": "MyFunctionForBatchingExample"
250+
},
251+
"StartingPosition": "LATEST",
252+
"DestinationConfig": {
253+
"OnFailure": {
254+
"Destination": {
255+
"Ref": "MySnsTopic"
256+
}
257+
}
258+
}
259+
}
260+
},
261+
"MyFunctionForBatchingExampleStreamEventWithEmptyDestinationConfig": {
262+
"Type": "AWS::Lambda::EventSourceMapping",
263+
"Properties": {
264+
"MaximumBatchingWindowInSeconds": {
265+
"Ref": "MyBatchingWindowParam"
266+
},
267+
"EventSourceArn": {
268+
"Fn::GetAtt": [
269+
"KinesisStream1",
270+
"Arn"
271+
]
272+
},
273+
"FunctionName": {
274+
"Ref": "MyFunctionForBatchingExample"
275+
},
276+
"StartingPosition": "LATEST"
277+
}
278+
},
236279
"KinesisStream": {
237280
"Type": "AWS::Kinesis::Stream",
238281
"Properties": {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. The only valid values for 'Type' are 'SQS' and 'SNS'"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. The only valid values for 'Type' are 'SQS' and 'SNS'"
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. 'OnFailure' is a required field for 'DestinationConfig'"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunction] is invalid. Event with id [MyFunctionStreamEvent] is invalid. 'OnFailure' is a required field for 'DestinationConfig'"
8+
}

tests/translator/output/aws-us-gov/function_with_event_source_mapping.json

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,49 @@
233233
}
234234
}
235235
},
236+
"MyFunctionForBatchingExampleStreamEventWithoutDestinationConfigType": {
237+
"Type": "AWS::Lambda::EventSourceMapping",
238+
"Properties": {
239+
"MaximumBatchingWindowInSeconds": {
240+
"Ref": "MyBatchingWindowParam"
241+
},
242+
"EventSourceArn": {
243+
"Fn::GetAtt": [
244+
"KinesisStream1",
245+
"Arn"
246+
]
247+
},
248+
"FunctionName": {
249+
"Ref": "MyFunctionForBatchingExample"
250+
},
251+
"StartingPosition": "LATEST",
252+
"DestinationConfig": {
253+
"OnFailure": {
254+
"Destination": {
255+
"Ref": "MySnsTopic"
256+
}
257+
}
258+
}
259+
}
260+
},
261+
"MyFunctionForBatchingExampleStreamEventWithEmptyDestinationConfig": {
262+
"Type": "AWS::Lambda::EventSourceMapping",
263+
"Properties": {
264+
"MaximumBatchingWindowInSeconds": {
265+
"Ref": "MyBatchingWindowParam"
266+
},
267+
"EventSourceArn": {
268+
"Fn::GetAtt": [
269+
"KinesisStream1",
270+
"Arn"
271+
]
272+
},
273+
"FunctionName": {
274+
"Ref": "MyFunctionForBatchingExample"
275+
},
276+
"StartingPosition": "LATEST"
277+
}
278+
},
236279
"KinesisStream": {
237280
"Type": "AWS::Kinesis::Stream",
238281
"Properties": {

0 commit comments

Comments
 (0)