Skip to content

Commit

Permalink
Update SNS details
Browse files Browse the repository at this point in the history
  • Loading branch information
timjell committed Dec 15, 2022
1 parent 7babfd0 commit ccb6f87
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ Resources:
- arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole
- arn:aws:iam::aws:policy/AmazonRedshiftDataFullAccess
- arn:aws:iam::aws:policy/AmazonRedshiftQueryEditor
- arn:aws:iam::aws:policy/AmazonSNSFullAccess
Path: "/"

RedshiftQMRLambdaFunction:
Expand Down
18 changes: 12 additions & 6 deletions src/QMRNotificationUtility/lambda/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def query_redshift():
objects_list = []

for row in result_rows:

print(row)
userid,query,service_class,rule,action,recordtime = row
d = collections.OrderedDict()
d['clusterid'] = clusterid
Expand All @@ -151,15 +153,17 @@ def query_redshift():
d['service_class'] = service_class
d['rule'] = rule
d['action'] = action
d['recordtime'] = recordtime.isoformat()
d['recordtime'] = recordtime #--.isoformat()

objects_list.append(d)

#Publish to SNS if any rows fetched
if len(objects_list) == 0:
print('No rows to publish to SNS')
else:
print("Publishing rows to SNS")
response= publish_to_sns(objects_list)
query_result_json = json.dumps(objects_list)
response = publish_to_sns(query_result_json)

print('Completed Succesfully ')
return 'Success'
Expand All @@ -169,6 +173,8 @@ def query_redshift():
return 'Failed'

def publish_to_sns(message):
global platform_endpoint

try:
# Publish a message.
response = platform_endpoint.publish(
Expand All @@ -181,9 +187,9 @@ def publish_to_sns(message):
'DataType': 'String'
}
}

)


print("Published message...")
return response

except:
Expand Down Expand Up @@ -222,10 +228,10 @@ def lambda_handler(event, context):
try:
sns = boto3.resource('sns')
platform_endpoint = sns.PlatformEndpoint('{sns_arn}'.format(sns_arn = sns_arn))

except:
print('SNS access failed: Exception %s' % sys.exc_info()[1])


# Execute the QMR query
query_redshift()

0 comments on commit ccb6f87

Please sign in to comment.