15
15
import boto3
16
16
import botocore
17
17
import logging
18
+ from datetime import datetime
18
19
19
20
LOG = logging .getLogger (__name__ )
20
21
21
22
22
23
class KinesisSubscriber (object ):
23
24
''' Invokes the lambda function on events from the Kinesis streams '''
24
25
def __init__ (self , config , profile_name ,
25
- function_name , stream_name , batch_size ):
26
+ function_name , stream_name , batch_size ,
27
+ starting_position , starting_position_ts = None ):
26
28
self ._aws_session = boto3 .session .Session (region_name = config .region ,
27
29
profile_name = profile_name )
28
30
self ._lambda_client = self ._aws_session .client ('lambda' )
29
31
self .function_name = function_name
30
32
self .stream_name = stream_name
31
33
self .batch_size = batch_size
34
+ self .starting_position = starting_position
35
+ self .starting_position_ts = starting_position_ts
32
36
33
37
def subscribe (self ):
34
38
''' Subscribes the lambda to the Kinesis stream '''
35
39
try :
36
40
LOG .debug ('Creating Kinesis subscription' )
37
- self ._lambda_client \
38
- .create_event_source_mapping (EventSourceArn = self .stream_name ,
39
- FunctionName = self .function_name ,
40
- BatchSize = self .batch_size ,
41
- StartingPosition = 'TRIM_HORIZON' )
41
+ if self .starting_position_ts :
42
+ self ._lambda_client \
43
+ .create_event_source_mapping (
44
+ EventSourceArn = self .stream_name ,
45
+ FunctionName = self .function_name ,
46
+ BatchSize = self .batch_size ,
47
+ StartingPosition = self .starting_position ,
48
+ StartingPositionTimestamp = self .starting_position_ts )
49
+ else :
50
+ self ._lambda_client \
51
+ .create_event_source_mapping (
52
+ EventSourceArn = self .stream_name ,
53
+ FunctionName = self .function_name ,
54
+ BatchSize = self .batch_size ,
55
+ StartingPosition = self .starting_position )
42
56
LOG .debug ('Subscription created' )
43
57
except botocore .exceptions .ClientError as ex :
44
58
response_code = ex .response ['Error' ]['Code' ]
@@ -56,6 +70,13 @@ def create_subscriptions(config, profile_name):
56
70
function_name = config .name
57
71
stream_name = data ['stream' ]
58
72
batch_size = data ['batch_size' ]
73
+ starting_position = data ['starting_position' ]
74
+ starting_position_ts = None
75
+ if starting_position == 'AT_TIMESTAMP' :
76
+ ts = data .get ('starting_position_timestamp' )
77
+ starting_position_ts = datetime .strptime (ts , '%Y-%m-%dT%H:%M:%SZ' )
59
78
s = KinesisSubscriber (config , profile_name ,
60
- function_name , stream_name , batch_size )
79
+ function_name , stream_name , batch_size ,
80
+ starting_position ,
81
+ starting_position_ts = starting_position_ts )
61
82
s .subscribe ()
0 commit comments