Skip to content

Instantly share code, notes, and snippets.

@ottokruse
Last active December 13, 2019 10:53
Show Gist options
  • Save ottokruse/28b8642e262a3b55cd00b232c11c30d5 to your computer and use it in GitHub Desktop.
Save ottokruse/28b8642e262a3b55cd00b232c11c30d5 to your computer and use it in GitHub Desktop.
A better cfn-response for Python3, only depends on stdlib
from urllib.request import Request, urlopen
import json
class CfnResponse:
SUCCESS = "SUCCESS"
FAILED = "FAILED"
@staticmethod
def send(
event,
context,
response_status,
reason="See CloudWatch Logs",
response_data=None,
physical_resource_id=None,
):
response = {
"Status": response_status,
"Reason": reason,
"PhysicalResourceId": physical_resource_id or context.log_stream_name,
"StackId": event["StackId"],
"RequestId": event["RequestId"],
"LogicalResourceId": event["LogicalResourceId"],
"Data": response_data or {},
}
urlopen(
Request(
event["ResponseURL"],
data=json.dumps(response).encode(),
headers={"content-type": ""},
method="PUT",
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment