33
44from samtranslator .model .lambda_ import LambdaEventSourceMapping
55from samtranslator .translator .arn_generator import ArnGenerator
6+ from samtranslator .model .exceptions import InvalidEventException
67
78
89class PullEventSource (ResourceMacro ):
910 """Base class for pull event sources for SAM Functions.
1011
11- The pull events are the streams-- Kinesis and DynamoDB Streams. Both of these correspond to an EventSourceMapping in
12- Lambda, and require that the execution role be given to Kinesis or DynamoDB Streams, respectively.
12+ The pull events are Kinesis Streams, DynamoDB Streams, and SQS Queues. All of these correspond to an EventSourceMapping in
13+ Lambda, and require that the execution role be given to Kinesis Streams, DynamoDB Streams, or SQS Queues , respectively.
1314
1415 :cvar str policy_arn: The ARN of the AWS managed role policy corresponding to this pull event source
1516 """
1617 resource_type = None
1718 property_types = {
18- 'Stream' : PropertyType (True , is_str ()),
19- 'BatchSize' : PropertyType (False , is_type (int )),
20- 'StartingPosition' : PropertyType (True , is_str ())
19+ 'Stream' : PropertyType (False , is_str ()),
20+ 'Queue' : PropertyType (False , is_str ()),
21+ 'BatchSize' : PropertyType (False , is_type (int )),
22+ 'StartingPosition' : PropertyType (False , is_str ())
2123 }
2224
2325 def get_policy_arn (self ):
@@ -32,23 +34,31 @@ def to_cloudformation(self, **kwargs):
3234 :rtype: list
3335 """
3436 function = kwargs .get ('function' )
35-
37+
3638 if not function :
3739 raise TypeError ("Missing required keyword argument: function" )
3840
3941 resources = []
4042
4143 lambda_eventsourcemapping = LambdaEventSourceMapping (self .logical_id )
4244 resources .append (lambda_eventsourcemapping )
43-
45+
4446 try :
4547 # Name will not be available for Alias resources
4648 function_name_or_arn = function .get_runtime_attr ("name" )
4749 except NotImplementedError :
4850 function_name_or_arn = function .get_runtime_attr ("arn" )
4951
52+ if not self .Stream and not self .Queue :
53+ raise InvalidEventException (
54+ self .relative_id , "No Queue (for SQS) or Stream (for Kinesis or DynamoDB) provided." )
55+
56+ if self .Stream and not self .StartingPosition :
57+ raise InvalidEventException (
58+ self .relative_id , "StartingPosition is required for Kinesis and DynamoDB." )
59+
5060 lambda_eventsourcemapping .FunctionName = function_name_or_arn
51- lambda_eventsourcemapping .EventSourceArn = self .Stream
61+ lambda_eventsourcemapping .EventSourceArn = self .Stream or self . Queue
5262 lambda_eventsourcemapping .StartingPosition = self .StartingPosition
5363 lambda_eventsourcemapping .BatchSize = self .BatchSize
5464
@@ -82,3 +92,11 @@ class DynamoDB(PullEventSource):
8292
8393 def get_policy_arn (self ):
8494 return ArnGenerator .generate_aws_managed_policy_arn ('service-role/AWSLambdaDynamoDBExecutionRole' )
95+
96+
97+ class SQS (PullEventSource ):
98+ """SQS Queue event source."""
99+ resource_type = 'SQS'
100+
101+ def get_policy_arn (self ):
102+ return ArnGenerator .generate_aws_managed_policy_arn ('service-role/AWSLambdaSQSExecutionRole' )
0 commit comments