Skip to content

Commit eb8ba58

Browse files
authored
Update analyitcs (#351)
* Update deploy-data-collection.yaml * refactor * refactor
1 parent 9cdf403 commit eb8ba58

File tree

1 file changed

+16
-40
lines changed

1 file changed

+16
-40
lines changed

data-collection/deploy/deploy-data-collection.yaml

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -528,49 +528,25 @@ Resources:
528528
ZipFile: |
529529
import os
530530
import json
531-
import uuid
532531
import urllib3
533-
import boto3
534-
535-
endpoint = os.environ['CID_ANALYTICS_ENDPOINT']
536-
account_id = boto3.client("sts").get_caller_identity()["Account"]
537-
538-
def lambda_handler(event, context): #pylint: disable=unused-argument
532+
import cfnresponse
533+
534+
def lambda_handler(event, context):
539535
print(json.dumps(event))
540536
try:
541-
if event['RequestType'].upper() not in ['CREATE', 'UPDATE', 'DELETE']:
542-
raise Exception(f"Unknown RequestType {event['RequestType']}") #pylint: disable=broad-exception-raised
543-
action = event['RequestType'].upper()
544-
name = event['ResourceProperties']['Name']
545-
method = {'CREATE':'PUT', 'UPDATE': 'PATCH', 'DELETE': 'DELETE'}.get(action)
546-
via_key = {'CREATE':'created_via', 'UPDATE': 'updated_via', 'DELETE': 'deleted_via'}.get(action)
547-
payload = {'id': 'data-collection-lab/' + name, 'account_id': account_id, via_key: 'CFN'}
548-
r = urllib3.PoolManager().request(method, endpoint, body=json.dumps(payload).encode('utf-8'), headers={'Content-Type': 'application/json'})
549-
if r.status != 200:
550-
raise Exception(f"There has been an issue logging action, server did not respond with a 200 response, actual status: {r.status}, response data {r.data.decode('utf-8')}. This issue will be ignored") #pylint: disable=broad-exception-raised
551-
res, reason = 'SUCCESS', 'success'
552-
except Exception as exc: #pylint: disable=broad-exception-caught
553-
res, reason = 'SUCCESS', f"{exc} . This issue will be ignored"
554-
body = {
555-
'Status': res,
556-
'Reason': reason,
557-
'PhysicalResourceId': event.get('PhysicalResourceId', str(uuid.uuid1())),
558-
'StackId': event.get('StackId'),
559-
'RequestId': event.get('RequestId'),
560-
'LogicalResourceId': event.get('LogicalResourceId'),
561-
'NoEcho': False,
562-
'Data': {'Reason': reason},
563-
}
564-
json_body=json.dumps(body)
565-
print(json_body)
566-
url = event.get('ResponseURL')
567-
if not url:
568-
return
569-
try:
570-
response = urllib3.PoolManager().request('PUT', url, body=json_body, headers={'content-type' : '', 'content-length' : str(len(json_body))}, retries=False)
571-
print(f"Status code: {response}")
572-
except Exception as exc: #pylint: disable=broad-exception-caught
573-
print("Failed sending PUT to CFN: " + str(exc))
537+
urllib3.PoolManager().request(
538+
method={'CREATE': 'PUT', 'UPDATE': 'PATCH', 'DELETE': 'DELETE'}[event['RequestType'].upper()],
539+
url=os.environ['CID_ANALYTICS_ENDPOINT'],
540+
body=json.dumps({
541+
'id': 'data-collection-lab/' + event['ResourceProperties'].get('Name'),
542+
'account_id': context.invoked_function_arn.split(':')[4],
543+
'via': 'CFN'
544+
}).encode('utf-8'),
545+
headers={'Content-Type': 'application/json'}
546+
)
547+
except Exception as exc:
548+
print(f"Exception occurred: {exc}")
549+
cfnresponse.send(event, context, cfnresponse.SUCCESS, {'Reason': 'success'})
574550
575551
LambdaInitRole: #Execution role for the custom resource for Init Lambda
576552
Type: AWS::IAM::Role

0 commit comments

Comments
 (0)