forked from techcto/rke-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawslambda.py
57 lines (47 loc) · 2.08 KB
/
awslambda.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
import boto3,json,os
from botocore.vendored import requests
responseData = {}
SUCCESS = "SUCCESS"
FAILED = "FAILED"
class AwsLambda:
def __init__(self, asg):
print("Init AwsLambda Class")
self.asg = asg
self.snsClient = boto3.client('sns')
self.s3Client = boto3.resource('s3')
def publish_sns_message(self, subject):
try:
self.snsClient.publish(TopicArn=self.asg.snsTopicArn,Message=json.dumps(self.asg.snsMessage),Subject=subject)
return True
except Exception as e:
print("SNS message not supported:" + str(e))
responseData['status'] = "success"
try:
print("Tell Cloudformation we are good!")
self.send_response(self.asg.event, self.asg.context, SUCCESS, responseData)
except BaseException as e:
return False
return False
def send_response(self, event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False):
responseUrl = event['ResponseURL']
print(responseUrl)
responseBody = {}
responseBody['Status'] = responseStatus
responseBody['Reason'] = 'See the details in CloudWatch Log Stream: ' + context.log_stream_name
responseBody['PhysicalResourceId'] = physicalResourceId or context.log_stream_name
responseBody['StackId'] = event['StackId']
responseBody['RequestId'] = event['RequestId']
responseBody['LogicalResourceId'] = event['LogicalResourceId']
responseBody['NoEcho'] = noEcho
responseBody['Data'] = responseData
json_responseBody = json.dumps(responseBody)
print("Response body:\n" + json_responseBody)
headers = {
'content-type' : '',
'content-length' : str(len(json_responseBody))
}
try:
response = requests.put(responseUrl, data=json_responseBody, headers=headers)
print("Status code: " + response.reason)
except Exception as e:
print("send(..) failed executing requests.put(..): " + str(e))