Skip to content

Commit 78ee0b1

Browse files
authored
cfnresponse module
1 parent a95c641 commit 78ee0b1

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

cfnresponse.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
# SPDX-License-Identifier: MIT-0
3+
4+
from __future__ import print_function
5+
import urllib3
6+
import json
7+
8+
SUCCESS = "SUCCESS"
9+
FAILED = "FAILED"
10+
11+
http = urllib3.PoolManager()
12+
13+
14+
def send(event, context, responseStatus, responseData, physicalResourceId=None, noEcho=False, reason=None):
15+
responseUrl = event['ResponseURL']
16+
17+
print(responseUrl)
18+
19+
responseBody = {
20+
'Status' : responseStatus,
21+
'Reason' : reason or "See the details in CloudWatch Log Stream: {}".format(context.log_stream_name),
22+
'PhysicalResourceId' : physicalResourceId or context.log_stream_name,
23+
'StackId' : event['StackId'],
24+
'RequestId' : event['RequestId'],
25+
'LogicalResourceId' : event['LogicalResourceId'],
26+
'NoEcho' : noEcho,
27+
'Data' : responseData
28+
}
29+
30+
json_responseBody = json.dumps(responseBody)
31+
32+
print("Response body:")
33+
print(json_responseBody)
34+
35+
headers = {
36+
'content-type' : '',
37+
'content-length' : str(len(json_responseBody))
38+
}
39+
40+
try:
41+
response = http.request('PUT', responseUrl, headers=headers, body=json_responseBody)
42+
print("Status code:", response.status)
43+
44+
45+
except Exception as e:
46+
47+
print("send(..) failed executing http.request(..):", e)

0 commit comments

Comments
 (0)