Skip to content

Instantly share code, notes, and snippets.

@anggras
Last active April 22, 2021 04:31
Show Gist options
  • Save anggras/6762bb617f9368dbe69f46984d859604 to your computer and use it in GitHub Desktop.
Save anggras/6762bb617f9368dbe69f46984d859604 to your computer and use it in GitHub Desktop.
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
import time
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('IoTRule')
current_time_s = time.time() # current time in second (float)
current_time_ms = round(current_time_s * 1000) # current time in ms (integer)
cutoff_time_ms = current_time_ms - (24 * 60 * 60 * 1000) # 24hrs before
result = table.scan( # Use scan for range 'query'
FilterExpression=Attr('ts').gt(cutoff_time_ms)
)
# result = table.query( # Use query to find single item (exact)
# KeyConditionExpression=Key('ts').gt(cutoff_time_ms)
# )
print(result['Items'])
return "Ok"
import json
import boto3
import time
def lambda_handler(event, context):
dynamodb_client = boto3.client('dynamodb')
current_time_s = time.time() # current time in second (float)
current_time_ms = round(current_time_s * 1000) # current time in ms (integer)
cutoff_time_ms = current_time_ms - (24 * 60 * 60 * 1000) # 24hrs before
humidity = 65
# Note: You need to give permission to the policy to execute PartiQL query
# PartiQL reference: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ql-reference.html
result = dynamodb_client.execute_statement(Statement=f'SELECT * FROM IoTRule WHERE ts > {cutoff_time_ms} AND humidity > {humidity}')
print(result['Items'])
return result['Items']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment