Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set timestamp to aws region timezone for aws lambda python code

in my python lambda code that scans dynamodb based on primary key as 'deviceId' & sortkey timestamp( which is in YYYY-MM-DD HH:MN:SS format) , i need to scan every for last 15min data (from time now). I am in Mumbai region (ap-south-1), how do I set local timezone in my following lambda code. since it is picking default utc datetime.The lambda ideally has to scan & if return count >= 10 then thermostat+5 .

import boto3
import math
import json
import time
from datetime import datetime,timedelta
from dateutil.tz import tzlocal
from boto3.dynamodb.conditions import Key, Attr

client = boto3.client('dynamodb')
dynamodb = boto3.resource('dynamodb')

def lambda_handler(event, context):

    #table_name= "thermostat_dynamo"
    table_name= "newsensor"
    Primary_Column_Name = 'deviceId'
    table = dynamodb.Table(table_name)
    #key_param = "thermostat"
    #thermostatVal = table.get_item(Key={key_param:event[key_param]}) ## get record from dynamodb for this sensor
    thermostatVal= 77
    #now = datetime.now()
    now = datetime.now(tzlocal())
    fifteen_min_ago =  now - timedelta(seconds=900)
    now = now.strftime('%F %T')
    fifteen_min_ago = fifteen_min_ago.strftime('%F %T')

    fe = Key('timeStamp').between(fifteen_min_ago,now);
    response = table.scan(FilterExpression=fe & Attr('temperature').lt(thermostatVal))

    if response['Count'] == 10:
    #return thermostatVal+5 
        thermonew = thermostatVal + 5
        tosensor = '{"thermostat":'+'"%s"}' %thermonew
        print(tosensor)
        #response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
        return

    elif response['Count'] < 10:
        #tosensor = '{"thermostat":'+'"%s"}' %thermostatVal
        print('{"thermostat":'+'"%s"}' %thermostatVal)
        #response = client.publish(topic="updatehomesensor", qos=1, payload=tosensor)
        return
like image 957
Sandeep Nag Avatar asked Jul 10 '26 23:07

Sandeep Nag


1 Answers

All Lambda instances are set to UTC timezone. Incase you want to change that behavior you need to update environment variable.

Detailed document is below which mentions TZ as Reserved environment variable https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html.

So in your case for example you want to setup lambda to use IST [ Indian Standard Timezone ]. You need to go through following steps.

  1. In configuration tab of function scroll down to environment variables section.
  2. Click Edit and add variable TZ with value Asia/Calcutta [ For IST ].

It should look like below

enter image description here

After saving the environment variable your environment will use timezone mentioned in the TZ variable.

like image 103
Ketav Sharma Avatar answered Jul 14 '26 12:07

Ketav Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!