Skip to content

Instantly share code, notes, and snippets.

nivleshc

Block or report user

Report or block nivleshc

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@nivleshc
nivleshc / cfn-amazon-connect-create-lambda-dynamodbTable.yml
Created Oct 10, 2019
This AWS CloudFormation template deploys an Amazon DynamoDB Table and two AWS Lambda functions. These will be used by the Amazon Connect instance.
View cfn-amazon-connect-create-lambda-dynamodbTable.yml
AWSTemplateFormatVersion: "2010-09-09"
Description: Template for deploying Amazon DynamoDB and AWS Lambda functions that will be used by the Amazon Connect instance
Parameters:
authorisedUsersTablename:
Description: Name of the Amazon DynamoDB Table that will be created to store phone numbers for approved callers to Amazon Connect
Type: String
Default: amzn-connect-authorisedUsers
DynamoDBBillingMode:
Description: Billing mode to be used for authorisedUsers Amazon DynamoDB Table
@nivleshc
nivleshc / manageInstances.py
Last active Oct 9, 2019
This is the code for an AWS Lambda function. Amazon SES will invoke this lambda function to manage Amazon EC2 instances
View manageInstances.py
import boto3
def provideHelp(params):
message = "Set the subject of the email to one of the following\n"
message += "help - provides this help\n"
message += "status - provides the status of all ec2 instances in the region\n"
message += "start {instance-id} - starts the ec2 instance with with the specified instance-id\n"
message += "stop {instance-id} - stops the ec2 instance with the specified instance id"
return message
View tps_vehiclepos_full.py
#this script will query OpenData API to get the current positions for Sydney Trains. It will then use this information to plot the
#positions on a map
from google.transit import gtfs_realtime_pb2
import requests
import datetime
import boto3
import os
import time
from pytz import timezone
View tps_vehiclepos_run.py
def run(event, context):
print(datetime.datetime.now(), 'Started')
today = datetime.datetime.now(tz_sydney)
initialise_global_variables()
load_pushpinLabels_from_dynamodb(today)
#loop through each operator and get their vehicle positions
for operator in operators:
callOpenData(operator, feed)
View tps_vehiclepos_update_pushpinLabelsTable.py
def update_pushpinLabelsTable(tripdate):
global lastpushpinLabel
#update the pushpinLabels dynamodb table with all the pushpinLabels that were created in this invocation
print("update_pushpinLabelsTable:Uploading new pushpinLabels to dynamodb Table")
time_now = datetime.datetime.now(tz_sydney) #each item uploaded to dynamodb table will have the time it was inserted
time_now_str = time_now.strftime("%Y-%m-%d %H:%M:%S")
epoch_time_now = time_now.timestamp()
expirationTime = int(epoch_time_now + (dynamodb_item_expiry_in_days * 24 * 3600)) #convert expiry days to seconds
print("update_pushpinLabelsTable:time_now:", time_now_str," epoch_time_now:", epoch_time_now, " expirationTime:", expirationTime)
num_items_added_to_dynamodb = 0
View tps_vehiclepos_generate_vehicle_position_webpage.py
def generate_vehicle_position_webpage(map_url, body, s3Bucket, s3ImageKey, imagefilename, mainHtmlfilename, today):
global dict_pushpinLabel_to_desc_map
global dict_vehicleId_to_pushpinLabel_map
global lastpushpinLabel
global total_vehicles_found
#get a handle on the s3 bucket
s3 = boto3.resource('s3')
imgObject = s3.Object(s3Bucket,s3ImageKey+imagefilename)
map_webrequest = requests.post(map_url, data=body)
View tps_vehiclepos_process_feed.py
def process_feed(feed, today):
global dict_pushpinLabel_to_desc_map
global dict_vehicleId_to_pushpinLabel_map
global lastpushpinLabel
global pushpin_url
global total_vehicles_found
#we will use the python module 'time' to convert epoch time (this is what gtfsr timestamps are in) to local time
#set the timezone for time
os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
View tps_vehiclepos_callOpenData.py
def callOpenData(operator, feed):
print("callOpenData:Obtaining vehicle location for", operator,end="...")
response = requests.get(baseURL + operator, headers=headers)
feed.ParseFromString(response.content)
print("done")
View tps_vehiclepos_load_pushpinLabels_from_dynamodb.py
#read the contents ot pushpinLabelsTable and populate pushpinLabels_LabelNum and pushpinLabels_Label dict
def load_pushpinLabels_from_dynamodb(today):
global dict_pushpinLabel_to_desc_map
global dict_vehicleId_to_pushpinLabel_map
global lastpushpinLabel
rdate = today.strftime("%Y-%m-%d")
pushpinLabelsTableContents = pushpinLabelsTable.query(
IndexName='TripDate-GSI',
KeyConditionExpression='TripDate = :tripdate',
View tps_vehiclepos_initialise_global_variables.py
#most of the times, lambda functions get re-used by AWS for consequetive runs. To ensure the global variables are sanitised and do not
#cause issues because they contain values from last run, we will initialise them
def initialise_global_variables():
global lastpushpinLabel
global dict_pushpinLabel_to_desc_map
global dict_vehicleId_to_pushpinLabel_map
global pushpin_url
global total_vehicles_found
#initialise the values for the above global variables
You can’t perform that action at this time.