Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.2K
Views
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
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

Firstly there's no native CLI command for getting a regions timezone so the timezone for the region needs to come from you.

You could create an environment variable of AWS_TIMEZONE and set the value to 'Asia/Kolkata'. You would then need to add import os and from pytz import timezone and call timezone = timezone(os.environ['AWS_TIMEZONE']) followed by updating now = datetime.now(timezone). Each region you deployed to you could set a different variable value for AWS_TIMEZONE.

However, I would recommend if you possibly could to actually store any date times in UTC as it leads to less confusion around timezone problems and is generally considered best practice.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!