in publishfunction/cwmetricstream2.py [0:0]
def lambda_handler(event, context):
for record in event['Records']:
metricData = []
try:
event_timestamp = record['dynamodb']['NewImage']['EventTimestamp']['N']
timestamp = float(event_timestamp) / 1000
event_time = datetime.utcfromtimestamp(timestamp).strftime('%Y-%m-%dT%H:%M:%S')
print('event_time=' + event_time)
metric_type = record['dynamodb']['NewImage']['MetricType']['S']
is_metric_int = isWholeNumber(metric_type)
for metric_detail in record['dynamodb']['NewImage']['MetricDetails']['L']:
if is_metric_int:
value = float(metric_detail['M']['UNITVALUEINT']['N'])
else:
value = float(metric_detail['M']['UNITVALUEFLOAT']['N'])
if metric_detail['M']['METRICITEM']['S'] == 'null':
metricname = metric_type
else:
metricname = metric_detail['M']['METRICITEM']['S']
metricDataItem={
'MetricName': metricname,
'Timestamp': event_time,
'Value': value,
'Unit': 'None',
'StorageResolution': 1
}
metricData.append(metricDataItem)
namespace = prettyUp(metric_type)
print('metrics to cwc = {}'.format(metricData))
cwc=boto3.client('cloudwatch')
response = cwc.put_metric_data(Namespace=namespace,MetricData=metricData)
print(response)
except: # skip when records are removed via TTL
print('Skip removed records ({})'.format(sys.exc_info()[0]))
#raise
return 'Successfully processed records.'