-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathanonymized-data-logger.py
66 lines (63 loc) · 3.25 KB
/
anonymized-data-logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# -*- coding: utf-8 -*-
##############################################################################
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #
# #
# Licensed under the Apache License Version 2.0 (the "License"). #
# You may not use this file except in compliance with the License. #
# A copy of the License is located at #
# #
# http://www.apache.org/licenses/ #
# #
# or in the "license" file accompanying this file. This file is distributed #
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, #
# express or implied. See the License for the specific language governing #
# permissions and limitations under the License. #
##############################################################################
#
# PURPOSE:
# This function sends anonymized performance data to the AWS
# Solutions metrics API. This information is anonymized and helps improve the
# quality of the solution.
#
##############################################################################
import uuid
import lib.cfnresponse as cfn
import lib.metrics as Metrics
def handler(event, context):
print("We got this event:\n", event)
# Each resource returns a promise with a json object to return cloudformation.
try:
request_type = event['RequestType']
resource = event['ResourceProperties']['Resource']
config = event['ResourceProperties']
# Remove ServiceToken (lambda arn) to avoid sending AccountId
config.pop("ServiceToken", None)
config.pop("Resource", None)
# Add some useful fields related to stack change
config["CFTemplate"] = (
request_type + "d"
) # Created, Updated, or Deleted
response_data = {}
print('Request::{} Resource::{}'.format(request_type, resource))
if request_type == 'Create' or request_type == 'Update':
if resource == 'UUID':
response_data = {'UUID': str(uuid.uuid4())}
unique_id = response_data['UUID']
cfn.send(event, context, 'SUCCESS', response_data, unique_id)
elif resource == 'AnonymizedMetric':
Metrics.send_metrics(config)
unique_id = 'Metrics Sent'
cfn.send(event, context, 'SUCCESS', response_data, unique_id)
else:
print('Create failed, {} not defined in the Custom Resource'.format(resource))
cfn.send(event, context, 'FAILED', {}, context.log_stream_name)
elif request_type == 'Delete':
print('RESPONSE:: {}: Not required to report data for delete request.'.format(resource))
cfn.send(event, context, 'SUCCESS', {})
else:
print('RESPONSE:: {} Not supported'.format(request_type))
except Exception as e:
print('Exception: {}'.format(e))
cfn.send(event, context, 'FAILED', {}, context.log_stream_name)
print(e)