Skip to content
Vaquar Khan edited this page Nov 7, 2022 · 2 revisions
		 `import json`
		`import boto3`
		`from boto3.dynamodb.conditions import Key`
		`from datetime import datetime`



		`#dynamodb_client = boto3.client('dynamodb')`
		`dynamodb = boto3.resource('dynamodb')`


		`def lambda_handler(event, context):`
			
			`# ct stores current time`
			`ct =  datetime.now()`
			`print("current time:-", ct)`
			  
			`# ts store timestamp of current time`
			`datetime_end = ct.timestamp()`
			`print("timestamp:-", datetime_end)`
			
			`###################################`
			`#currentTime=str(ct).rsplit('.',1)[0]`
			`currentTime=fixedTimeStampForDDB(ct)`
			`print("currentTime==="+currentTime)`

			`#  =15 min logic for stream id`
			`fmt = '%Y-%m-%d %H:%M:%S'`
			`tstamp1 = datetime.strptime('2022-06-03 18:44:31', fmt)`
			`tstamp2 = datetime.strptime('2022-06-03 19:09:58', fmt)`
			`'''`
			`if tstamp1 > tstamp2:`
				`td = tstamp1 - tstamp2`
			`else:`
				`td = tstamp2 - tstamp1`
			`td_mins = int(round(td.total_seconds() / 60))`
			
			`print('The difference is approx. %s minutes' % td_mins)`
			`'''`
			`###########################################`

			`#List table`
			`print(list(dynamodb.tables.all()))`
			
			`# Insert into dynamodb table`
			`table = dynamodb.Table('Employees')`

			`response = table.put_item(`
			`Item = { `
				 `'Name': 'Kelvin Galabuzi',`
				 `'Email': 'kelvingalabuzi@handson.cloud'`
				   `}`
			`)`
			`print(response)`
			`#`
			`#`
			`# boto3 dynamodb getitem`
			`print("===============================================================")`
			`response = table.get_item(`
				`Key={`
					`'Name': 'Kelvin Galabuzi',`
					`'Email': 'kelvingalabuzi@handson.cloud'`
				`}`
			`)`
			`print(response['Item'])`
			
			`print("===============================================================")`
			`# boto3 ddb query`
			`response = table.query(KeyConditionExpression=Key('Name').eq('Kelvin Galabuzi'))`
			
			`print("The query returned the following items:")`
			`for item in response['Items']:`
				`print(item)`


			`return {`
				`'statusCode': 200,`
				`'body': json.dumps('Hello from Lambda!')`
			`}`
			
		`####################################################`
		`# This method return timeDiff between two timestamp`
		`####################################################    `
		`def calculateTimeDiff(ts1,ts2):`
			`#currentTime=str(ct).rsplit('.',1)[0]`
			`#print("currentTime==="+currentTime)`

			`#  =15 min logic for stream id`
			`#tstamp1 = datetime.strptime('2022-06-03 18:44:31', fmt)`
			`#tstamp2 = datetime.strptime('2022-06-03 19:09:58', fmt)`
			
			`if tstamp1 > tstamp2:`
				`td = tstamp1 - tstamp2`
			`else:`
				`td = tstamp2 - tstamp1`
			`td_mins = int(round(td.total_seconds() / 60))`
			
			`#print('The difference is approx. %s minutes' % td_mins)`
			`return td_mins`

		`####################################################`
		`# This method return proper timestamp for instert into Dynamodb`
		`####################################################`
		`def fixedTimeStampForDDB(currentTimestamp):`
			`currentTime=str(currentTimestamp).rsplit('.',1)[0]`
			`#print("currentTime==="+currentTime)`
			`return currentTime`
Clone this wiki locally